Faster PostgreSQL Counting

PHOTO EMBED

Fri Apr 30 2021 19:03:30 GMT+0000 (Coordinated Universal Time)

Saved by @mvieira

-- create a million random numbers and strings
CREATE TABLE items AS
  SELECT
    (random()*1000000)::integer AS n,
    md5(random()::text) AS s
  FROM
    generate_series(1,1000000);

-- inform planner of big table size change
VACUUM ANALYZE;
content_copyCOPY

https://www.citusdata.com/blog/2016/10/12/count-performance/