Secondary Indexes — Complete Guide
Secondary Indexes — Complete Guide: free step-by-step lesson with examples, common mistakes, and interview tips — part of MySQL Tutorial on Toolliyo Academy.
On this page
MySQL Tutorial · Lesson 62 of 100
Secondary Indexes
Basics ✓ → Advanced
Advanced · 2 — Production · ~10 min · MySQL — Indexing & Performance
What is this?
Secondary indexes are non-primary B-tree indexes on columns like email or customer_id. Leaves store indexed column + primary key for row lookup.
Why should you care?
Login by email needs idx_customers_email — without it MySQL scans entire customers table every auth.
See it live — copy this example
Run in MySQL Workbench or the mysql CLI.
CREATE INDEX idx_customers_email ON customers(email);
EXPLAIN SELECT customer_id, full_name
FROM customers
WHERE email = 'priya@dataflow.in';
What happened?
- Index on email lets optimizer seek matching entries, then fetch rows by customer_id PK.
- type=ref in EXPLAIN with key idx_customers_email shows index use.
Practice next
- ADD email column if missing; populate sample rows.
- EXPLAIN before index — type ALL.
- CREATE INDEX; EXPLAIN again.
- DROP INDEX and measure slow query log entry.
- Unique index on email prevents duplicate accounts.
Remember
Secondary indexes speed selective WHERE/JOIN. Each index costs write overhead. EXPLAIN confirms index pick.
DataFlow login path
Auth service queries by email 50k times/min — idx_customers_email required.
Outcome: Login P99 stays under 20ms.
Interview prep for this lesson
Practice these questions aloud after reading—each links to a full structured answer.
Sign in to ask a question or upvote helpful answers.
No questions yet — be the first to ask!