MySQL Index Strategies and Verification

Composite indexes, invisible indexes, descending indexes, generated-column indexing, and EXPLAIN workflows in MySQL.

View
StandardDetailedCompact
Export
Copy the compact sheet, download it, or print it.
Download
`D` dense toggle · `C` copy all

MySQL index patterns

Common MySQL DDL and tuning workflows.

Create a composite index

Support a filter plus order-by pattern.

sqlANYmysqlcreate-index
sql
CREATE INDEX idx_orders_status_created_at ON orders (status, created_at DESC);
Notes

Descending indexes can help mixed sort-order workloads and order-sensitive queries in modern MySQL.

Index a generated column

Practical route for function-based searches.

sqlANYmysqlgenerated-columnfunctional-index
sql
ALTER TABLE users
  ADD COLUMN email_lc VARCHAR(255) GENERATED ALWAYS AS (LOWER(email)) STORED,
  ADD INDEX idx_users_email_lc (email_lc);
Notes

A generated column can make case-normalized lookups indexable in MySQL.

Create an invisible index

Test an index without making it visible to the optimizer by default.

sqlANYmysqlinvisible-index
sql
CREATE INDEX idx_orders_coupon_code ON orders (coupon_code) INVISIBLE;
Notes

Invisible indexes are useful for experiments and staged rollouts.

Toggle index visibility

Hide or re-enable an index without dropping it.

sqlANYmysqloptimizerindex
sql
ALTER TABLE orders ALTER INDEX idx_orders_coupon_code VISIBLE;
Notes

This lets you test optimizer behavior before committing to a drop.

Verify usage

Use EXPLAIN and related patterns before and after adding an index.

Explain a query

See whether the optimizer chooses your index.

sqlANYmysqlexplain
sql
EXPLAIN SELECT id, total_amount
FROM orders
WHERE status = 'paid'
ORDER BY created_at DESC
LIMIT 50;
Notes

Look at the chosen key, access method, estimated rows, and extra information.

Refresh table statistics

Help the optimizer make better choices after major data changes.

sqlANYmysqlanalyze-tablestatistics
sql
ANALYZE TABLE orders;
Notes

Stale statistics can cause unexpected plans even when a good index exists.

Test with FORCE INDEX

Diagnostic tool for checking alternate index paths.

sqlANYmysqlforce-index
sql
SELECT id
FROM orders FORCE INDEX (idx_orders_status_created_at)
WHERE status = 'paid'
ORDER BY created_at DESC
LIMIT 50;
Notes

Useful for testing, but avoid relying on it as the first fix for poor design.

Recommended next

No recommendations yet.