What if you could find any piece of data instantly, no matter how you search?
Primary vs secondary indexes in DBMS Theory - When to Use Which
Start learning this pattern below
Jump into concepts and practice - no test required
Imagine you have a huge phone book with thousands of names and numbers. You want to find a friend's number quickly, but the book has no tabs or markers. You have to flip through every page until you find the name.
Searching manually is slow and tiring. If you want to find a number by name, you must check every entry. If you want to find a number by address, you have no easy way. This wastes time and causes mistakes.
Primary and secondary indexes act like organized tabs in the phone book. A primary index sorts entries by a main key, like name, so you find it fast. A secondary index lets you search by other keys, like address, without scanning everything.
search all records one by one for a valueuse primary index for main key search; use secondary index for other keys
Indexes let databases find data instantly using different keys, making searches fast and efficient.
When you search for a product on an online store by name, the primary index helps. When you filter by brand or price, secondary indexes speed up the results.
Primary index organizes data by main key for quick access.
Secondary index allows fast search on other attributes.
Both improve speed and reduce errors in data retrieval.
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
