If PostgreSQL\'s count(*) is always slow how to paginate complex queries?
Making triggers doesn\'t seem to be a good solution as long as in this case we
If you're doing SELECT count(*) FROM table and have pg stats enabled you can use the lower example, which in this case goes from 13ms down to 0.05ms.
SELECT count(*) FROM news;
26171
EXPLAIN ANALYZE SELECT count(*) FROM news;
Total runtime: 13.057 ms
SELECT reltuples::bigint AS count FROM pg_class WHERE oid = 'public.news'::regclass;
26171
EXPLAIN ANALYZE SELECT reltuples::bigint AS count FROM pg_class WHERE oid = 'public.news'::regclass;
Total runtime: 0.053 ms