skip to content

In MySQL, before dropping a suspected-unused secondary index on a large table, how would you test the drop with an invisible index, and what does invisibility leave unchanged?

level: seniorimportance: nice to knowfreq 28%

answer

  1. hide it from the optimizer first
  2. ALTER TABLE ... ALTER INDEX
  3. still maintained, still unique
  4. use_invisible_indexes for one query

basics

~20 s

Run ALTER TABLE ... ALTER INDEX idx INVISIBLE, watch plans, the slow query log and Performance Schema, then drop it or make it VISIBLE again instantly. An invisible index is still maintained on writes and still enforces uniqueness.

solid answer

~50 s

Start from evidence: `sys.schema_unused_indexes` lists indexes with no recorded use, meaningful only after a representative uptime. Then hide the candidate with `ALTER TABLE orders ALTER INDEX idx INVISIBLE`, a fast in-place change, unlike dropping and later rebuilding the index on a large table. The optimizer stops considering it while `use_invisible_indexes` is `off`, the default. Watch for regressions: changed `EXPLAIN` plans, new entries in the slow query log, more work in Performance Schema statement data, and errors from queries whose index hints name the index. If something regresses, `ALTER INDEX idx VISIBLE` restores it at once; if nothing does over a full business cycle, drop it. Invisibility does not save write cost: the index is still updated on every change, a `UNIQUE` index still rejects duplicates, and a primary key, explicit or implicit, cannot be made invisible.

code

sql · 17 lines
sql
-- 1. Candidates (meaningful only after a representative uptime)
SELECT object_schema, object_name, index_name
FROM sys.schema_unused_indexes
WHERE object_schema = 'shop';

-- 2. Hide one index from the optimizer
ALTER TABLE orders ALTER INDEX idx_legacy_status INVISIBLE;

SELECT INDEX_NAME, IS_VISIBLE
FROM INFORMATION_SCHEMA.STATISTICS
WHERE TABLE_SCHEMA = 'shop' AND TABLE_NAME = 'orders';

-- 3a. Something regressed: restore it at once
ALTER TABLE orders ALTER INDEX idx_legacy_status VISIBLE;

-- 3b. Nothing regressed over a full cycle: drop it
-- ALTER TABLE orders DROP INDEX idx_legacy_status;

go deeper

for a junior

Recall that ALTER TABLE ... ALTER INDEX ... INVISIBLE hides an index from the optimizer and VISIBLE brings it back.

for a middle

Explain what still happens to an invisible index, maintenance and uniqueness, and how use_invisible_indexes with SET_VAR tests a single query.

for a senior

Run the full drop test: gather evidence over a representative period, hide one index, watch plans, the slow query log and hint errors, then restore or drop.

for a principal

Set the team's index-retirement policy: how long an index stays hidden, who signs off, and how unused-index evidence is collected so rare workloads are not broken.

