What it is: A composite index is an index that contains multiple columns in a specific order, e.g. (user_id, status, created_at).
What it’s for: Speed up queries that filter/sort by those columns, especially when the query uses the leftmost columns first.
Key rule (leftmost prefix):
If your index is (A, B, C), it helps queries using:
It usually does NOT help as much if you only filter by (B) or (C) without (A).
CREATE TABLE orders (
id BIGINT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
user_id BIGINT UNSIGNED NOT NULL,
STATUS VARCHAR(20) NOT NULL,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);
CREATE INDEX idx_orders_user_status_created
ON orders (user_id, STATUS, created_at);
-- Uses (user_id, status, created_at)
SELECT *
FROM orders
WHERE user_id = 10 AND STATUS = 'paid'
ORDER BY created_at DESC
LIMIT 50;
-- Uses leftmost prefix (user_id)
SELECT *
FROM orders
WHERE user_id = 10
ORDER BY created_at DESC
LIMIT 50;
-- Uses (user_id, status) even without ordering
SELECT COUNT(*)
FROM orders
WHERE user_id = 10 AND STATUS = 'paid';
-- Missing leftmost column (user_id)
SELECT *
FROM orders
WHERE STATUS = 'paid'
ORDER BY created_at DESC;
EXPLAIN
SELECT *
FROM orders
WHERE user_id = 10 AND STATUS = 'paid'
ORDER BY created_at DESC
LIMIT 50;
Vocabulary (hard words):
composite /kəmˈpɑː.zɪt/ — composite (kết hợp)
selectivity /sɪˌlek.tɪˈvɪt.i/ — selectivity (độ phân biệt / mức “lọc” tốt)
prefix /ˈpriː.fɪks/ — prefix (tiền tố)
verify /ˈver.ɪ.faɪ/ — verify (xác minh)
slow down /sloʊ daʊn/ — slow down (làm chậm)