Composite index and column order in SQL - Time & Space Complexity
When using composite indexes in databases, the order of columns affects how fast queries run.
We want to understand how the query speed changes as data grows, depending on column order.
Analyze the time complexity of this query using a composite index on (last_name, first_name):
SELECT * FROM employees
WHERE last_name = 'Smith' AND first_name = 'John';
This query looks up employees by last and first name using a composite index.
Look at what repeats when searching with the composite index.
- Primary operation: Searching the index tree by matching last_name first, then first_name.
- How many times: The database narrows down matches step-by-step, scanning fewer rows as it goes.
As the number of employees grows, the search stays fast because the index narrows results quickly.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 3 steps to find matching names |
| 100 | About 7 steps |
| 1000 | About 10 steps |
Pattern observation: The steps grow slowly, not directly with total data size, because the index guides the search.
Time Complexity: O(log n)
This means the search time grows slowly as data grows, thanks to the index structure.
[X] Wrong: "Changing the order of columns in the composite index does not affect query speed."
[OK] Correct: The database uses the first column to quickly narrow results; if the query filters on the second column first, the index is less effective.
Understanding how composite indexes work and how column order affects query speed shows you know how databases handle large data efficiently.
What if we reversed the column order in the composite index to (first_name, last_name)? How would that change the time complexity for the same query?
Practice
composite index in a database?Solution
Step 1: Understand what a composite index does
A composite index is created on multiple columns to help the database find rows faster when queries filter on those columns together.Step 2: Compare options to the definition
Only It speeds up queries filtering on multiple columns together. correctly describes the benefit of speeding up queries using multiple columns. Other options describe unrelated features.Final Answer:
It speeds up queries filtering on multiple columns together. -> Option DQuick Check:
Composite index = speeds up multi-column queries [OK]
- Thinking it reduces database size
- Confusing indexing with backup or encryption
- Assuming it speeds up single-column queries only
last_name and first_name in a table employees?Solution
Step 1: Recall correct CREATE INDEX syntax
The correct syntax is: CREATE INDEX index_name ON table_name (column1, column2);Step 2: Match options to syntax
CREATE INDEX idx_name ON employees (last_name, first_name); matches the correct syntax exactly. Options A, C, and D have syntax errors or misplaced keywords.Final Answer:
CREATE INDEX idx_name ON employees (last_name, first_name); -> Option BQuick Check:
CREATE INDEX ... ON table (columns) [OK]
- Omitting parentheses around columns
- Placing columns before ON keyword
- Using WHERE instead of column list
(city, age) in a table users, which query will use the index efficiently?Solution
Step 1: Understand index column order usage
A composite index on (city, age) can be used efficiently if the query filters on city first, then age.Step 2: Analyze each query
SELECT * FROM users WHERE city = 'Boston' AND age > 30; filters on city and age, matching index order. SELECT * FROM users WHERE age > 30; filters only on age (second column), so index is less useful. SELECT * FROM users WHERE age = 25 AND city = 'Boston'; filters age first, then city, which is less efficient. SELECT * FROM users WHERE city = 'Boston' ORDER BY age DESC; filters city but only orders by age, which may use index partially.Final Answer:
SELECT * FROM users WHERE city = 'Boston' AND age > 30; -> Option CQuick Check:
Index (city, age) best for city then age filters [OK]
- Assuming index works well if filtering only second column
- Ignoring column order in composite index
- Thinking ORDER BY always uses index fully
(department, salary) but your query filtering only on salary is slow. What is the likely cause?Solution
Step 1: Recall how composite index column order affects usage
Composite indexes work best when filtering starts with the first column. Filtering only on the second column (salary) won't use the index efficiently.Step 2: Evaluate options
The index column order starts with department, so filtering only salary can't use it well. correctly explains the cause. Options A, B, and D are unlikely or incorrect because the index exists and salary can be indexed normally.Final Answer:
The index column order starts with department, so filtering only salary can't use it well. -> Option AQuick Check:
Filter must start with first index column [OK]
- Assuming any column in index can be filtered efficiently alone
- Blaming index corruption without checking usage
- Ignoring column order importance
orders with columns customer_id, order_date, and status. You often run queries filtering by customer_id and sorting by order_date. Which composite index will optimize these queries best?Solution
Step 1: Identify query filter and sort columns
Queries filter by customer_id and sort by order_date, so index should start with customer_id and include order_date next.Step 2: Match index column order to query pattern
CREATE INDEX idx_customer_date ON orders (customer_id, order_date); matches this order (customer_id, order_date), allowing efficient filtering and sorting. CREATE INDEX idx_date_customer ON orders (order_date, customer_id); reverses order, less efficient for filtering. Options A, B, and C do not match query needs.Final Answer:
CREATE INDEX idx_customer_date ON orders (customer_id, order_date); -> Option AQuick Check:
Index columns match filter then sort order [OK]
- Reversing column order in composite index
- Indexing unrelated columns
- Using single-column index when composite needed
Ready to prove it?
Pass the test and this pattern moves unknown
