Primary vs secondary indexes in DBMS Theory - Performance Comparison
Start learning this pattern below
Jump into concepts and practice - no test required
Indexes help databases find data faster. We want to understand how using primary or secondary indexes affects the time it takes to search.
How does the choice of index change the work done as data grows?
Analyze the time complexity of searching with primary and secondary indexes.
-- Searching with Primary Index
SELECT * FROM Employees WHERE EmployeeID = 12345;
-- Searching with Secondary Index
SELECT * FROM Employees WHERE Department = 'Sales';
The first query uses a primary index on EmployeeID, the second uses a secondary index on Department.
Look at what repeats when searching:
- Primary index search: Uses a tree or sorted structure to find one record quickly.
- Secondary index search: Finds all matching records, may need extra steps to get full data.
- Dominant operation: Number of lookups in the index and data retrieval steps.
As the number of records (n) grows, the work changes:
| Input Size (n) | Primary Index Lookups | Secondary Index Lookups |
|---|---|---|
| 10 | About 3 steps | About 3 steps + fetching multiple rows |
| 100 | About 7 steps | About 7 steps + fetching multiple rows |
| 1000 | About 10 steps | About 10 steps + fetching multiple rows |
Primary index search grows slowly with data size. Secondary index search also grows slowly for lookup but may fetch many rows, increasing work.
Time Complexity: O(log n) for primary index search, O(log n + k) for secondary index search where k is matching rows.
This means finding one record by primary index is fast and grows slowly, but secondary index search can take longer if many records match.
[X] Wrong: "Secondary indexes are always as fast as primary indexes."
[OK] Correct: Secondary indexes may return many records, so fetching all data can take more time than a single primary key lookup.
Understanding how indexes affect search speed shows you know how databases handle data efficiently. This helps you explain choices in real projects clearly.
"What if the secondary index is on a unique column? How would the time complexity change?"
Practice
primary index in a database?Solution
Step 1: Understand the role of primary index
A primary index is created on the primary key of a table, which uniquely identifies each record.Step 2: Identify its main function
It ensures fast and unique access to records based on the primary key values.Final Answer:
To provide unique and fast access to records using the primary key -> Option AQuick Check:
Primary index = unique fast access [OK]
- Confusing primary index with secondary index
- Thinking primary index allows duplicates
- Assuming primary index is for backup
Solution
Step 1: Recall SQL syntax for indexes
Secondary indexes are created using the standardCREATE INDEXstatement without the PRIMARY keyword.Step 2: Identify the correct syntax
CREATE INDEX idx_name ON table(column); uses the correct syntax:CREATE INDEX idx_name ON table(column);Final Answer:
CREATE INDEX idx_name ON table(column); -> Option AQuick Check:
Secondary index syntax = CREATE INDEX [OK]
- Using CREATE SECONDARY INDEX which is invalid
- Confusing with CREATE PRIMARY INDEX syntax
- Using UNIQUE keyword incorrectly for secondary index
Employees(emp_id, name, department) where emp_id is the primary key. Which index type would speed up a query filtering by department?Solution
Step 1: Identify the primary key and its index
The primary key isemp_id, so the primary index is onemp_id.Step 2: Determine which index helps filter by department
Sincedepartmentis not the primary key, a secondary index ondepartmentspeeds up queries filtering by it.Final Answer:
Secondary index on department -> Option CQuick Check:
Filter by non-key column = secondary index [OK]
- Assuming primary index helps filter by any column
- Trying to create primary index on non-key column
- Ignoring the benefit of secondary indexes
Solution
Step 1: Understand secondary index behavior with duplicates
Secondary indexes can be created on columns with duplicates but may become less efficient because many records share the same key.Step 2: Identify the impact on performance
Low uniqueness means the index has many entries pointing to multiple rows, slowing down search performance.Final Answer:
The secondary index will be inefficient due to low uniqueness -> Option DQuick Check:
Duplicates in secondary index = inefficiency [OK]
- Thinking secondary index enforces uniqueness
- Believing primary index gets corrupted
- Expecting index creation to fail
customer_id and a secondary index on city. You want to optimize queries filtering by both customer_id and city. What is the best indexing strategy?Solution
Step 1: Analyze current indexes and query filters
Primary index exists oncustomer_id, secondary index oncity. Queries filter by both columns.Step 2: Understand composite index benefits
A composite index on (customer_id, city) allows efficient filtering on both columns together, improving query speed.Step 3: Evaluate other options
Dropping indexes or creating separate secondary indexes won't optimize combined filtering as well as a composite index.Final Answer:
Create a composite index on (customer_id, city) -> Option BQuick Check:
Combined filter = composite index [OK]
- Dropping useful indexes
- Creating redundant secondary indexes
- Ignoring composite index advantages
