The five newest bugs
Warm-up · indexing, primary-key, limit
Given a table bugs(id, token, title, category, device, reported_at, created_at, updated_at)
with about 10 million rows, list the 5 most recently created bugs, newest first.
Output the columns (id, title).
Expected columns
id, title
Hints
Hint 1 — is created_at indexed?
Run SHOW INDEX FROM bugs. created_at is in no index, so ORDER BY created_at DESC has to read and sort all 10M rows before it can return the first 5.
Hint 2 — the table's own order
InnoDB stores rows in primary-key order. Look at the first rows: created_at grows with id. Which ORDER BY lets MySQL read the last 5 entries of the primary key and stop?