Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1XjjzM-0001uv-Ou for pgsql-admin@arkaria.postgresql.org; Thu, 30 Oct 2014 07:17:44 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1XjjzM-00066Y-2E for pgsql-admin@arkaria.postgresql.org; Thu, 30 Oct 2014 07:17:44 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1XjjzL-00066S-7o for pgsql-admin@postgresql.org; Thu, 30 Oct 2014 07:17:43 +0000 Received: from mail-pd0-x236.google.com ([2607:f8b0:400e:c02::236]) by makus.postgresql.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:256) (Exim 4.80) (envelope-from ) id 1XjjzA-0001iO-2n for pgsql-admin@postgresql.org; Thu, 30 Oct 2014 07:17:41 +0000 Received: by mail-pd0-f182.google.com with SMTP id fp1so4623623pdb.27 for ; Thu, 30 Oct 2014 00:17:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:message-id:date:user-agent:mime-version:to:subject :content-type:content-transfer-encoding; bh=GlhN6NVy1iDa5q1LIXrTHCFAZZjhqg7VdwNEgECG9t4=; b=ijv1QP23OYuS4zA2UMVr67fNxfXS7cl8oKCHCbTeg0pkIqMH+pojtU8aTVGdHN/4Sx y2kwVqmlhemja3n186N+04f2aEu5ZSj98Fj/ehcRJnewVxD/VvuzH/gcHam3g6WmgPFa VhQP1/yxREndD/zyQITUErQY9frj5mdP+URDj+4vPsToCCJSCfFRgql6F6hSeMDDlNVS NkGp+2CYxeDOJzpDpzY7TpBDaasWLG9bWDf0CSORiK6j9FqtrzgGqZcmlZBLvr4yGmCu KupMbIqnVX9xRdoa4/oXLAt0FtqM0QgBl3vR3kLoPdkmJDqdnZfUqvn82rVnl49sLexy K2QA== X-Received: by 10.68.104.194 with SMTP id gg2mr4719517pbb.119.1414653449960; Thu, 30 Oct 2014 00:17:29 -0700 (PDT) Received: from [192.168.2.104] ([180.173.32.164]) by mx.google.com with ESMTPSA id p10sm6196779pds.37.2014.10.30.00.17.28 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 30 Oct 2014 00:17:29 -0700 (PDT) From: Rural Hunter X-Google-Original-From: Rural Hunter Message-ID: <5451E60A.60009@gmail.com> Date: Thu, 30 Oct 2014 15:17:30 +0800 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:31.0) Gecko/20100101 Thunderbird/31.2.0 MIME-Version: 1.0 To: pgsql-admin@postgresql.org Subject: wrong count estimation in query plan Content-Type: text/plain; charset=gb18030; format=flowed Content-Transfer-Encoding: 7bit X-Pg-Spam-Score: -2.7 (--) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-admin Precedence: bulk Sender: pgsql-admin-owner@postgresql.org Hi, I have a table with very frequent updates while inserts and deletes are very few. Recently I noticed the count estimation of the table in query plan are far wrong. This is what I found: # select count(*) from users; count ------- 1116 (1 row) testdb=# explain select * from users; QUERY PLAN --------------------------------------------------------------- Seq Scan on users (cost=0.00..34456.68 rows=73468 width=625) (1 row) testdb=# vacuum analyze users; VACUUM testdb=# explain select * from users; QUERY PLAN --------------------------------------------------------------- Seq Scan on users (cost=0.00..34705.32 rows=97332 width=624) (1 row) testdb=# analyze users; ANALYZE testdb=# explain select * from users; QUERY PLAN --------------------------------------------------------------- Seq Scan on users (cost=0.00..33857.43 rows=11743 width=625) (1 row) testdb=# analyze users; ANALYZE testdb=# explain select * from users; QUERY PLAN -------------------------------------------------------------- Seq Scan on users (cost=0.00..33765.88 rows=2288 width=648) (1 row) testdb=# analyze users; \ANALYZE testdb=# explain select * from users; QUERY PLAN -------------------------------------------------------------- Seq Scan on users (cost=0.00..33778.94 rows=1294 width=625) (1 row) testdb=# analyze users; ANALYZE testdb=# explain select * from users; QUERY PLAN -------------------------------------------------------------- Seq Scan on users (cost=0.00..33777.19 rows=1119 width=624) (1 row) My question is, why I had to run analyze so many times to make the estimation be close with the actual count? -- Sent via pgsql-admin mailing list (pgsql-admin@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-admin