|
|
|
|
|
by inferiorhuman
1487 days ago
|
|
So that's not the greatest quote but the article shows postgres having to fall back on a sequential scan for a naive COUNT(*). The author goes on to mention that as of 9.2 Postgres can do an index scan (in the context of SELECT DISTINCT): But now we come to a quirk, SELECT COUNT(DISTINCT n) FROM items will not use
the index even though SELECT DISTINCT n does. As many blog posts mention (“one
weird trick to make postgres 50x faster!”) you can guide the planner by rewriting
count distinct as the count of a subquery
To me it seems like you can trick the query planner into doing an index scan sometimes, but that it's a bit brittle. Has this improved much since? |
|
https://wiki.postgresql.org/wiki/Index-only_scans#Is_.22coun...
It says that index only scans can be used with predicates and less often without predicates, though in my experience I've seen the index often used even without predicates.