Index selection strategy in MySQL - Time & Space Complexity
When a database uses an index to find data, it can do it faster than searching every row.
We want to understand how choosing the right index affects the speed of finding data as the table grows.
Analyze the time complexity of this query using an index.
SELECT * FROM employees
WHERE last_name = 'Smith';
-- Assume there is an index on last_name
This query looks for all employees with the last name 'Smith' using an index on the last_name column.
Look at what repeats when the database searches for matching rows.
- Primary operation: Searching the index tree to find matching entries.
- How many times: The search follows a path down the index, which grows slowly as the table grows.
As the table gets bigger, the index search path grows slowly, not linearly.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 3 steps |
| 100 | About 7 steps |
| 1000 | About 10 steps |
Pattern observation: The number of steps grows slowly, like climbing a short ladder, even if the table grows a lot.
Time Complexity: O(log n)
This means the time to find data grows slowly as the table gets bigger, thanks to the index.
[X] Wrong: "Using an index means the query always runs instantly no matter the table size."
[OK] Correct: Even with an index, the search takes more steps as the table grows, just much fewer than scanning all rows.
Understanding how indexes speed up searches helps you explain why databases stay fast with lots of data.
"What if the query searched on a column without an index? How would the time complexity change?"
Practice
Solution
Step 1: Understand what an index does
An index creates a shortcut to quickly find data without scanning the whole table.Step 2: Compare options with index purpose
Only To speed up data retrieval by creating shortcuts describes speeding up data retrieval, which is the main use of indexes.Final Answer:
To speed up data retrieval by creating shortcuts -> Option BQuick Check:
Index = Speed up search [OK]
- Thinking indexes compress data
- Confusing indexes with backups
- Believing indexes increase database size
email in a table users?Solution
Step 1: Recall correct CREATE INDEX syntax
The correct syntax is: CREATE INDEX index_name ON table_name (column_name);Step 2: Match syntax with options
CREATE INDEX email_index ON users (email); matches the correct syntax exactly, others have wrong order or keywords.Final Answer:
CREATE INDEX email_index ON users (email); -> Option AQuick Check:
CREATE INDEX index_name ON table (column) [OK]
- Swapping index name and table name
- Omitting ON keyword
- Wrong keyword order
orders with an index on customer_id, what will happen when you run this query?SELECT * FROM orders WHERE customer_id = 123;
Solution
Step 1: Understand index usage in WHERE clause
When a column in WHERE has an index, MySQL uses it to find rows faster.Step 2: Analyze query behavior
The query filters by customer_id, which has an index, so it uses the index to speed up search.Final Answer:
The query will use the index to quickly find matching rows -> Option CQuick Check:
Indexed column in WHERE = index used [OK]
- Assuming full table scan always happens
- Confusing SELECT with UPDATE
- Expecting syntax errors in correct queries
last_name but your query SELECT * FROM employees WHERE first_name = 'John'; is slow. What is the likely problem?Solution
Step 1: Identify which column is indexed
The index is on last_name, but the query filters on first_name.Step 2: Understand index usage
Indexes only help if the filtered column is indexed; here first_name is not indexed, so index is unused.Final Answer:
The index on last_name is not used because the query filters on first_name -> Option AQuick Check:
Index column ≠ filtered column = no index use [OK]
- Expecting index on different column to help
- Thinking index causes syntax errors
- Confusing filtering with updating
products with columns category, price, and stock. You often run this query:SELECT * FROM products WHERE category = 'Books' AND price < 20;Which index strategy is best to speed up this query?
Solution
Step 1: Analyze query filter columns
The query filters on category and price together.Step 2: Choose index covering both columns
A composite index on (category, price) helps MySQL filter efficiently on both conditions.Step 3: Evaluate other options
Separate indexes or indexes on stock do not help this query's WHERE clause.Final Answer:
Create a composite index on (category, price) -> Option DQuick Check:
Composite index on filtered columns = best speed [OK]
- Indexing unrelated columns
- Using separate indexes instead of composite
- Ignoring query filter columns
Ready to prove it?
Pass the test and this pattern moves unknown
