Index maintenance in MySQL - Time & Space Complexity
When we add or remove data in a database, the indexes need to update too.
We want to know how the time to update indexes changes as data grows.
Analyze the time complexity of the following index update during an insert.
INSERT INTO employees (id, name, department) VALUES (101, 'Alice', 'Sales');
-- The database updates the index on 'id' after this insert
This code inserts a new row and updates the index on the 'id' column.
When inserting, the database finds the right place in the index tree to add the new key.
- Primary operation: Traversing the index tree from root to leaf.
- How many times: Once per insert, following the tree height.
As the table grows, the index tree gets taller, but only slowly.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 2 steps down the tree |
| 100 | About 3 steps down the tree |
| 1000 | About 4 steps down the tree |
Pattern observation: The steps grow slowly, roughly with the height of the tree, not the total rows.
Time Complexity: O(log n)
This means the time to update the index grows slowly as the data grows, making inserts efficient.
[X] Wrong: "Updating an index takes as long as scanning all rows."
[OK] Correct: Indexes use tree structures that let the database jump quickly to the right spot without checking every row.
Understanding how indexes update helps you explain why databases stay fast even with lots of data.
"What if the index was a simple list instead of a tree? How would the time complexity change?"
Practice
Solution
Step 1: Understand what indexes do
Indexes help the database find data faster, like an index in a book.Step 2: Identify the main benefit of index maintenance
Maintaining indexes keeps searches quick and efficient.Final Answer:
To speed up data searches and queries -> Option AQuick Check:
Indexes improve search speed = D [OK]
- Thinking indexes increase database size only
- Confusing index maintenance with data backup
- Believing indexes delete data
idx_name from a table users in MySQL?Solution
Step 1: Recall MySQL syntax for dropping an index
The correct command isDROP INDEX index_name ON table_name;.Step 2: Match the syntax with options
DROP INDEX idx_name ON users; matches the correct syntax exactly.Final Answer:
DROP INDEX idx_name ON users; -> Option BQuick Check:
DROP INDEX ... ON ... is correct syntax = B [OK]
- Using DELETE or REMOVE instead of DROP
- Omitting ON keyword
- Wrong keyword order
products with an index on category_id, what will be the effect of running OPTIMIZE TABLE products;?Solution
Step 1: Understand what OPTIMIZE TABLE does
OPTIMIZE TABLE rebuilds the table and its indexes to reduce fragmentation and improve speed.Step 2: Analyze the options
Only It rebuilds the table and its indexes to improve performance correctly describes this effect.Final Answer:
It rebuilds the table and its indexes to improve performance -> Option DQuick Check:
OPTIMIZE TABLE rebuilds table and indexes = A [OK]
- Thinking OPTIMIZE deletes data
- Assuming it adds indexes automatically
- Confusing it with renaming tables
DROP INDEX idx_user ON users; but got an error. What is a likely cause?Solution
Step 1: Check common reasons for DROP INDEX errors
An error usually occurs if the index name is wrong or missing.Step 2: Evaluate the options
The index nameidx_userdoes not exist on the table is the most common cause; other options are incorrect commands or unnecessary actions.Final Answer:
The index name idx_user does not exist on the table -> Option CQuick Check:
Wrong index name causes DROP INDEX error = C [OK]
- Using DELETE instead of DROP
- Assuming server restart is needed
- Not checking table existence first
orders with a composite index on (customer_id, order_date). After frequent inserts and deletes, queries on customer_id are slow. What is the best index maintenance step to improve performance?Solution
Step 1: Understand index fragmentation impact
Frequent inserts and deletes cause index fragmentation, slowing queries.Step 2: Choose the best maintenance action
OPTIMIZE TABLE rebuilds indexes and reduces fragmentation, improving query speed.Step 3: Evaluate other options
Dropping and recreating indexes separately may not help and can be costly; deleting data or renaming table is unrelated.Final Answer:
Run OPTIMIZE TABLE orders; to rebuild the table and indexes -> Option AQuick Check:
OPTIMIZE TABLE fixes fragmentation = A [OK]
- Dropping composite index without reason
- Thinking renaming table refreshes indexes
- Deleting data instead of maintaining indexes
Ready to prove it?
Pass the test and this pattern moves unknown
