elect count(*) as count, "CD_ID" from "SDS" GROUP BY "CD_ID" ORDER BY count DESC LIMIT 50; count | CD_ID --------+-------- 717971 | 5923 717905 | 427785 715656 | 437457 715655 | 440053 714934 | 467278 629928 | 438681 584551 | 427948 584551 | 6148 394061 | 219757 387904 | 212802 379888 | 467264 363500 | 427894 60757 | 7962 60757 | 467275 60757 | 467263 25659 | 1273 24147 | 1272 19573 | 428052 19539 | 428092 18459 | 9279 18409 | 466396 18409 | 466398 18103 | 466395 18018 | 466397 17432 | 466401 17432 | 466402 16760 | 35897 16760 | 35898 16758 | 35888 16754 | 434631 16736 | 434630 16438 | 427355 16436 | 427290 16434 | 428262 16429 | 426997 16414 | 425737 16301 | 35890 15980 | 427175 15625 | 466393 14609 | 35893 14609 | 427291 14608 | 426060 14607 | 35891 14607 | 427135 14603 | 421114 14489 | 36704 14489 | 427454 14432 | 428737 14429 | 421113 14226 | 35889 (50 rows) CREATE INDEX IF NOT EXISTS "SDS_N50" ON "SDS" USING btree ("CD_ID"); EXPLAIN (ANALYZE,BUFFERS,TIMING) select count(*) from "SDS" where "CD_ID"=5923; QUERY PLAN --------------------------------------------------------------------------------------------------------------------------- ------------------ Aggregate (cost=286314.50..286314.51 rows=1 width=8) (actual time=177.937..177.937 rows=1 loops=1) Buffers: shared hit=1919 read=6935 -> Index Only Scan using "SDS_N50" on "SDS" (cost=0.43..284576.31 rows=695275 width=0) (actual time=0.182..135.756 row s=717971 loops=1) Index Cond: ("CD_ID" = 5923) Heap Fetches: 85645 Buffers: shared hit=1919 read=6935 Planning time: 0.276 ms Execution time: 177.965 ms (8 rows) DROP INDEX IF EXISTS "SDS_N50"; EXPLAIN (ANALYZE,BUFFERS,TIMING) select count(*) from "SDS" where "CD_ID"=5923; QUERY PLAN ------------------------------------------------------------------------------------------------------------------------- Aggregate (cost=661161.50..661161.51 rows=1 width=8) (actual time=3281.768..3281.768 rows=1 loops=1) Buffers: shared hit=256273 read=251476 -> Seq Scan on "SDS" (cost=0.00..659423.31 rows=695275 width=0) (actual time=894.265..3233.365 rows=717971 loops=1) Filter: ("CD_ID" = 5923) Rows Removed by Filter: 11416000 Buffers: shared hit=256273 read=251476 Planning time: 0.179 ms Execution time: 3281.790 ms (8 rows) SELECT version(); version --------------------------------------------------------------------------------------------------------------- PostgreSQL 9.6.19 on x86_64-redhat-linux-gnu, compiled by gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-37), 64-bit (1 row) select count(*) from "SDS"; count ---------- 12133996 (1 row)