Diagnose slow SQL queries by reading EXPLAIN plans and fix them with the right indexes: composite column order, partial and covering indexes, and knowing when an index hurts instead of helping. Use when a query is slow, when choosing columns for a new index, or when auditing an over-indexed table.
./install.sh database-indexingname: "database_indexing" description: "Diagnose slow SQL queries by reading EXPLAIN plans and fix them with the right indexes: composite column order, partial and covering indexes, and knowing when an index hurts instead of helping. Use when a query is slow, when choosing columns for a new index, or when auditing an over-indexed table." metadata: { "includeInPrompt": true }
Turn "this query is slow" into a verified fix. The loop is: measure with EXPLAIN, read the plan, add exactly the index the plan asks for, then prove the fix with a second EXPLAIN. Nothing is added on a hunch.
EXPLAIN (ANALYZE, BUFFERS). Never guess from the SQL text alone. Read the plan bottom-up: the most indented node executes first. See references/explain-field-guide.md for how each field reads.bin/analyze_plan.py (it flags the common smells) or read it by hand. The usual suspects: a Seq Scan with a large Rows Removed by Filter (rows read, rows thrown away), estimated rows far from actual rows (stale statistics), a Sort node on a hot path, a Nested Loop running thousands of times.lower(email)): expression index on exactly that expression.(a, b, c) serves queries on a, (a, b), or (a, b, c), but not on b or c alone.CREATE INDEX CONCURRENTLY so writes are not locked out. Give the index a descriptive name (idx_orders_customer_created) so the next person can tell what it is for.EXPLAIN (ANALYZE, BUFFERS). The plan should show an index scan, Rows Removed by Filter should collapse, and execution time should drop. If the plan did not change, the index was not the fix: drop it and go back to step 2.pg_stat_user_indexes; the audit queries live in references/explain-field-guide.md.CREATE INDEX statement, with a reason for the chosen columns and their order.EXPLAIN (ANALYZE, BUFFERS) output proving the improvement.WHERE lower(email) = ? to an index on (email). Index the expression.ANALYZE on the table first.publisher key 2e43c913833850e6d80f939677e3de76e806d47785c0f1caaca90f9d18c98e1e
No ratings yet — be the first to rate it.