## Why not just drop it Every secondary index costs writes, buffer pool memory and disk, so an unused one is worth removing. The risk is that "unused" is a guess: a monthly report, a batch job or an admin screen may depend on it. Dropping is easy; **re-creating** an index on a large table means building it again from every row. MySQL's **invisible indexes** separate the two steps: first hide the index from the optimizer, then drop it once nothing has missed it. ## The procedure 1. **Find candidates.** `sys.schema_unused_indexes` lists indexes with no recorded events. The manual warns it is useful only after the server has run long enough for its workload to be representative, so a restart last night makes the list worthless. 2. **Hide the index.** `ALTER TABLE orders ALTER INDEX idx_legacy_status INVISIBLE;` Visibility changes are fast, in-place operations, unlike rebuilding an index. 3. **Confirm the state.** `INFORMATION_SCHEMA.STATISTICS` shows `IS_VISIBLE = NO`, and `SHOW INDEX` shows it in the `Visible` column. 4. **Watch for regressions** over at least one full business cycle, including month-end jobs. The manual names the signals: - queries with index hints that name the invisible index start failing with errors; - Performance Schema data shows more work for affected statements; - `EXPLAIN` plans change, often to `type: ALL` or a worse index; - statements appear in the slow query log that were not there before. 5. **Decide.** Either `ALTER TABLE orders ALTER INDEX idx_legacy_status VISIBLE;` to restore it at once, or `ALTER TABLE orders DROP INDEX idx_legacy_status;` to remove it for good. ## Testing one query against the hidden index The `use_invisible_indexes` flag of `optimizer_switch` decides whether the optimizer considers invisible indexes. It is **`off` by default**. Turned on, invisible indexes stay invisible but are considered for plans. With the `SET_VAR` optimizer hint you can turn it on for a single statement: ```sql EXPLAIN SELECT /*+ SET_VAR(optimizer_switch = 'use_invisible_indexes=on') */ id FROM orders WHERE status = 'pending'; ``` Comparing this plan with the normal one shows exactly what one query loses without the index, without making it visible to everyone. ## What invisibility does not change | Aspect | Visible index | Invisible index | |---|---|---| | Optimizer considers it | yes | no, unless `use_invisible_indexes=on` | | Updated on `INSERT`, `UPDATE`, `DELETE` | yes | **yes** | | `UNIQUE` still rejects duplicates | yes | **yes** | | Disk and buffer pool space | used | **still used** | | Index hints naming it | work | **raise errors** | - **Write cost stays.** The manual states that visibility does not affect index maintenance. Hiding an index proves whether reads need it; it does not show the write-throughput gain you will get by dropping it. - **Uniqueness stays.** A hidden `UNIQUE` index still enforces its constraint, so "no query uses it" is not a reason to drop it if it guards a business key. - **Hints break.** A query with `FORCE INDEX` or `USE INDEX` naming it fails rather than quietly choosing another plan. That is a useful signal, but it is also an outage for that query, so search the code for hints first. ## Indexes you cannot hide - The **primary key** cannot be made invisible. - Neither can an **implicit primary key**: on a table with no explicit primary key, the first `UNIQUE` index on `NOT NULL` columns acts as one, and `ALTER INDEX ... INVISIBLE` on it fails with `ERROR 3522 (HY000): A primary key index cannot be invisible.` Once an explicit primary key is added, that unique index can be hidden. ## Judgement calls - Hide **one index at a time** per table, so a regression has one obvious cause. - Keep the hidden period long enough to cover rare workloads; a week misses a quarterly report. - Indexes that look redundant because another index starts with the same columns are better candidates than "never used" ones on a recently restarted server. - Hide the index during normal traffic, not a quiet period, so the regressions you are looking for have a chance to happen. - Before hiding, take the top statements for that table from Performance Schema's statement digests and save their `EXPLAIN` output, so a changed plan is easy to spot afterwards. - Write down which index was hidden, when, and what was watched; the drop then has an audit trail if a forgotten job fails months later.

  • In MySQL, the invisible index you are testing is UNIQUE. Does hiding it change what INSERTs are accepted?
    No. Visibility only affects the optimizer; the index is still maintained and still rejects duplicate values. If no query reads it, dropping it would still remove a constraint, so first decide whether the uniqueness rule is needed and, if so, keep the index or replace it with another that enforces the same rule.
  • In MySQL, why can making an index invisible break a query outright instead of just slowing it down?
    A query whose index hint names the invisible index raises an error rather than falling back to another plan. Search application code and stored routines for `FORCE INDEX`, `USE INDEX` and `IGNORE INDEX` naming the index before hiding it, and remove or change those hints first.

Closing a road to traffic while the crews keep resurfacing it: drivers show you whether anyone needed the road, but the upkeep bill does not fall until you actually tear it up.

  • An invisible index stops costing anything on writes
  • An invisible UNIQUE index no longer rejects duplicates
  • The primary key can be hidden to test the clustered index
  • sys.schema_unused_indexes is reliable right after a restart
  • Hiding an index forces a full rebuild of the table