Consider a table Employees with 1 million rows. It has a column LastName without an index. You run this query:
SELECT * FROM Employees WHERE LastName = 'Smith';
Now, an index is added on LastName. What is the most likely effect on the query execution?
Think about how indexes help find data quickly like a book's index helps find pages fast.
Indexes speed up searches by allowing the database to jump directly to matching rows instead of scanning all rows.
Why can adding indexes to a table slow down inserting new rows?
Think about what happens behind the scenes when you add a new row and the index exists.
When inserting, the database updates all indexes on the table to keep them accurate, which adds extra work.
Which SQL statement correctly creates an index named idx_lastname on the LastName column of the Employees table?
Remember the order: CREATE INDEX <name> ON <table> (<column>);
The correct syntax is CREATE INDEX index_name ON table_name (column_name);
You have a table Orders with an index on OrderDate. You run this query:
EXPLAIN SELECT * FROM Orders WHERE OrderDate = '2023-01-01';
What is the expected behavior in the query plan?
EXPLAIN shows how the database plans to execute the query, including index usage.
When an index exists on the filtered column, the query plan usually shows an index scan to speed up the search.
You created an index on the Email column of the Users table. But this query is still slow:
SELECT * FROM Users WHERE Email LIKE '%@example.com';
Why is the index not helping?
Think about how indexes work with string patterns and wildcards.
Indexes cannot be used efficiently when the search pattern starts with a wildcard, as the database cannot jump to a starting point.