Bird
Raised Fist0
MySQLquery~20 mins

Why indexes speed up queries in MySQL - Challenge Your Understanding

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

Practice

(1/5)
1. Why do indexes speed up queries in MySQL?
easy
A. They store all data in memory for faster access.
B. They create shortcuts to quickly find data without scanning the whole table.
C. They compress the data to reduce size.
D. They automatically backup the database.

Solution

  1. Step 1: Understand what an index does

    An index works like a shortcut or a map to find data quickly instead of checking every row.
  2. Step 2: Compare with full table scan

    Without an index, MySQL must look at every row, which is slow for big tables.
  3. Final Answer:

    They create shortcuts to quickly find data without scanning the whole table. -> Option B
  4. Quick Check:

    Indexes = shortcuts to data [OK]
Hint: Think of index as a book's index for fast lookup [OK]
Common Mistakes:
  • Confusing indexes with data compression
  • Thinking indexes store all data in memory
  • Believing indexes backup data automatically
2. Which of the following is the correct syntax to create an index on the column email in a table users?
easy
A. CREATE INDEX ON users email_index(email);
B. INDEX CREATE email_index ON users(email);
C. CREATE users INDEX email_index(email);
D. CREATE INDEX email_index ON users(email);

Solution

  1. Step 1: Recall the correct CREATE INDEX syntax

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

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

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

    CREATE INDEX index_name ON table(column) [OK]
Hint: Remember: CREATE INDEX name ON table(column) [OK]
Common Mistakes:
  • Swapping keywords order
  • Missing ON keyword
  • Placing index name after table name incorrectly
3. Given the table products(id INT, name VARCHAR(50), price DECIMAL) with an index on price, what will be the effect of this query?
EXPLAIN SELECT * FROM products WHERE price = 100;
medium
A. The query will use the index on price to find matching rows quickly.
B. The query will scan the entire table ignoring the index.
C. The query will cause a syntax error due to missing index hint.
D. The query will lock the entire table during execution.

Solution

  1. Step 1: Understand EXPLAIN output with index

    When a column has an index and the query filters on that column, MySQL uses the index to speed up search.
  2. Step 2: Analyze query behavior

    The query filters by price = 100, so the index on price helps find rows without scanning all.
  3. Final Answer:

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

    Index on filtered column = faster search [OK]
Hint: Filter on indexed column uses index automatically [OK]
Common Mistakes:
  • Assuming index is ignored without hints
  • Thinking syntax error occurs without hints
  • Believing table locks always happen on SELECT
4. You created an index on column username but your query SELECT * FROM users WHERE username = 'alice'; is still slow. What could be the problem?
medium
A. The column username is not used in the WHERE clause.
B. The query syntax is incorrect and causes slow execution.
C. The index was not created properly or is corrupted.
D. Indexes slow down all queries by default.

Solution

  1. Step 1: Check if index exists and is valid

    If the index was not created correctly or got corrupted, MySQL won't use it, causing slow queries.
  2. Step 2: Verify query and column usage

    The query syntax is correct and uses the indexed column, so syntax or column usage is not the issue.
  3. Final Answer:

    The index was not created properly or is corrupted. -> Option C
  4. Quick Check:

    Index missing or broken = slow query [OK]
Hint: Check index existence if query is slow on indexed column [OK]
Common Mistakes:
  • Blaming query syntax when it's correct
  • Thinking indexes always slow queries
  • Ignoring that column is used in WHERE clause
5. You have a large table orders with columns order_id, customer_id, and order_date. You often run this query:
SELECT * FROM orders WHERE customer_id = 123 AND order_date > '2023-01-01';
Which indexing strategy will speed up this query best?
hard
A. Create a composite index on (customer_id, order_date).
B. Create separate indexes on customer_id and order_date.
C. Create an index only on order_date.
D. No index is needed; full table scan is faster.

Solution

  1. Step 1: Understand composite index benefits

    A composite index on (customer_id, order_date) helps MySQL filter rows by both columns efficiently in one index.
  2. Step 2: Compare with separate indexes

    Separate indexes may be used but are less efficient than a composite index for combined conditions.
  3. Final Answer:

    Create a composite index on (customer_id, order_date). -> Option A
  4. Quick Check:

    Composite index matches multi-column WHERE best [OK]
Hint: Use composite index for multi-column WHERE filters [OK]
Common Mistakes:
  • Creating only single-column indexes for multi-column queries
  • Assuming full table scan is faster on large tables
  • Ignoring order of columns in composite index
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