EXPLAIN ANALYZE and Index Optimization
Diagnose slow queries with EXPLAIN ANALYZE and create targeted indexes to dramatically improve PostgreSQL query performance.
Prerequisites
- -PostgreSQL database with tables and data
- -psql or database client
Steps
Analyze a query execution plan
Run EXPLAIN ANALYZE to see the actual execution plan with real timings.
BUFFERS shows I/O statistics. Look for Seq Scan on large tables as the primary indicator of missing indexes.
Find missing indexes with pg_stat_user_tables
Identify tables with high sequential scan counts that could benefit from indexes.
Create an index for a common query pattern
Add a composite index to support filtered queries efficiently.
CONCURRENTLY builds the index without locking the table for writes. Always use it in production.
CREATE INDEX CONCURRENTLY cannot run inside a transaction block.
Check index usage statistics
Verify that your indexes are actually being used by queries.
Find unused indexes wasting space
Identify indexes that consume disk space but are never used by the query planner.
Full Script
FAQ
Discussion
Loading comments...