Index selection guidelines in DBMS Theory - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
Choosing the right index affects how fast a database finds data.
We want to know how the time to find data changes as the data grows.
Analyze the time complexity of searching with and without an index.
-- Without index
SELECT * FROM employees WHERE employee_id = 12345;
-- With index on employee_id
CREATE INDEX idx_employee_id ON employees(employee_id);
SELECT * FROM employees WHERE employee_id = 12345;
This code shows a search query before and after adding an index on the searched column.
Look at what repeats when searching for data.
- Primary operation: Scanning rows to find matching employee_id.
- How many times: Without index, it checks each row one by one; with index, it uses a tree to jump directly.
As the table grows, the search time changes differently with and without an index.
| Input Size (n) | Approx. Operations Without Index |
|---|---|
| 10 | 10 checks |
| 100 | 100 checks |
| 1000 | 1000 checks |
| Input Size (n) | Approx. Operations With Index |
|---|---|
| 10 | About 4 steps |
| 100 | About 7 steps |
| 1000 | About 10 steps |
Pattern observation: Without index, operations grow directly with data size; with index, operations grow slowly as data grows.
Time Complexity: O(n) without index, O(log n) with index
This means searching without an index gets slower as data grows, but with an index it stays much faster.
[X] Wrong: "Adding an index always makes queries faster."
[OK] Correct: Indexes speed up searches but slow down data changes like inserts or updates because the index must be updated too.
Understanding how indexes affect search time helps you explain database speed choices clearly and confidently.
"What if we added a composite index on two columns instead of one? How would the time complexity change?"
Practice
Solution
Step 1: Understand the purpose of an index
An index is like a shortcut that helps the database find rows faster when searching by that column.Step 2: Compare options with index purpose
Only speeding up searches matches the main use of indexes; other options do not relate to indexing benefits.Final Answer:
To speed up searches on that column -> Option CQuick Check:
Indexes improve search speed = A [OK]
- Thinking indexes reduce database size
- Believing indexes speed up data insertion
- Confusing indexes with uniqueness constraints
idx_name on the column last_name of the table employees?Solution
Step 1: Recall standard SQL syntax for creating an index
The correct syntax is: CREATE INDEX index_name ON table_name (column_name);Step 2: Match options to syntax
CREATE INDEX idx_name ON employees (last_name); matches the correct syntax exactly; others have wrong order or keywords.Final Answer:
CREATE INDEX idx_name ON employees (last_name); -> Option AQuick Check:
Standard SQL index creation = C [OK]
- Swapping keywords order
- Omitting the INDEX keyword
- Placing index name after table name incorrectly
orders with columns order_id, customer_id, and order_date. If you create an index on customer_id, what will be the expected effect when running this query?SELECT * FROM orders WHERE customer_id = 123;Solution
Step 1: Understand index effect on search queries
An index on customer_id allows the database to quickly locate rows where customer_id = 123 without scanning the whole table.Step 2: Analyze query behavior with index
The query uses a WHERE condition on customer_id, so the index speeds up the search, making the query faster.Final Answer:
The query will run faster because the index helps find matching rows quickly -> Option DQuick Check:
Index speeds up WHERE searches = B [OK]
- Thinking indexes slow down searches
- Believing indexes filter out data
- Assuming indexes cause errors in queries
email column of the users table, but after inserting many new users, the database performance for inserts slowed down significantly. What is the most likely cause?Solution
Step 1: Understand index impact on data changes
Indexes improve search speed but add overhead during inserts because the index structure must be updated for each new row.Step 2: Analyze why inserts slow down
Since the index updates on every insert, many inserts cause slower performance, which matches Indexes slow down data insertion because they must update with each insert.Final Answer:
Indexes slow down data insertion because they must update with each insert -> Option BQuick Check:
Indexes slow inserts due to update overhead = A [OK]
- Blaming wrong column choice for insert slowdown
- Thinking indexes cause errors on email columns
- Assuming small tables don't need indexes
sales table with columns sale_id, product_id, sale_date, and region. You often run queries filtering by product_id and region together. Which index strategy is best to improve query speed without hurting insert performance too much?Solution
Step 1: Analyze query filter columns
Queries filter by both product_id and region together, so a composite index on both columns helps the database find matching rows efficiently.Step 2: Compare index strategies
Separate indexes may help but are less efficient for combined filters; indexing sale_date is irrelevant here; no index hurts query speed.Final Answer:
Create a composite index on (product_id, region) -> Option AQuick Check:
Composite index matches multi-column filters = D [OK]
- Creating separate indexes instead of composite
- Indexing unrelated columns
- Avoiding indexes and hurting query speed
