Dave's Brain

Browse - Programming Tips - Postgresql Make Count Faster

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

Add a comment

Sign in to add a comment
Copyright © 2008-2017, 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.