Understanding When Indexes Help and When They Hurt
📖 Scenario: You are managing a small online bookstore database. You want to learn how adding indexes can speed up searching for books by title but also understand when indexes might slow down data changes like adding new books.
🎯 Goal: Build a simple books table, add an index on the title column, run a query using the index, then see how inserting a new book is affected by the index.
📋 What You'll Learn
Create a table called books with columns id (integer primary key) and title (text).
Create an index called idx_title on the title column of the books table.
Write a query to select all books where the title contains the word 'SQL'.
Insert a new book into the books table and observe the presence of the index.
💡 Why This Matters
🌍 Real World
Indexes are used in real databases to speed up searching for data, like finding books by title quickly in an online store.
💼 Career
Database administrators and developers use indexes to optimize query performance and understand trade-offs when inserting or updating data.
Progress0 / 4 steps
1
Create the books table
Create a table called books with two columns: id as an integer primary key and title as text.
SQL
Hint
Use CREATE TABLE with id INTEGER PRIMARY KEY and title TEXT.
2
Add an index on the title column
Create an index called idx_title on the title column of the books table.
SQL
Hint
Use CREATE INDEX idx_title ON books(title); to add the index.
3
Query books using the index
Write a query to select all columns from books where the title contains the word 'SQL'. Use the LIKE operator with '%SQL%'.
SQL
Hint
Use SELECT * FROM books WHERE title LIKE '%SQL%'; to find books with 'SQL' in the title.
4
Insert a new book and understand index impact
Insert a new book with id 1 and title 'Learning SQL Basics' into the books table. This shows how indexes exist while adding data.
SQL
Hint
Use INSERT INTO books (id, title) VALUES (1, 'Learning SQL Basics'); to add the book.
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
Step 1: Understand what indexes do
Indexes help the database find rows faster by creating a quick lookup structure on columns.
Step 2: Identify when this is useful
Searching rows by column values is faster with indexes, but inserts or deletes can slow down.
Final Answer:
When you want to quickly find rows based on column values. -> Option C
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
Step 1: Recall correct CREATE INDEX syntax
The correct syntax is: CREATE INDEX index_name ON table_name(column_name);
Step 2: Match the options to syntax
CREATE INDEX idx_email ON users(email); matches the correct syntax exactly.
Final Answer:
CREATE INDEX idx_email ON users(email); -> Option A
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
Step 1: Understand index usage in WHERE clause
Indexes help the database quickly locate rows matching the WHERE condition on indexed columns.
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.
Final Answer:
The query will use the index to quickly find matching rows. -> Option A
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
Step 1: Understand index impact on inserts
Each insert must update all indexes to keep them current, which adds overhead and slows inserts.
Step 2: Identify cause of slow inserts
Adding many indexes increases this overhead, causing slower insert performance.
Final Answer:
Indexes slow down inserts because each insert updates all indexes. -> Option D
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
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.
Step 2: Consider performance trade-offs
Having multiple indexes helps reads but slows down inserts and updates because all indexes must be maintained.
Final Answer:
Index on category_id helps filtering; index on price helps sorting; but too many indexes slow inserts. -> Option B
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 unknownstrongwhile earning +1 Grip toward your certificate.