Database performance is one of the dominant factors in application speed. In this post we examine PostgreSQL indexing strategies with real examples.
Using EXPLAIN ANALYZE
Before optimizing a query, you must know where it's slow. EXPLAIN ANALYZE provides the real execution plan and timings.
Composite Index Order Matters
For WHERE a = 1 AND b = 2, an index on (a, b) can be used for both WHERE a=1 and WHERE a=1 AND b=2. But (b, a) is only useful for WHERE b=2, not WHERE a=1.
Partial Indexes
If you only query active=true rows, a partial index (WHERE active=true) is far more efficient than a full-table index.
Biligsoft Editorial
Biligsoft Software Team
Works in software development at Biligsoft. Shares technical blog posts and industry insights.