Bird
Raised Fist0
SQLquery~10 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. Which of the following best describes when an index helps in a database?
easy
A. When you want to reduce the size of the database file.
B. When you want to speed up inserting new rows.
C. When you want to quickly find rows based on column values.
D. When you want to delete all rows faster.

Solution

  1. Step 1: Understand what indexes do

    Indexes help the database find rows faster by creating a quick lookup structure on columns.
  2. Step 2: Identify when this is useful

    Searching rows by column values is faster with indexes, but inserts or deletes can slow down.
  3. Final Answer:

    When you want to quickly find rows based on column values. -> Option C
  4. Quick Check:

    Indexes speed up searching [OK]
Hint: Indexes speed up searches, not inserts or deletes [OK]
Common Mistakes:
  • Thinking indexes speed up inserts
  • Confusing index purpose with storage size
  • Assuming indexes help deletes
2. Which SQL statement correctly creates an index on the column email in the table users?
easy
A. CREATE INDEX idx_email ON users(email);
B. CREATE users INDEX ON email;
C. INDEX CREATE ON users(email);
D. CREATE INDEX users_email email;

Solution

  1. Step 1: Recall correct CREATE INDEX syntax

    The correct syntax is: CREATE INDEX index_name ON table_name(column_name);
  2. Step 2: Match the options to syntax

    CREATE INDEX idx_email ON users(email); matches the correct syntax exactly.
  3. Final Answer:

    CREATE INDEX idx_email ON users(email); -> Option A
  4. Quick Check:

    Correct CREATE INDEX syntax = CREATE INDEX idx_email ON users(email); [OK]
Hint: CREATE INDEX index_name ON table(column) is correct syntax [OK]
Common Mistakes:
  • Swapping table and column names
  • Omitting ON keyword
  • Wrong order of keywords
3. Given a table orders with 1 million rows and an index on customer_id, what is the expected effect of running SELECT * FROM orders WHERE customer_id = 123;?
medium
A. The query will use the index to quickly find matching rows.
B. The query will scan all rows, ignoring the index.
C. The query will be slower because indexes slow down SELECT queries.
D. The query will fail because indexes cannot be used in WHERE clauses.

Solution

  1. Step 1: Understand index usage in WHERE clause

    Indexes help the database quickly locate rows matching the WHERE condition on indexed columns.
  2. Step 2: Apply to the query

    Since customer_id is indexed, the query uses the index to find rows without scanning all 1 million rows.
  3. Final Answer:

    The query will use the index to quickly find matching rows. -> Option A
  4. Quick Check:

    Indexed WHERE columns speed up SELECT [OK]
Hint: WHERE on indexed columns uses index for fast lookup [OK]
Common Mistakes:
  • Thinking indexes slow down SELECT
  • Believing indexes are ignored in WHERE
  • Assuming query fails with indexes
4. You notice that inserting rows into a table is very slow after adding several indexes. What is the most likely cause?
medium
A. Indexes cause syntax errors during inserts.
B. Indexes speed up inserts, so the problem is elsewhere.
C. The table is too small for indexes to matter.
D. Indexes slow down inserts because each insert updates all indexes.

Solution

  1. Step 1: Understand index impact on inserts

    Each insert must update all indexes to keep them current, which adds overhead and slows inserts.
  2. Step 2: Identify cause of slow inserts

    Adding many indexes increases this overhead, causing slower insert performance.
  3. Final Answer:

    Indexes slow down inserts because each insert updates all indexes. -> Option D
  4. Quick Check:

    More indexes = slower inserts [OK]
Hint: More indexes mean slower inserts due to update overhead [OK]
Common Mistakes:
  • Thinking indexes speed up inserts
  • Blaming syntax errors for slow inserts
  • Ignoring index update cost
5. A table products has indexes on category_id and price. You run a query filtering on category_id and sorting by price. How can indexes help, and what might hurt performance?
hard
A. Indexes do not help filtering or sorting; they only help joins.
B. Index on category_id helps filtering; index on price helps sorting; but too many indexes slow inserts.
C. Indexes speed up inserts but slow down filtering and sorting.
D. Indexes on both columns cause syntax errors in queries.

Solution

  1. Step 1: Analyze filtering and sorting with indexes

    The index on category_id speeds up filtering rows matching that category. The index on price can speed up sorting if used properly.
  2. Step 2: Consider performance trade-offs

    Having multiple indexes helps reads but slows down inserts and updates because all indexes must be maintained.
  3. Final Answer:

    Index on category_id helps filtering; index on price helps sorting; but too many indexes slow inserts. -> Option B
  4. Quick Check:

    Indexes help filtering and sorting but slow inserts [OK]
Hint: Indexes help reads but slow writes when many exist [OK]
Common Mistakes:
  • Believing indexes only help joins
  • Thinking indexes speed up inserts
  • Assuming indexes cause syntax errors
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard