Why indexing speeds up data retrieval in DBMS Theory - Performance Analysis
Start learning this pattern below
Jump into concepts and practice - no test required
When we search for data in a database, the time it takes can change a lot depending on how the data is organized.
We want to understand how using an index changes the speed of finding data.
Analyze the time complexity of searching data with and without an index.
-- Without index: full table scan
SELECT * FROM Employees WHERE EmployeeID = 12345;
-- With index on EmployeeID
CREATE INDEX idx_employeeid ON Employees(EmployeeID);
SELECT * FROM Employees WHERE EmployeeID = 12345;
The first query searches the whole table, the second uses an index to find the data faster.
Look at what repeats when searching:
- Primary operation without index: Checking each row one by one.
- How many times: Once for every row in the table.
- Primary operation with index: Navigating the index tree to find the matching entry.
- How many times: A few steps, much less than total rows.
Imagine the table grows bigger:
| Input Size (n) | Approx. Operations Without Index | Approx. Operations With Index |
|---|---|---|
| 10 | 10 checks | About 3 steps |
| 100 | 100 checks | About 4 steps |
| 1000 | 1000 checks | About 5 steps |
Without an index, the work grows directly with the number of rows. With an index, the steps grow slowly, even if the table gets very large.
Time Complexity: O(n) without index, O(log n) with index
This means searching without an index takes longer as the table grows, but with an index, it stays fast even for big tables.
[X] Wrong: "Adding an index always makes every query faster."
[OK] Correct: Indexes speed up searches but can slow down data changes like inserts or updates because the index must be updated too.
Understanding how indexes affect search speed shows you know how databases handle data efficiently, a useful skill for many real-world projects.
"What if the index was on a column with many repeated values instead of unique ones? How would the time complexity change?"
Practice
Solution
Step 1: Understand what indexing does
Indexing creates a special data structure that helps find data quickly without scanning the whole table.Step 2: Compare to a book's index
Just like a book's index lets you find a topic page fast, database indexes let the system find rows quickly.Final Answer:
Because it creates a quick lookup structure like a book's index -> Option AQuick Check:
Index = Quick lookup [OK]
- Confusing indexing with data compression
- Thinking indexing deletes data
- Assuming indexing randomizes data order
employee_id in SQL?Solution
Step 1: Recall SQL syntax for creating an index
The correct syntax starts withCREATE INDEX, followed by the index name, thenONand the table and column.Step 2: Match syntax with options
CREATE INDEX idx_emp ON employees(employee_id); matches the correct SQL syntax exactly.Final Answer:
CREATE INDEX idx_emp ON employees(employee_id); -> Option CQuick Check:
CREATE INDEX ... ON ... [OK]
- Using wrong keyword order
- Confusing CREATE INDEX with other commands
- Missing ON keyword
username column. What will likely happen when you run SELECT * FROM users WHERE username = 'alice';?Solution
Step 1: Understand the role of index in query
The index onusernamehelps the database find the row with 'alice' quickly without scanning the entire table.Step 2: Analyze the query execution
The database uses the index to jump directly to the matching row, improving speed.Final Answer:
The database uses the index to quickly find 'alice' without scanning all rows -> Option AQuick Check:
Index speeds up SELECT search [OK]
- Thinking index slows down SELECT
- Believing index is ignored in queries
- Assuming query deletes data
Solution
Step 1: Understand index maintenance during inserts
When new rows are inserted, the index must also be updated to include the new data, adding extra work.Step 2: Explain why this slows inserts
This extra step means inserts take longer compared to no index.Final Answer:
Indexes require extra work to update during inserts -> Option BQuick Check:
Index update slows inserts [OK]
- Thinking indexes block inserts
- Believing indexes delete data
- Assuming indexes compress data during insert
email. You create an index on email. However, queries are still slow. What could be a reason?Solution
Step 1: Understand how functions affect index usage
If a query applies a function like LOWER() on the indexed column, the index may not be used because the function changes the data.Step 2: Explain why this causes slow queries
Without using the index, the database must scan many rows, causing slow performance.Final Answer:
The index is not used because the query filters with a function like LOWER(email) -> Option DQuick Check:
Functions on indexed columns block index use [OK]
- Assuming indexes always speed queries
- Believing table size alone blocks indexes
- Thinking text columns cannot be indexed
