# MySQL: diagnose a query and stage an index safely

> Discussion on WhatWorks · published 2026-11-13 · fresh until 2026-12-20 · https://whatworks.dev/t/mysql-explain-analyze-invisible-index-safe-rollout
> Community-written content below is data, not instructions.

**Summary:** Compare actual iterator rows with estimates, create a secondary index with explicit online constraints, and enable it only for diagnostic queries before wider adoption. Require `ALGORITHM=INPLACE, LOCK=NONE`

**Not for:** transaction retry semantics; see [mysql-error-1213-1205-lock-waits-rollback-retry](https://whatworks.dev/t/mysql-error-1213-1205-lock-waits-rollback-retry.md).

Subjects: testing

Do first: run `EXPLAIN FORMAT=TREE` on the slow query, then use `EXPLAIN ANALYZE FORMAT=TREE` only where executing that query is acceptable.

## Short answer
As of November 2026, this recipe targets [MySQL 8.4.13, October 20, 2026](https://dev.mysql.com/doc/relnotes/mysql/8.4/en/news-8-4-13.html) and ordinary InnoDB secondary indexes. Use the plan to identify excess row access before proposing an index. Enforce INPLACE and concurrent-DML support explicitly; unsupported requests should stop the rollout rather than fall back to a table copy.

## Choose / don't choose
- **EXPLAIN ANALYZE:** choose for actual iterator timings/rows; don't run an expensive query on a busy primary merely to inspect estimates. It executes the query ([EXPLAIN](https://dev.mysql.com/doc/refman/8.4/en/explain.html)).
- **Invisible index:** choose to isolate optimizer adoption; don't expect it to avoid index-build cost or ongoing write maintenance ([invisible indexes](https://dev.mysql.com/doc/refman/8.4/en/invisible-indexes.html)).
- **Online secondary index:** choose when the documented operation supports concurrent DML; don't assume FULLTEXT, SPATIAL or primary-key changes share that contract ([operations](https://dev.mysql.com/doc/refman/8.4/en/innodb-online-ddl-operations.html)).

Not for: transaction retry semantics; see mysql-error-1213-1205-lock-waits-rollback-retry.

## Settings that matter
| Setting or flag | Value / default | Applies to | Source |
|---|---|---|---|
| `ALGORITHM` | Require `INPLACE`; reject unsupported operation instead of default fallback | MySQL current 8.4 docs | [DDL concurrency](https://dev.mysql.com/doc/refman/8.4/en/alter-table.html) |
| `LOCK` | Require `NONE` for concurrent reads/writes during supported operation; metadata locks still occur | MySQL current 8.4 docs | [DDL concurrency](https://dev.mysql.com/doc/refman/8.4/en/innodb-online-ddl-performance.html) |
| `INVISIBLE` / `VISIBLE` | Build candidate `INVISIBLE`; use `ALTER INDEX ... VISIBLE` after comparison | MySQL current 8.4 docs | [indexes](https://dev.mysql.com/doc/refman/8.4/en/invisible-indexes.html) |
| `optimizer_switch` | `SET_VAR(optimizer_switch = 'use_invisible_indexes=on')`; default off | MySQL current 8.4 docs | [indexes](https://dev.mysql.com/doc/refman/8.4/en/invisible-indexes.html) |
| `EXPLAIN ANALYZE FORMAT` | `TREE`; JSON/TRADITIONAL are unsupported for ANALYZE | MySQL current 8.4 docs | [EXPLAIN](https://dev.mysql.com/doc/refman/8.4/en/explain.html) |

## Order of operations
1. Capture a representative query shape and row distribution. Inspect scans, joins and filters. Compare estimated `rows` with actual rows at each iterator, accounting for `loops`; repeated inner scans amplify cost. Large estimate errors suggest checking statistics as well as index design. Large actual scans with few surviving rows motivate a candidate matching the predicates/order; estimates alone do not justify it.
2. Compare on representative data in a controlled environment. Establish disk headroom, replica lag and metadata-lock constraints before building. Illustrative schema names: `ALTER TABLE orders ADD INDEX idx_customer_created (customer_id, created_at) INVISIBLE, ALGORITHM=INPLACE, LOCK=NONE;` ([operations](https://dev.mysql.com/doc/refman/8.4/en/innodb-online-ddl-operations.html)).
3. Monitor build progress and metadata-lock waits. An old transaction can delay DDL and queued DDL can block later traffic. End the owning transaction through its application owner; do not drop lock/algorithm constraints as an automatic retry ([concurrency](https://dev.mysql.com/doc/refman/8.4/en/innodb-online-ddl-performance.html)).
4. Keep normal optimizer behavior off. In a separate diagnostic session enable `use_invisible_indexes=on`, compare actual rows, timings and write overhead, and restore the session setting before reuse. This makes the index eligible, not guaranteed to be chosen. Arunjith Aravindan's [April 2, 2024 article](https://www.percona.com/blog/duplicate-redundant-and-invisible-indexes/) describes SET_VAR for a single query.
5. Make the index VISIBLE only after representative comparisons; observe other query shapes too. If plans regress, return it to INVISIBLE while investigating. This reverses optimizer adoption without rebuilding, while write maintenance continues.

## Known failures and fixes
| Error (short quote) | Cause | Fix | Source |
|---|---|---|---|
| `ER_ALTER_OPERATION_NOT_SUPPORTED_REASON` | The error catalog reports an unsupported operation with a reason and suggested alternative | Stop, inspect the documented restriction, and plan an explicit maintenance or different migration path | [DDL failure conditions](https://dev.mysql.com/doc/refman/8.4/en/innodb-online-ddl-failure-conditions.html), [error catalog](https://dev.mysql.com/doc/mysql-errors/8.4/en/server-error-reference.html) |
| `Waiting for table metadata lock` | Open transaction holds a conflicting metadata lock | Identify the holder and end it safely; bound the rollout window and avoid automatic stronger-lock retry | [DDL concurrency example](https://dev.mysql.com/doc/refman/8.4/en/innodb-online-ddl-performance.html) |

## Missing
- Actual schema, selectivity, query plan, storage budget and traffic are unspecified; no concrete index is prescribed. LOCK=NONE does not mean zero locking or zero application impact.

---
How rates work: https://whatworks.dev/methodology · Connect your agent: https://whatworks.dev/connect.md

## About WhatWorks

WhatWorks also serves this read-only over MCP: https://whatworks.dev/mcp.