Creating indexes in MySQL - Performance & Efficiency
When we create indexes in a database, it helps us find data faster. But creating these indexes also takes some time.
We want to know how the time to create an index grows as the amount of data grows.
Analyze the time complexity of the following code snippet.
CREATE INDEX idx_customer_name ON customers(name);
This code creates an index on the "name" column of the "customers" table to speed up searches by name.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Scanning all rows in the table to build the index structure.
- How many times: Once for each row in the table.
As the number of rows grows, the time to create the index grows roughly in the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 operations to read and insert into the index |
| 100 | About 100 operations |
| 1000 | About 1000 operations |
Pattern observation: The time grows roughly in direct proportion to the number of rows.
Time Complexity: O(n)
This means creating the index takes time proportional to the number of rows in the table.
[X] Wrong: "Creating an index is instant and does not depend on table size."
[OK] Correct: The database must look at every row to build the index, so more rows mean more work and more time.
Understanding how index creation time grows helps you explain database performance clearly and shows you know how data size affects operations.
"What if the index was created on multiple columns instead of one? How would the time complexity change?"
Practice
Solution
Step 1: Understand what an index does
An index acts like a shortcut to find data faster in a table.Step 2: Identify the main benefit
Indexes speed up searches and filtering by avoiding full table scans.Final Answer:
To speed up data retrieval when searching or filtering -> Option AQuick Check:
Index = faster search [OK]
- Thinking indexes store duplicate values
- Believing indexes increase database size significantly
- Confusing indexes with backups
email in a table named users?Solution
Step 1: Recall MySQL index creation syntax
The correct syntax is: CREATE INDEX index_name ON table_name (column_name);Step 2: Match the syntax with options
CREATE INDEX email_index ON users (email); matches the correct syntax exactly.Final Answer:
CREATE INDEX email_index ON users (email); -> Option BQuick Check:
CREATE INDEX index_name ON table (column) [OK]
- Swapping order of table and index name
- Omitting ON keyword
- Using wrong keyword order
products with columns id, name, and category, what will be the effect of this query?CREATE UNIQUE INDEX idx_name ON products (name);
Solution
Step 1: Understand UNIQUE index effect
A UNIQUE index prevents duplicate values in the indexed column.Step 2: Identify additional benefit
Indexes also speed up searches on that column.Final Answer:
It prevents duplicate product names and speeds up searches by name -> Option DQuick Check:
UNIQUE index = no duplicates + faster search [OK]
- Thinking UNIQUE index allows duplicates
- Confusing index with backup or delete
- Ignoring speed improvement
CREATE INDEX idx_age ON employees age;
What is the error in this statement?
Solution
Step 1: Check syntax for CREATE INDEX
The column list must be inside parentheses after the table name.Step 2: Identify the error
The statement lacks parentheses aroundage, causing syntax error.Final Answer:
Missing parentheses around the column name -> Option CQuick Check:
Columns must be in parentheses [OK]
- Forgetting parentheses around columns
- Thinking index names have restrictions
- Assuming numeric columns can't be indexed
orders table with columns order_id, customer_id, and order_date. You often query orders by customer_id and order_date. Which index creation will best improve query speed without allowing duplicate entries for the same customer on the same date?Solution
Step 1: Analyze query needs
Queries filter by bothcustomer_idandorder_date, so a composite index on both is best.Step 2: Consider uniqueness requirement
To prevent duplicate orders for the same customer on the same date, use a UNIQUE index on both columns.Final Answer:
CREATE UNIQUE INDEX idx_customer_date ON orders (customer_id, order_date); -> Option AQuick Check:
Composite UNIQUE index matches query and uniqueness [OK]
- Creating single-column indexes only
- Ignoring uniqueness requirement
- Indexing unrelated columns
Ready to prove it?
Pass the test and this pattern moves unknown
