Unique indexes in MySQL - Time & Space Complexity
When we use unique indexes in a database, it helps keep data clean by preventing duplicates.
We want to understand how the time to check uniqueness grows as the data grows.
Analyze the time complexity of the following code snippet.
CREATE TABLE users (
id INT PRIMARY KEY,
email VARCHAR(255),
UNIQUE INDEX unique_email (email)
);
INSERT INTO users (id, email) VALUES (1, 'a@example.com');
INSERT INTO users (id, email) VALUES (2, 'b@example.com');
-- Attempt to insert duplicate email
INSERT INTO users (id, email) VALUES (3, 'a@example.com');
This code creates a table with a unique index on the email column and tries to insert rows, including a duplicate email.
- Primary operation: Checking if the email already exists in the unique index.
- How many times: Once per insert operation, the database searches the index to find duplicates.
As more rows are added, the database must search a larger index to check uniqueness.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 4 steps to find or reject duplicate |
| 100 | About 7 steps |
| 1000 | About 10 steps |
Pattern observation: The number of steps grows slowly as data grows, not one-by-one.
Time Complexity: O(log n)
This means checking for duplicates takes a little more time as data grows, but it grows slowly and efficiently.
[X] Wrong: "Checking for duplicates takes the same time no matter how many rows there are."
[OK] Correct: The database uses a special structure that lets it find duplicates faster than checking every row one by one.
Understanding how unique indexes work and their time cost shows you know how databases keep data clean efficiently.
"What if the unique index was on multiple columns instead of one? How would the time complexity change?"
Practice
UNIQUE index in MySQL?Solution
Step 1: Understand the role of UNIQUE index
A UNIQUE index ensures that no two rows have the same value in the indexed column(s).Step 2: Compare options with this definition
Only To prevent duplicate values in one or more columns correctly states that UNIQUE prevents duplicates. Others are incorrect or unrelated.Final Answer:
To prevent duplicate values in one or more columns -> Option AQuick Check:
UNIQUE index = prevent duplicates [OK]
- Thinking UNIQUE speeds up inserts
- Believing UNIQUE allows duplicates
- Confusing UNIQUE with automatic deletion
email in a table named users?Solution
Step 1: Recall correct syntax for UNIQUE index creation
The correct syntax is: CREATE UNIQUE INDEX index_name ON table_name(column_name);Step 2: Match options with correct syntax
CREATE UNIQUE INDEX idx_email ON users(email); matches exactly. Others have wrong keyword order or syntax errors.Final Answer:
CREATE UNIQUE INDEX idx_email ON users(email); -> Option CQuick Check:
CREATE UNIQUE INDEX ... ON ... (column) [OK]
- Swapping UNIQUE and INDEX keywords
- Using FROM instead of ON
- Incorrect keyword order
employees with a UNIQUE index on employee_id, what happens if you run this query?INSERT INTO employees (employee_id, name) VALUES (101, 'Alice');
INSERT INTO employees (employee_id, name) VALUES (101, 'Bob');
Solution
Step 1: Understand UNIQUE index effect on inserts
A UNIQUE index on employee_id prevents duplicate values in that column.Step 2: Analyze the inserts
The first insert adds employee_id 101. The second insert tries to add the same employee_id 101, violating the UNIQUE constraint, causing an error.Final Answer:
The second insert fails with a duplicate key error -> Option AQuick Check:
UNIQUE index blocks duplicate inserts [OK]
- Assuming duplicates are allowed
- Thinking second insert overwrites first
- Believing second insert is ignored silently
username but got an error. Which of these is the most likely cause?Solution
Step 1: Understand UNIQUE index creation requirements
Creating a UNIQUE index requires all existing values in the column to be unique.Step 2: Identify common error causes
If duplicates exist, MySQL throws an error when creating the UNIQUE index.Final Answer:
The username column contains duplicate values -> Option BQuick Check:
Duplicates block UNIQUE index creation [OK]
- Assuming index name is mandatory
- Believing UNIQUE not allowed on text columns
- Ignoring existing duplicates
first_name and last_name in the contacts table is unique, but duplicates are still appearing. Which is the best way to fix this?Solution
Step 1: Understand uniqueness on multiple columns
To ensure uniqueness on a combination, a composite UNIQUE index on both columns is needed.Step 2: Analyze other options
Separate UNIQUE indexes allow duplicates across combinations. PRIMARY KEY on one column doesn't cover both. CHECK constraints can't enforce uniqueness.Final Answer:
Create a UNIQUE index on both first_name and last_name columns together -> Option DQuick Check:
Composite UNIQUE index = unique pairs [OK]
- Creating separate UNIQUE indexes instead of composite
- Using PRIMARY KEY on one column only
- Relying on CHECK constraints for uniqueness
Ready to prove it?
Pass the test and this pattern moves unknown
