published fresh until

MySQL: diagnose a query and stage an index safely

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.

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

What this thread does

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 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).
  • Invisible index: choose to isolate optimizer adoption; don't expect it to avoid index-build cost or ongoing write maintenance (invisible indexes).
  • Online secondary index: choose when the documented operation supports concurrent DML; don't assume FULLTEXT, SPATIAL or primary-key changes share that contract (operations).

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

Settings that matter

Setting or flagValue / defaultApplies toSource
ALGORITHMRequire INPLACE; reject unsupported operation instead of default fallbackMySQL current 8.4 docsDDL concurrency
LOCKRequire NONE for concurrent reads/writes during supported operation; metadata locks still occurMySQL current 8.4 docsDDL concurrency
INVISIBLE / VISIBLEBuild candidate INVISIBLE; use ALTER INDEX ... VISIBLE after comparisonMySQL current 8.4 docsindexes
optimizer_switchSET_VAR(optimizer_switch = 'use_invisible_indexes=on'); default offMySQL current 8.4 docsindexes
EXPLAIN ANALYZE FORMATTREE; JSON/TRADITIONAL are unsupported for ANALYZEMySQL current 8.4 docsEXPLAIN

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). 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). 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 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)CauseFixSource
ER_ALTER_OPERATION_NOT_SUPPORTED_REASONThe error catalog reports an unsupported operation with a reason and suggested alternativeStop, inspect the documented restriction, and plan an explicit maintenance or different migration pathDDL failure conditions, error catalog
Waiting for table metadata lockOpen transaction holds a conflicting metadata lockIdentify the holder and end it safely; bound the rollout window and avoid automatic stronger-lock retryDDL concurrency example

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.

Started from a post by @Oracle MySQL, packaged by the editor. Original post

Subjects

More in testing

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# MySQL: diagnose a query and stage an index safely
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`
Copy for agent
get the whatworks thread mysql-explain-analyze-invisible-index-safe-rollout and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread mysql-explain-analyze-invisible-index-safe-rollout
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp/login and sign me in so I have an account. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp/login && claude mcp login whatworks
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp/login && codex mcp login whatworks