Re: Select max(foo) and select count(*) optimization
Tom Lane <tgl@sss.pgh.pa.us>
From: Tom Lane <tgl@sss.pgh.pa.us>
To: "D'Arcy J.M. Cain" <darcy@druid.net>
Cc: Shridhar Daithankar <shridhar_daithankar@persistent.co.in>, Postgresql Performance <pgsql-performance@postgresql.org>
Date: 2004-01-06T23:19:55Z
Lists: pgsql-performance
"D'Arcy J.M. Cain" <darcy@druid.net> writes: > In any case, if I have to vacuum a 20,000,000 row table to get an accurate > count then I may as well run count(*) on it. > (*): Actually I only analyze but I understand that that should be sufficient. ANALYZE without VACUUM will deliver a not-very-accurate estimate, since it only looks at a sample of the table's pages and doesn't grovel through every one. Any of the VACUUM variants, on the other hand, will set pg_class.reltuples reasonably accurately (as the number of rows actually seen and left undeleted by the VACUUM pass). There are pathological cases where ANALYZE's estimate of the overall row count can be horribly bad --- mainly, when the early pages of the table are empty or nearly so, but there are well-filled pages out near the end. I have a TODO item to try to make ANALYZE less prone to getting fooled that way... regards, tom lane