🗃️Database Index
Also known as Index, SQL Index, Table Index
A sorted list that lets the database find rows fast. It skips the rest.
Think of it like
Think of it like the index at the back of a book. You jump to the right page instead of reading it all.
Example
Jack's orders table grows to 2 million rows and the search page takes 4 seconds. An index on the email column brings it under 10 milliseconds.
Why it matters
Definition
Example code
sql
EXPLAIN QUERY PLAN SELECT * FROM customers WHERE city = 'London';
-- SCAN customers
CREATE INDEX idx_city ON customers(city);
EXPLAIN QUERY PLAN SELECT * FROM customers WHERE city = 'London';
-- SEARCH customers USING INDEX idx_city (city=?)Common mistake
Key takeaways
- 1Jump, do not scan. Find rows without reading all.
- 2Faster reads. But a little slower writes.
- 3Pick the columns. Index what you search on.