Searching a database without an Index is like looking for a name in a phone book that isn't alphabetized. You have to read every single page from start to finish. This is called a Full Table Scan. An Index is the alphabetized version of that data.
Index: A separate data structure (usually a B-Tree) that stores the values of a column in a specific order, along with a "pointer" to the actual row record on the disk.
- Speed: Finding a row in a table of 1 Billion rows takes milliseconds with an index, but minutes without one.
- Optimization: MySQL uses indexes to speed up
WHEREfilters,JOINconditions, andORDER BYsorting. - Uniqueness: Unique indexes ensure that no two rows have the same value (like Emails).
MySQL (InnoDB) uses B+ Trees for indexing.
- Structure: Imagine a branching tree. The top is the "Root," the middle are "Branches," and the bottom are "Leaves."
- Order: Every value in the tree is sorted.
- Search Logic (O(log N)): To find
ID 500, MySQL starts at the root, sees that 500 is > 100, moves to the right branch, sees that 500 is < 700, and arrives at the leaf containing 500 in just 3-4 steps.
The Cost: An index is not "Free."
- It consumes Disk Space.
- It slows down
INSERT/UPDATE/DELETEbecause MySQL has to update the index every time the data changes.
-- Create an Index on the Email column
CREATE INDEX idx_user_email ON Users(Email);
-- Remove an Index
DROP INDEX idx_user_email ON Users;
-- See all indexes on a table
SHOW INDEX FROM Users;The "Needle in a Haystack":
Searching for a specific Transaction_ID in a table with 500 million records.
- Without Index: MySQL reads 500,000,000 rows. (Slow).
- With Index: MySQL reads ~30 internal index nodes. (Instant).
- Task 1: What is the primary benefit of creating an index on a column?
- Task 2: Name one potential downside of having too many indexes on a single table.