0
0
MySQLquery~20 mins

Why indexes speed up queries in MySQL - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Index Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
How do indexes improve query speed?

Imagine you have a huge phone book and you want to find a person's phone number. Which method is faster?

  1. Looking at every name one by one.
  2. Using the alphabetical index to jump directly to the name.

How does this relate to database indexes?

AIndexes copy the entire database into memory for faster access.
BIndexes store all data in a separate table, so queries run on smaller tables.
CIndexes let the database jump directly to the data instead of checking every row.
DIndexes delete unnecessary data to make queries faster.
Attempts:
2 left
💡 Hint

Think about how you find a word in a dictionary quickly.

query_result
intermediate
2:00remaining
Query speed with and without index

Given a table users(id INT, name VARCHAR(100)) with 1 million rows, which query will run faster?

SELECT * FROM users WHERE name = 'Alice';

Assuming there is an index on name, which option is true about the query speed?

AThe query with the index on <code>name</code> will be much faster than without it.
BThe query speed is the same with or without the index.
CThe query without the index will be faster because indexes slow down reads.
DThe query will fail if there is an index on <code>name</code>.
Attempts:
2 left
💡 Hint

Indexes help find matching rows quickly.

📝 Syntax
advanced
2:00remaining
Identify the correct syntax to create an index

Which SQL statement correctly creates an index on the email column of the customers table?

ACREATE INDEX idx_email ON customers(email);
BCREATE customers INDEX ON email;
CCREATE INDEX customers_email email;
DINDEX CREATE email ON customers;
Attempts:
2 left
💡 Hint

Remember the order: CREATE INDEX <index_name> ON <table>(<column>);

optimization
advanced
2:00remaining
Choosing the best index for a query

You have a table orders(order_id, customer_id, order_date). You often run this query:

SELECT * FROM orders WHERE customer_id = 123 AND order_date = '2024-01-01';

Which index will speed up this query the most?

AAn index only on customer_id
BA composite index on (customer_id, order_date)
CAn index only on order_date
DNo index is needed for this query
Attempts:
2 left
💡 Hint

Think about which columns appear together in the WHERE clause.

🔧 Debug
expert
2:00remaining
Why does this query not use the index?

Given a table products(id, name, price) with an index on price, why does this query not use the index?

SELECT * FROM products WHERE price + 10 > 100;
ABecause the index only works with equality conditions, not inequalities.
BBecause the index on price is corrupted and unusable.
CBecause the query syntax is invalid and causes an error.
DBecause the condition modifies the indexed column with an expression, the index cannot be used.
Attempts:
2 left
💡 Hint

Indexes work best when the column is used directly in conditions.