If PostgreSQL count(*) is always slow how to paginate complex queries?

后端 未结 2 1649
鱼传尺愫
鱼传尺愫 2020-12-08 14:50

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

2条回答
  •  Happy的楠姐
    2020-12-08 15:36

    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

提交回复
热议问题