DEV Community

Cover image for Indexing every WHERE column is not PostgreSQL optimization
bertrand HARTWIG
bertrand HARTWIG

Posted on

Indexing every WHERE column is not PostgreSQL optimization

One PostgreSQL indexing mistake I see often:

“The query filters on A, B and C, so let’s create an index on A, B, C.”

That may work, but it may also be the wrong index.

For composite B-tree indexes, PostgreSQL cares about predicate type, column order, selectivity, table size, and the actual execution plan.

In this post, I explain why equality predicates usually belong before range predicates, why n_distinct from statistics matters, and why a theoretically good index is useless if the planner never uses it.

I also show how pgAssistant turns this into an automated index recommendation workflow using EXPLAIN ANALYZE and planner statistics.

Full write-up:
https://beh74.github.io/pgassistant-blog/post/query_advisor/

Top comments (0)