// troubleshooting guide

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.

Covers SQL Server PostgreSQL MySQL SQLite Oracle

The symptom

Fast yesterday, slow today, same SQL. The query is not the variable - its environment is. There are only a handful of things that change under a running system, and each leaves evidence.

Diagnose it

First: is it slow, or is it waiting?

Catch the query while it runs. On SQL Server, sys.dm_exec_requests показывает blocking_session_id and wait type; on PostgreSQL, pg_stat_activity показывает wait_event_type = 'Lock'; on MySQL, sys.innodb_lock_waits; on Oracle, v$session.blocking_session. If it is waiting behind another session, this is a blocking problem and the blocking guide is the one to read.

The plan changed

The most common cause on the big engines: the optimizer picked a different plan. SQL Server's Query Store shows it directly - the Regressed Queries report in SSMS, or sys.query_store_plan joined to sys.query_store_runtime_stats, shows the same query_id switching plans, and sp_query_store_force_plan pins the good one back while you investigate. On PostgreSQL and MySQL, compare today's EXPLAIN ANALYZE with one from before the change, if you saved one (PostgreSQL's auto_explain module can log the plans of slow statements automatically). Read each in the query plan viewer. A plan flip with identical SQL and parameters points at statistics. On Oracle, DBMS_XPLAN.DISPLAY_CURSOR per child cursor, or DBMS_XPLAN.DISPLAY_AWR with the Diagnostics Pack, shows the old and new plan_hash_value.

The statistics went stale

A bulk load, a big delete, or a migration changed the data shape; the next stats refresh (or the first query after it, in some engines) recompiles and picks a different plan. The tell is in the plan: estimated rows that no longer match actual rows on the same node. Refresh deliberately and compare:

UPDATE STATISTICS dbo.Orders WITH FULLSCAN;   -- SQL Server
ANALYZE orders;                                -- PostgreSQL / SQLite
ANALYZE TABLE orders;                          -- MySQL
EXEC DBMS_STATS.GATHER_TABLE_STATS(USER, 'ORDERS');

The data crossed a threshold

A table grew past the point where the old plan was cheapest - a nested loop over 500 rows is fine, over 5 million it is not. Nothing regressed; the query outgrew its plan. The fix is the index the new plan needs, not a complaint about the optimizer.

Something else in the system changed

An index was dropped by a migration, autovacuum fell behind on Postgres (n_dead_tup ballooning in pg_stat_user_tables), a config change shrank a cache, or the disk filled up. On SQLite, which has no plan cache to regress, growth past a few hundred thousand rows plus a missing index is the overwhelming favorite - check EXPLAIN QUERY PLAN for a SCAN that used to be cheap when the table was small.

Fix it

Work in this order: prove it is not waiting (step 1), compare the current plan with the last known-good plan, refresh the statistics and re-measure, then fix the access path if the plan is now honest and still slow. If you cannot reproduce it, leave instrumentation in place - slow query log, Query Store, pg_stat_statements - because the next occurrence will leave the evidence the first one did not.

Resist restarting the server as a fix. A restart clears the plan cache and the buffer pool, which can mask a statistics problem for a day and resets the evidence you need.

The checklist

Work through it in order. Each step gives you the evidence for the next one.

  1. Catch the query running and check whether it is working or waiting on locks.
  2. Compare the current execution plan with the last known-good one.
  3. If the plan flipped with the same inputs, refresh statistics and re-measure.
  4. Check table row counts against last week; growth past a threshold changes the cheapest plan.
  5. Verify no migration dropped or changed an index the query uses.
  6. On PostgreSQL, check n_dead_tup and last_autovacuum in pg_stat_user_tables.
  7. Leave the slow log / Query Store / pg_stat_statements on so the next occurrence is captured.

Questions that come up

Nothing changed in the database - how can the plan change?

Statistics age on their own schedule, the data grows with normal use, and autovacuum or auto-update-stats runs in the background. A plan is a function of the data's shape as the optimizer last measured it, so 'nothing changed' almost always means 'the measurement changed'.

How do I pin a good plan while I investigate?

SQL Server: sp_query_store_force_plan, or a Query Store hint (SQL Server 2022+). Oracle: a SQL Plan Baseline loaded from the cursor cache with DBMS_SPM. PostgreSQL has no built-in plan pinning; the pg_hint_plan extension adds hints, otherwise fix the statistics or add the index that makes the good plan the obvious choice. MySQL has no plan baselines either, only optimizer hints written into the statement itself. In every case the pinned plan is a tourniquet, not a treatment.

Could an application deploy make the query slow without touching the SQL?

Yes: a new ORM version may change parameter types (an implicit conversion that blocks an index), wrap the query differently, or fetch more columns. Capture the exact SQL the app sends now - from the engine's statement history - and compare it with what you profiled before.

The slowdown follows a daily pattern - afternoon only. Still a plan change?

Usually not. A time-of-day pattern points at contention: backups, batch jobs, traffic peaks saturating CPU or I/O, or lock queues behind a scheduled writer. See the high CPU and blocking guides; the sudden-slowdown question is for when it never recovers.

Where do I compare the old and new plans?

Save both. The free query plan viewer at /tools/query-plan-viewer renders one plan at a time, so open each in its own tab and compare the expensive operators. The SQLly desktop app keeps a history of plans per query tab and diffs any two of them operator by operator.

// fix it where the query lives

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.