Dave's Brain

Browse - programming tips - postgresql slow count

Date: 2011jul26
Product: Postgresql

Q.
SELECT COUNT(*) FROM tablename;
is slow in Postgresql.  How can I make it faster?


A.   Instead do:

SELECT reltuples FROM pg_class WHERE oid = 'schemaname.tablename'::regclass::oid;

The number returned is an estimate of the number of tables in the table at the time of the last ANALYZE. 

Source http://wiki.postgresql.org/wiki/Introduction_to_VACUUM,_ANALYZE,_EXPLAIN,_and_COUNT#COUNT.28.2A.29
What this info useful to you? You can donate to say thanks

Add a comment

Sign in to add a comment
Copyright © 2008-2012, dave - Code samples on Dave's Brain is licensed under the Creative Commons Attribution 2.5 License. However other material, including English text has all rights reserved.
Advertisements: