S
Supanote
Sign in Sign up

Zero-Risk MySQL Index Cleanup (Invisible Index & EXPLAIN ANALYZE)

plain (sql) 1 day ago · 47 lines · 4 views
Verified Blueprint MySQL 8.4 LTS

Drop unused or redundant MySQL indexes safely without downtime using sys schema audits, ALTER INDEX INVISIBLE, and EXPLAIN ANALYZE.

  • ✓ Detects redundant prefix indexes and zero-read write-amplifying indexes via MySQL sys views
  • ✓ Hides candidate indexes metadata-instantly (ALTER INDEX INVISIBLE) so rollback takes 1 millisecond
  • ✓ Validates real iterator execution times and row estimates with EXPLAIN ANALYZE before online DROP
Quick Fetch
1-- Zero-Risk MySQL 8.4 / 9.x Unused Index Cleanup Workflow
2-- Avoids multi-hour index rebuilds if a seemingly unused index is needed by a monthly job
3
4-- Step 1: List Unused & Redundant Indexes (Ensure server uptime >= 7 days)
5SELECT
6 object_schema AS db_name,
7 object_name AS table_name,
8 index_name,
9 format('ALTER TABLE %I.%I ALTER INDEX %I INVISIBLE;', object_schema, object_name, index_name) AS step1_hide_sql
10FROM sys.schema_unused_indexes
11WHERE object_schema NOT IN ('mysql', 'sys', 'performance_schema');
13SELECT
14 table_schema,
15 table_name,
16 redundant_index_name,
17 redundant_index_columns,
18 dominant_index_name,
19 dominant_index_columns,
20 sql_drop_index
21FROM sys.schema_redundant_indexes;
23-- Step 2: Hide Candidate Index Instantly (Metadata-Only Operation, Zero Table Lock)
24ALTER TABLE orders ALTER INDEX idx_orders_legacy_status INVISIBLE;
26-- Step 3: Compare Execution Plans With vs Without the Invisible Index in Current Session
27EXPLAIN ANALYZE
28SELECT id, customer_id, total_cents
29FROM orders
30WHERE status = 'processing' AND created_at >= NOW() - INTERVAL 7 DAY
31ORDER BY created_at DESC LIMIT 100;
33-- Toggle visibility ON solely for this session to verify whether latency changes:
34SET SESSION optimizer_switch = 'use_invisible_indexes=on';
35EXPLAIN ANALYZE
36SELECT id, customer_id, total_cents
37FROM orders
38WHERE status = 'processing' AND created_at >= NOW() - INTERVAL 7 DAY
39ORDER BY created_at DESC LIMIT 100;
40SET SESSION optimizer_switch = 'use_invisible_indexes=off';
42-- Step 4A: Emergency Instant Rollback (If p99 latency regressed during soak window)
43-- ALTER TABLE orders ALTER INDEX idx_orders_legacy_status VISIBLE;
45-- Step 4B: Permanent Lock-Free Removal After Safe Soak Window
46ALTER TABLE orders DROP INDEX idx_orders_legacy_status, ALGORITHM=INPLACE, LOCK=NONE;

No replies yet

Every reply is a note. Start a discussion, ask a question, or attach a code snippet.

Related MySQL 8.4 LTS & Database, Cache & SQL Blueprints

Production-ready configurations and diagnostics from the verified Supanote library.

All Database, Cache & SQL →
Interactive Developer Workbench: .env, Log & API Key Secret Scanner

Detect and redact leaked AWS keys, JWTs, database URIs, private keys, and Bearer tokens before sharing.

Open Secret Scanner →

Share Note

Open
Download SVG
Social Card Preview
Open on mobile
Point your phone camera to open this note directly

Report this note

Notification