When indexes help and when they hurt in SQL - Time & Space Complexity
Indexes can speed up database searches but sometimes slow down other operations.
We want to understand when using an index helps and when it might hurt performance.
Analyze the time complexity of this query using an index.
-- Assume an index exists on column 'name'
SELECT * FROM employees WHERE name = 'Alice';
-- Insert operation
INSERT INTO employees (id, name, department) VALUES (101, 'Bob', 'Sales');
The first query searches for a specific name using an index. The second inserts a new row, which may update the index.
Look at what repeats during these operations.
- Primary operation for SELECT: Searching the index tree to find matching rows.
- How many times: Depends on the height of the index tree, usually much less than scanning all rows.
- Primary operation for INSERT: Adding a new entry to the index structure.
- How many times: Once per insert, but involves updating the index which can be costly.
As the table grows, the index search stays efficient, but inserts get a bit slower.
| Input Size (n) | Approx. Operations for SELECT | Approx. Operations for INSERT |
|---|---|---|
| 10 | About 3 steps (small tree height) | Simple insert + index update |
| 100 | About 4 steps | Insert + more index updates |
| 1000 | About 5 steps | Insert + even more index updates |
Pattern observation: SELECT grows slowly with input size, INSERT grows a bit faster due to index maintenance.
Time Complexity: O(log n)
This means searching with an index grows slowly as data grows, but insertions cost more because the index must be updated.
[X] Wrong: "Adding an index always makes everything faster."
[OK] Correct: Indexes speed up searches but slow down inserts, updates, and deletes because the index must be maintained.
Understanding when indexes help or hurt shows you know how databases balance speed and cost, a key skill for real projects.
"What if we added multiple indexes on different columns? How would that affect insert and search time complexity?"
Practice
Solution
Step 1: Understand what indexes do
Indexes help the database find rows faster by creating a quick lookup structure on columns.Step 2: Identify when this is useful
Searching rows by column values is faster with indexes, but inserts or deletes can slow down.Final Answer:
When you want to quickly find rows based on column values. -> Option CQuick Check:
Indexes speed up searching [OK]
- Thinking indexes speed up inserts
- Confusing index purpose with storage size
- Assuming indexes help deletes
email in the 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 the options to syntax
CREATE INDEX idx_email ON users(email); matches the correct syntax exactly.Final Answer:
CREATE INDEX idx_email ON users(email); -> Option AQuick Check:
Correct CREATE INDEX syntax = CREATE INDEX idx_email ON users(email); [OK]
- Swapping table and column names
- Omitting ON keyword
- Wrong order of keywords
orders with 1 million rows and an index on customer_id, what is the expected effect of running SELECT * FROM orders WHERE customer_id = 123;?Solution
Step 1: Understand index usage in WHERE clause
Indexes help the database quickly locate rows matching the WHERE condition on indexed columns.Step 2: Apply to the query
Since customer_id is indexed, the query uses the index to find rows without scanning all 1 million rows.Final Answer:
The query will use the index to quickly find matching rows. -> Option AQuick Check:
Indexed WHERE columns speed up SELECT [OK]
- Thinking indexes slow down SELECT
- Believing indexes are ignored in WHERE
- Assuming query fails with indexes
Solution
Step 1: Understand index impact on inserts
Each insert must update all indexes to keep them current, which adds overhead and slows inserts.Step 2: Identify cause of slow inserts
Adding many indexes increases this overhead, causing slower insert performance.Final Answer:
Indexes slow down inserts because each insert updates all indexes. -> Option DQuick Check:
More indexes = slower inserts [OK]
- Thinking indexes speed up inserts
- Blaming syntax errors for slow inserts
- Ignoring index update cost
products has indexes on category_id and price. You run a query filtering on category_id and sorting by price. How can indexes help, and what might hurt performance?Solution
Step 1: Analyze filtering and sorting with indexes
The index on category_id speeds up filtering rows matching that category. The index on price can speed up sorting if used properly.Step 2: Consider performance trade-offs
Having multiple indexes helps reads but slows down inserts and updates because all indexes must be maintained.Final Answer:
Index on category_id helps filtering; index on price helps sorting; but too many indexes slow inserts. -> Option BQuick Check:
Indexes help filtering and sorting but slow inserts [OK]
- Believing indexes only help joins
- Thinking indexes speed up inserts
- Assuming indexes cause syntax errors
Ready to prove it?
Pass the test and this pattern moves unknown
