CREATE INDEX syntax in SQL - Time & Space Complexity
Let's explore how the time to create an index changes as the data grows.
We want to know how the work done by the database increases when building an index on a table.
Analyze the time complexity of the following SQL command.
CREATE INDEX idx_customer_name
ON customers (last_name);
This command creates an index on the last_name column of the customers table to speed up searches.
When creating an index, the database must process each row to build the index structure.
- Primary operation: Reading each row's
last_namevalue and inserting it into the index. - How many times: Once for every row in the
customerstable.
As the number of rows grows, the work to create the index grows too.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 reads and inserts |
| 100 | About 100 reads and inserts |
| 1000 | About 1000 reads and inserts |
Pattern observation: The work grows roughly in direct proportion to the number of rows.
Time Complexity: O(n log n)
This means creating the index takes a bit more than just looking at each row once, because it also organizes the data efficiently.
[X] Wrong: "Creating an index is just as fast as scanning the table once, so it is O(n)."
[OK] Correct: Building the index involves sorting or organizing data, which adds extra steps, making it slower than a simple scan.
Understanding how index creation time grows helps you explain database performance clearly and shows you know what happens behind the scenes.
"What if the index was created on multiple columns instead of one? How would the time complexity change?"
Practice
CREATE INDEX in a database?Solution
Step 1: Understand what an index does
An index creates a shortcut to quickly find data in a column without scanning the whole table.Step 2: Identify the main benefit
This shortcut speeds up data retrieval, especially for searches and sorts.Final Answer:
To speed up data retrieval by creating shortcuts on columns -> Option AQuick Check:
CREATE INDEX improves search speed [OK]
- Thinking indexes speed up data deletion
- Confusing indexes with backups
- Believing indexes encrypt data
idx_name on the column last_name of the table employees?Solution
Step 1: Recall the standard CREATE INDEX syntax
The correct syntax is: CREATE INDEX index_name ON table_name (column_name);Step 2: Match the syntax with options
CREATE INDEX idx_name ON employees (last_name); matches the correct order and keywords exactly.Final Answer:
CREATE INDEX idx_name ON employees (last_name); -> Option BQuick Check:
CREATE INDEX index_name ON table (column) [OK]
- Swapping index name and table name positions
- Using wrong keyword order
- Omitting ON keyword
products(id INT, name VARCHAR, price DECIMAL), what will be the result of this SQL command?CREATE INDEX idx_price ON products(price);
Solution
Step 1: Understand what the CREATE INDEX command does here
The command creates an index named idx_price on the price column of products.Step 2: Identify the effect of the index
This index helps speed up queries that filter or sort by price without changing the table structure.Final Answer:
An index named idx_price is created on the price column to speed up queries filtering by price -> Option CQuick Check:
CREATE INDEX adds search speed, no deletion [OK]
- Thinking index creation deletes columns
- Assuming index requires primary key
- Confusing index with table duplication
CREATE INDEX ON employees (last_name);
Solution
Step 1: Check the syntax of CREATE INDEX
The syntax requires an index name after CREATE INDEX before ON.Step 2: Identify the missing part
The statement lacks the index name, which causes a syntax error.Final Answer:
Missing index name after CREATE INDEX -> Option DQuick Check:
CREATE INDEX must have a name [OK]
- Omitting the index name
- Confusing table name with index name
- Misplacing parentheses
category and price columns in the items table. Which is the best way to create an index?Solution
Step 1: Understand multi-column index usage
A single index on (category, price) helps queries filtering by both columns efficiently.Step 2: Compare options
CREATE INDEX idx_cat_price ON items (category, price); creates a composite index on category and price in that order, which is best for combined filters.Final Answer:
CREATE INDEX idx_cat_price ON items (category, price); -> Option AQuick Check:
Composite index on both columns is best [OK]
- Creating separate indexes instead of composite
- Wrong column order in composite index
- Creating index on only one column when filtering by two
Ready to prove it?
Pass the test and this pattern moves unknown
