// troubleshooting guide

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.

Covers SQL Server PostgreSQL MySQL SQLite Oracle

The symptom

You built the index, the plan still scans the table. The optimizer is not being stubborn - an index is only usable when the predicate matches its shape and using it is actually cheaper. Each cause below leaves a specific fingerprint in the plan.

Diagnose it

The predicate does not match the leading column

Composite indexes work left to right. An index on (customer_id, status) serves predicates on customer_id o customer_id AND status, but a predicate on status alone cannot seek into it, and the plan shows a scan. The fix is an index led by status. Check this first; it is the most common cause. The partial exception is a skip scan, which probes the index once per distinct leading value: Oracle, MySQL 8.0.13+, SQLite (once ANALYZE has gathered statistics) and PostgreSQL 18 can do it when the leading column has very few distinct values. A skip scan is cheaper than a full scan but still slower than an index with the right leading column.

A function or cast sits on the column

WHERE DATE(created_at) = '2026-05-01'      -- function on the column: no seek
WHERE created_at >= '2026-05-01'
AND created_at < '2026-05-02'            -- same meaning: seeks

The same happens with type mismatches. On SQL Server, a varchar column compared with an nvarchar parameter (what most drivers send for strings by default) gets converted to nvarchar row by row; with a SQL_ collation that turns the seek into a scan, and the plan shows a CONVERT_IMPLICIT warning. On MySQL, a string column compared with a number (WHERE phone = 5551234) is converted per row and cannot use its index; the reverse, a numeric column compared with a string literal, is fine. Move the function to the other side of the comparison and send parameters in the column's type.

The LIKE starts with a wildcard

LIKE '%smith' cannot use a B-tree seek because the index is ordered left to right. LIKE 'smith%' seeks fine. Infix and suffix search need a full-text index, a trigram index (PostgreSQL pg_trgm with a GIN index serves LIKE '%smith%' directly), or for suffixes, an index on the reversed string. Two engine traps for prefix LIKE: PostgreSQL only uses a plain B-tree index for LIKE 'smith%' when the database collation is C, so otherwise create the index with text_pattern_ops; and SQLite's case-insensitive LIKE needs an index declared COLLATE NOCASE.

Scanning really is cheaper

If the predicate matches most of the table, seeking per row costs more than one sequential pass - the optimizer is right. Check the estimated rows of the scan against the table size. If the estimate is honest, the fix is returning fewer rows or a covering index that makes the scan narrower, not forcing the index.

The statistics are lying

Stale or missing statistics can make a selective predicate look non-selective. Compare estimated vs actual rows in the plan; if they diverge, refresh statistics and re-check before touching the index:

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

To see which choice the optimizer made and why, capture the plan and open it in the query plan viewer; la formateador helps when the query that carries the predicate arrived as one line.

Fix it

Fix in the order the causes are listed: correct the index's leading column to match the predicate, de-function the predicate, align types, then (and only then) refresh statistics and re-measure. When the predicate genuinely matches most of the table, the winning move is usually a covering index so the scan reads a narrow index instead of wide table rows:

CREATE INDEX idx_orders_status_cover
ON orders (status) INCLUDE (customer_id, total);  -- SQL Server, PostgreSQL 11+
CREATE INDEX idx_orders_status_cover
ON orders (status, customer_id, total);           -- MySQL, SQLite, Oracle

Finally, confirm the fix stuck: sys.dm_db_index_usage_stats (SQL Server), pg_stat_user_indexes.idx_scan (PostgreSQL), sys.schema_unused_indexes (MySQL) and DBA_INDEX_USAGE (Oracle 12.2+) show whether the index is being used in production, not just in your test. The SQL Server and MySQL counters start over when the server restarts, PostgreSQL's survive a clean restart but reset after a crash or pg_stat_reset(), y DBA_INDEX_USAGE is kept in the dictionary across restarts.

The checklist

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

  1. Compare the index's leading column with the WHERE clause: do they match?
  2. Search the predicate for functions and casts applied to the indexed column.
  3. Check for implicit type mismatches between the column and the parameter or literal.
  4. Rewrite leading-wildcard LIKEs as prefix matches, or plan a full-text or trigram index.
  5. Compare estimated vs actual rows in the plan; refresh statistics if they diverge.
  6. If selectivity is genuinely low, build a covering index instead of forcing the seek.
  7. Re-capture the plan and confirm the SCAN became a SEEK with fewer logical reads.

Questions that come up

Should I force the index with a hint?

Only as a temporary measure with an expiry note. Hints fix the plan, not the reason the optimizer avoided the index, and the forced plan outlives the data change that made the index wrong. Fix the predicate, the types or the statistics and the optimizer will usually find the index on its own.

The index is used in staging but not in production. Why?

Different data: production has more rows (the threshold where seek beats scan differs), different column distributions, or different statistics staleness. Compare estimated rows for the same predicate on both systems and refresh production statistics before concluding anything else.

Do INCLUDE columns make an index more likely to be used?

They make a used index more useful - the seek can satisfy the SELECT from the index alone instead of fetching table rows (a key lookup). They do not change whether the index is chosen for filtering; that is decided by the key columns and the predicate.

How do I know if an index is ever used at all?

Each engine tracks it: sys.dm_db_index_usage_stats on SQL Server (cleared on restart), pg_stat_user_indexes.idx_scan on PostgreSQL (plus last_idx_scan from PostgreSQL 16), sys.schema_unused_indexes on MySQL, and DBA_INDEX_USAGE on Oracle 12.2+, which is collected automatically but sampled by default, so a rarely used index can be missed; older Oracle releases need ALTER INDEX ... MONITORING USAGE and v$object_usage. Watch over a full business cycle - a monthly job's index looks unused until month-end. SQLite does not track index usage.

Can an unused-looking plan still be optimal?

Yes. A full scan over a small table, a covering scan over a large one with low selectivity, or a hash join fed by scans can all be the cheapest plan. Judge by logical reads and time, not by whether the word 'Index' appears in the plan - the query plan viewer at /tools/query-plan-viewer makes the per-operator costs easy to read.

// 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.