Mastering MySQL for Data AnalystsLesson 39 of 42
Lesson 39 of 42

Indexing in MySQL: InnoDB B-Tree, Composite, Covering & FULLTEXT Indexes

Overview & Learning Objectives

An index is to a database table what an index is to a textbook: it allows the MySQL engine to locate target records in $O(\log N)$ time without scanning every page from beginning to end. In MySQL InnoDB, tables are organized as Clustered Indexes keyed on the Primary Key. Secondary indexes contain pointers back to the primary key. Analysts must master composite multi-column indexing, the Leftmost Prefix Rule, and Covering Indexes.

  • Understand the architecture of InnoDB Clustered Index vs Secondary B-Tree Indexes.
  • Design composite multi-column indexes adhering to the Leftmost Prefix Rule.
  • Architect Covering Indexes to satisfy queries entirely from memory without table heap lookups.
  • Implement FULLTEXT indexes for natural language text search.

Industry Standard

Standard: Production database engineering at Uber and Amazon enforces that every high-frequency analytical query achieves a Covering Index scan (`Using index` in EXPLAIN).

Real-World Example: ScoopCraft 50-Million Order Query Optimization

ScoopCraft customer order history query slows down to 18 seconds as the table reaches 50 million rows; adding a composite index cuts latency to 4 milliseconds.

Index StrategyIndex DefinitionQuery Time (50M Rows)EXPLAIN Output
No IndexFull Table Scan18.4 secondstype: ALL, rows: 50,000,000
Single IndexKEY (customer_id)0.35 seconds (52x faster)type: ref, Using where
Covering IndexKEY (customer_id, order_date, total)0.004 seconds (4,600x faster)type: ref, Using index (Covering)

Syntax & Core SQL Implementation

-- Creating optimal B-Tree and Composite Indexes in MySQL
-- 1. Standard single-column index
CREATE INDEX idx_orders_customer ON orders(customer_id);

-- 2. Composite multi-column index (Follows Leftmost Prefix Rule: customer_id, then order_date)
CREATE INDEX idx_orders_cust_date ON orders(customer_id, order_date);

-- 3. Covering Index (Satisfies query completely from index tree)
CREATE INDEX idx_orders_covering ON orders(customer_id, order_date, order_total);

-- 4. FULLTEXT Index for natural language catalog search
CREATE FULLTEXT INDEX idx_ft_flavor ON menu_items(flavor_name, description);

-- Natural language fulltext search query
SELECT flavor_name, MATCH(flavor_name, description) AGAINST('chocolate almond' IN NATURAL LANGUAGE MODE) AS relevance
FROM menu_items
WHERE MATCH(flavor_name, description) AGAINST('chocolate almond' IN NATURAL LANGUAGE MODE);

Parameters & Arguments Breakdown

  • Clustered Index: In InnoDB, the table data itself is stored directly within the leaf pages of the Primary Key B-Tree.
  • Secondary Index: Leaf nodes store the indexed column values plus the Primary Key value of the row.
  • Leftmost Prefix Rule: A composite index on (A, B, C) can satisfy queries on (A), (A, B), and (A, B, C), but NOT on (B) or (C) alone.
  • Covering Index: An index that contains every single column requested by the SELECT, WHERE, and ORDER BY clauses.

Sample Walkthrough (Q&A)

Question: What does "Using index" in the Extra column of MySQL EXPLAIN indicate?

SQL Pattern: EXPLAIN SELECT ... Extra: Using index

Result: Covering Index achieved! The query was resolved completely inside the index tree without reading physical table data pages

Explanation: Covering indexes eliminate expensive random disk I/O, delivering maximum possible query throughput.

Common Mistakes & Limitations

  • Over-indexing: adding an index to every single column, which drastically slows down INSERT, UPDATE, and DELETE write speeds.
  • Violating the Leftmost Prefix Rule: creating index on `(store_id, order_date)` and trying to query `WHERE order_date = "2026-11-01"` without store_id (index is ignored!).

Keyboard Shortcuts (MySQL Workbench)

ActionWindowsMac
Show Table IndexesSHOW INDEX FROM table_name;SHOW INDEX FROM table_name;

Pro Tips & Modern Best Practices

  • Remember the Equality First, Range Second rule when designing composite indexes: place columns filtered by `=` first, and columns filtered by range (`>`, `<`, `BETWEEN`) last.
  • InnoDB secondary indexes automatically append the primary key column at the leaf level, so you never need to include the primary key at the end of a composite index.