Browse - Programming Tips - Postgresql Make Count Faster

Date: 2011jul26
Product: Postgresql


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,_ANALYZE,_EXPLAIN,_and_COUNT#COUNT.28.2A.29