0
0
SQLquery~10 mins

When indexes help and when they hurt in SQL - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - When indexes help and when they hurt
Query arrives
Check for index on columns used
Use index
Fast search
Return results
Consider index maintenance cost on writes
When a query runs, the database checks if an index can speed up searching. Using an index helps read speed but can slow down writes because indexes need updating.
Execution Sample
SQL
SELECT * FROM employees WHERE department_id = 5;
-- Assume index on department_id exists

SELECT * FROM employees WHERE last_name LIKE '%son';
-- No index used here
Two queries: one uses an index for fast lookup, the other does not and scans the whole table.
Execution Table
StepQueryIndex Used?ActionPerformance Impact
1SELECT * FROM employees WHERE department_id = 5;YesUse index to find matching rowsFast read
2Return matching rowsN/AFetch rows by row IDs from indexFast response
3SELECT * FROM employees WHERE last_name LIKE '%son';NoFull table scan to check each rowSlow read
4Return matching rowsN/AReturn rows after scanning allSlow response
5INSERT INTO employees ...YesUpdate table and indexSlower write due to index maintenance
6UPDATE employees SET ...YesUpdate table and indexSlower write due to index maintenance
7DELETE FROM employees WHERE ...YesRemove from table and indexSlower write due to index maintenance
💡 Queries stop after returning results; writes include extra index update steps causing slower performance.
Variable Tracker
OperationIndex UsageRows ScannedRows ReturnedWrite Cost
SELECT with indexYesFew (only matching)FewNone
SELECT without indexNoAll rowsFewNone
INSERT/UPDATE/DELETEYesN/AN/AHigh (index updated)
Key Moments - 3 Insights
Why does using an index slow down INSERT or UPDATE operations?
Because each write must update the index structure as well as the table, adding extra work and time (see execution_table rows 5-7).
Why does a query with a condition like last_name LIKE '%son' not use an index?
Because the wildcard at the start prevents the database from using the index efficiently, so it scans the whole table (see execution_table row 3).
When does using an index help the most?
When the query filters on columns with an index and the condition allows the index to quickly find matching rows (see execution_table rows 1-2).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, at which step does the database perform a full table scan?
AStep 3
BStep 1
CStep 5
DStep 2
💡 Hint
Check the 'Action' column for 'Full table scan' in execution_table row 3.
According to variable_tracker, which operation has the highest write cost?
ASELECT with index
BINSERT/UPDATE/DELETE
CSELECT without index
DSELECT with no condition
💡 Hint
Look at the 'Write Cost' column in variable_tracker row for INSERT/UPDATE/DELETE.
If the query changes to WHERE last_name LIKE 'son%', how would the execution_table change?
AQuery would fail due to syntax error
BStill no index used, full table scan happens
CIndex would be used, making the search faster
DIndex would slow down the query
💡 Hint
Queries with wildcard only at the end can use indexes efficiently (see key_moments about LIKE patterns).
Concept Snapshot
When indexes help and when they hurt:
- Indexes speed up reads by quickly locating rows.
- Indexes slow down writes because they must be updated.
- Queries with conditions matching indexed columns benefit most.
- Wildcards at the start of LIKE prevent index use.
- Balance index use based on read/write patterns.
Full Transcript
This visual execution shows how indexes affect query performance. When a query filters on an indexed column, the database uses the index to quickly find matching rows, speeding up reads. However, if the query condition prevents index use, like a wildcard at the start of a LIKE pattern, the database scans the entire table, which is slower. Also, when inserting, updating, or deleting rows, the database must update the indexes, which slows down write operations. Understanding when indexes help or hurt helps design better databases and queries.