SQL performance troubleshooting
A slow query is fixed with evidence: the waits, the plan and the statement history your database already records. Each guide below works through that evidence for one engine or one symptom, with the commands to run and an ordered checklist at the end.
By database engine
An engine guide covers the whole loop on that engine: rank the statements that cost the
most, read the execution plan, and fix what it shows. It uses the engine's own DMVs,
pg_stat views, EXPLAIN variants and system views.
SQL Server
- SQL Server query performance troubleshooting A method for slow SQL Server queries: read the waits, find the statements that cost the most, get the actual plan, and fix what it shows - missing indexes, stale statistics or a plan that only works for some parameter values.
Symptom guides with SQL Server commands
PostgreSQL
- PostgreSQL query performance troubleshooting A method for slow PostgreSQL queries: rank statements with pg_stat_statements, read EXPLAIN (ANALYZE, BUFFERS), then fix the seq scans, bad estimates and bloat it shows.
Symptom guides with PostgreSQL commands
MySQL
- MySQL query performance troubleshooting A method for slow MySQL queries: capture them with the slow log, rank them with performance_schema, read EXPLAIN ANALYZE, and fix what it shows.
Symptom guides with MySQL commands
SQLite
- SQLite query performance troubleshooting SQLite is fast when the plan is a SEARCH and slow when it is a SCAN. How to read EXPLAIN QUERY PLAN, build the index the query needs, and keep the planner informed.
Symptom guides with SQLite commands
Oracle
- Oracle query performance troubleshooting A method for slow Oracle queries: see what sessions wait on, find the expensive SQL in V$SQLSTATS or ASH, get the actual plan from DBMS_XPLAN, then fix the access path or the statistics.
Symptom guides with Oracle commands
By symptom
Start from what you see: one query that is always slow, a query that used to be fast, CPU pegged, sessions queueing behind locks, an index the planner ignores, or reads going to disk because the cache is too small.
-
Slow query
One query runs long, every time. Get the plan, find the expensive step, fix the access path.
- Diagnosing a slow SQL query One query is slow, every time it runs. The same five steps work on SQL Server, PostgreSQL, MySQL, SQLite and Oracle: measure, get the real plan, find the expensive step, check the estimates, fix the access path.
Also covered in
- SQL Server query performance troubleshooting
- PostgreSQL query performance troubleshooting
- MySQL query performance troubleshooting
- SQLite query performance troubleshooting
- Oracle query performance troubleshooting
- Finding what burns CPU on a database server
- Why the optimizer ignores your index
- Memory pressure
-
Sudden slowdown
The same query used to be fast. Something changed: statistics, the plan, the data or the locks.
- When a fast query turns slow The query has not changed but it now takes seconds. What actually changed: statistics, the chosen plan, the data size, the indexes, or what it waits behind. How to find out which, per engine.
Also covered in
-
High CPU
The server burns CPU. Find which statements account for it before you resize anything.
- Finding what burns CPU on a database server CPU pegged on the database box. CPU cost follows logical reads and row-by-row work, so the statement history names the culprits: how to rank them on SQL Server, PostgreSQL, MySQL and Oracle, then fix the usual CPU shapes.
Also covered in
-
Blocking and deadlocks
Sessions queue up behind locks or kill each other. Shorten transactions and fix what they wait on.
- Blocking and deadlocks Sessions queue up or kill each other with deadlock errors. How to see the wait chain on SQL Server, PostgreSQL, MySQL and Oracle, read the deadlock graph, and fix the design that causes it.
Also covered in
-
Bad index usage
An index exists but is ignored, or is missing. Check what the optimizer chose and why.
- Why the optimizer ignores your index The index is there but the plan scans the table anyway. The usual reasons: wrong leading column, a function or implicit conversion on the predicate, a leading-wildcard LIKE, low selectivity, or statistics that make scanning look cheaper.
Also covered in
-
Memory pressure
Buffers keep missing and reads go to disk. Size the caches for the working set, not the database.
- Memory pressure The database is slow and disk is busy: the working set does not fit in the cache. How to measure the hit rate on each engine, size the main caches correctly, and spot memory leaks vs undersizing.
Also covered in
By what the plan shows
Start from the execution plan: one guide per thing a plan can tell you, with how to confirm it and what to change. The query plan viewer links each of its findings to the matching guide.
-
Reading the plan
Capture a plan with run-time numbers and learn what each kind of operator does.
- How to read an execution plan An execution plan is the list of steps the database took to answer a query. How to get one that includes what really happened, how to read it, and which numbers to look at first.
- Execution plan operators Every step in an execution plan is an operator: something that reads rows, joins them, sorts them or passes them on. The engines use different names for the same two dozen ideas. This page lists them by what they do.
-
Indexes and access paths
The plan reads far more rows than it returns, or goes back to the table for every row it finds.
- Full table scans in a query plan The plan reads every row of a table and keeps a handful. How to recognise the scan on each engine, measure how much it discards, add the index that turns it into a seek, and tell when the scan is the right plan.
- Missing index suggestions in the plan SQL Server writes the index it wanted into the plan. How to read the suggestion, find the same request in the missing-index DMVs, turn it into an index that fits the ones you already have, and what the suggestion does not consider.
- Key lookups and covering indexes An index found the rows, but the query needs columns the index does not have, so the plan fetches every row from the table one at a time. How to see that step on each engine, cover the query with the right index, and when the lookup is fine.
- Heap fetches in an index-only scan A PostgreSQL index-only scan that reports thousands of heap fetches is reading the table after all. Why the visibility map decides that, how to measure it, and how to keep the map current with VACUUM and autovacuum settings.
- Index merge and BitmapAnd plans The plan reads two single-column indexes and intersects or unites what they return. How MySQL index_merge and PostgreSQL BitmapAnd and BitmapOr show up, why an intersection usually means a composite index is missing, and why a union is usually fine.
- Non-sargable predicates The column is indexed, but the filter wraps it in a function, a calculation or a leading wildcard, so the index cannot be searched. How each engine's plan shows that, how to rewrite the filter as a range, and when to index the expression instead.
- Implicit conversions and type mismatches A comparison between two different types makes the engine convert one side. When it converts the column, the index on that column cannot be searched. How each engine shows the conversion in the plan and how to make the types match.
- Partition pruning in the plan A partitioned table is only fast when the plan can skip partitions. How PostgreSQL, MySQL and SQL Server show which partitions a query reads, the filters that prevent pruning, and how to write the query so the partition key does its job.
- Index spools and automatic indexes When a query needs an index that does not exist, SQL Server and SQLite can build a temporary one while the query runs and throw it away afterwards. How to recognise an Index Spool or an AUTOMATIC INDEX in a plan and replace it with a permanent index.
-
Row estimates and statistics
The optimizer planned for a different number of rows than it got.
- Estimated rows far from actual rows Every plan choice rests on a guess at how many rows each step returns. When the guess is far off, the join method, join order and memory are chosen for the wrong amount of data. How to find the first wrong estimate and what to do about each cause.
- Parameter sniffing and cached plans The same procedure is fast for one customer and slow for another, or fast until a restart and slow after. The plan was built for the first value it saw and reused for all the rest. How to prove it and what to change.
-
Sorts, memory and temp space
A sort, hash or temporary table is doing the work an index or more memory would avoid.
- Expensive sort operators ORDER BY, GROUP BY, DISTINCT and merge joins all need sorted rows. If no index supplies the order, the engine sorts at query time. How to spot the sort, judge its cost, and remove it with an index that matches the ORDER BY.
- Sorts and hashes that spill to disk Sorts, hash joins and hash aggregates work in memory until the memory they were given runs out, then continue in temporary files. How PostgreSQL, SQL Server and MySQL report a spill, what causes it, and how to give the operation enough memory or less to do.
- Using temporary in MySQL plans MySQL builds an internal temporary table when it cannot group, de-duplicate or order rows as it reads them. How to see it in EXPLAIN, tell whether the table went to disk, and remove it with an index.
-
Joins and subqueries
The join order or join method multiplies the work, or a subquery runs once per row.
- Nested loop, hash and merge joins A plan joins tables with nested loops, a hash table or a merge of sorted inputs. Each is the best choice for some row counts and a poor one for others. How to read them and what to fix when the wrong one was chosen or the right one has no index to use.
- Joins that multiply rows A join that produces far more rows than it was given is either missing its join condition or fanning out across a one-to-many relationship. How both look in a plan, why DISTINCT only hides them, and the rewrites that remove the extra rows.
- Correlated subqueries and NOT IN A subquery that refers to the outer query can be executed once for every outer row. How each engine shows that in a plan, how to index or rewrite it, and why NOT IN over a nullable column is both slow and wrong.
- Materialized CTEs PostgreSQL computes a materialized CTE in full and only then applies the filters of the outer query. How to recognise CTE Scan with rows removed by a filter, when PostgreSQL materializes, and how NOT MATERIALIZED lets the filter reach the index.
-
Parallelism, functions and compile time
The plan ran on fewer threads than it could, calls a function per row, or spends its time compiling.
- Parallel plans that do not run in parallel A parallel plan only helps when the work is really spread over several workers. How to spot a PostgreSQL plan that launched fewer workers than it planned, a SQL Server plan where one thread did nearly everything, and a plan that was not allowed to go parallel at all.
- Scalar functions that run once per row A scalar user-defined function in a SQL Server query runs once for every row, keeps its own reads out of the plan and the I/O statistics, and stops the query from running in parallel. How to find it, and how to inline it or replace it.
- JIT overhead and planning time Some PostgreSQL queries are slow before they read a single row. How to tell from EXPLAIN ANALYZE that JIT compilation or planning took longer than the work itself, and how to bring each one down.
Run these queries in SQLly
SQLly is a native SQL workbench for macOS, Windows and Linux. The query plan viewer and formatter these guides link to are built into its editor.