Who’s this for Basics How data is stored in disk How indexes speedup access to data Costs associated with indexes Disk Space Write operations Query planner Memory usage Types of Indexes Btree Hash BRIN GIN GiST & SP-GiST Conclusion Who’s this for This text is for developers that have an intuitive knowledge of what database indexes are, but don’t necessarily know how they work internaly, what are the tradeoffs associated with indexes, what are the types of indexes provided by postgres and how you can use some of its more advanced options to make them more optimized for your use case.
Here’s my quick guide to fixing slow queries:
EXPLAIN ANALYZE <query>Parallel Seq Scan, check theFiltervalue (which column are we checking) - you should likely try adding an index.CREATE INDEX ON <table>(<column>);EXPLAIN ANALYZE <query>again to check for improvementsNote that
EXPLAIN <query>will only do a rough analysis, whileEXPLAIN ANALYZE <query>will run your actual query, and analyse it.If you want to
ANALYSE EXPLAINanINSERTquery, do this:A simple
ROLLBACKwill help you here.Rewriting the query will typically not do much - it might, but the chance it relatively small vs slapping an index on it.