I am running the below query in postgres DB which has around 2,861,092,854 records.
Type column is indexed and it can have 2 values either 'customer' or 'vendor'
Why it is taking this much time?
SELECT count(*) FROM companies where type='vendor';
Explain Analyze Query Response
Finalize Aggregate (cost=61231320.98..61231320.98 rows=1 width=8) (actual time=756767.565..756774.121 rows=1 loops=1)
-> Gather (cost=61231320.76..61231320.97 rows=2 width=8) (actual time=756767.489..756774.115 rows=3 loops=1)
Workers Planned: 2
Workers Launched: 2
-> Partial Aggregate (cost=61230320.76..61230320.77 rows=1 width=8) (actual time=756764.026..756764.028 rows=1 loops=3)
-> Parallel Index Only Scan using companies_type on companies (cost=0.58..59886514.92 rows=537522334 width=0) (actual time=0.256..735750.564 rows=434640967 loops=3)
Index Cond: (type = 'vendor'::text)
Heap Fetches: 1303955010
Planning Time: 7.047 ms
Execution Time: 756774.221 ms
(10 rows)
type='vendor', e.g.CREATE INDEX idx_company_type_vendor ON companies (type) WHERE type = 'vendor'. This will reduce query time, but perhaps slow down inserts and updates a little bit.explain (analyze, buffers, timing)would be interesting to judge how fast your disk is (settrack_io_timingtoonbefore doing that)