LUCENT GRID
SQL Lab ยท Index Tuning โ€” Medium
Loading transactions table (500,000 rows)...
๐Ÿ–ฅ๏ธ
SQL Terminal
โšก
Query Profiler
๐Ÿ”
EXPLAIN Output
๐Ÿ“Œ OBJECTIVES
โ—‹Run EXPLAIN on slow query
โ—‹Identify full table scan
โ—‹Create index on amount column
โ—‹Create composite index
โ—‹Verify <200ms execution time
โ—‹Document findings
0 / 6 complete
SQL TERMINAL โ€” analytics_db.transactions
MySQL 8.0 โ€” analytics_db Table: transactions (500,000 rows) โš  Dashboard queries timing out. Investigate and optimise. โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
mysql>
QUERY PROFILER โ€” execution time
Run queries to see execution times. Target: under 200ms.
SELECT with full table scan (no index)4,200ms
Scanning all 500,000 rows โ€” type: ALL
SELECT with single-column indexโ€”
Create an index to unlock this result
SELECT with composite indexโ€”
Create a composite index to unlock this result
EXPLAIN OUTPUT
HOW TO READ EXPLAIN
type: ALL โ€” full table scan (bad)
type: ref โ€” index lookup (good)
type: range โ€” index range scan (good)
rows โ€” estimated rows examined
key โ€” which index was used (NULL = none)
Extra โ€” additional info about the plan
Run EXPLAIN SELECT ... to see the query plan here.
โ˜ฐ BRIEF
๐Ÿ–ฅ Terminal
โšก Profiler
๐Ÿ” EXPLAIN
MISSION BRIEF

MISSION BRIEFING

INDEX TUNING โ€” APEX ANALYTICS

SCENARIO

The Apex Analytics dashboard has been timing out. The culprit is a query on the transactions table โ€” 500,000 rows, no indexes. Your job is to diagnose the query plan with EXPLAIN, identify the bottleneck, and create the right indexes to bring execution time under 200ms.

THE SLOW QUERY

SELECT * FROM transactions WHERE amount > 1000 AND status = 'completed' ORDER BY created_at DESC LIMIT 50;

Currently taking ~4,200ms. Unacceptable for a dashboard.

COMMANDS

  • EXPLAIN SELECT * FROM transactions WHERE amount > 1000;
  • SHOW INDEX FROM transactions;
  • CREATE INDEX idx_amount ON transactions(amount);
  • CREATE INDEX idx_status_amount ON transactions(status, amount);
  • CREATE INDEX idx_status_amount_date ON transactions(status, amount, created_at);
  • EXPLAIN ANALYZE SELECT ...;
  • DROP INDEX idx_name ON transactions;

TIP

A composite index on (status, amount) will be more effective than individual indexes because MySQL can use it for both the WHERE clause and avoid a filesort. Try both and compare the profiler results.

Display Mode
LAB COMPLETE
๐Ÿ†
QUERY OPTIMISED
Index Tuning โ€” Apex Analytics
โšก
+100 XP
EXPERIENCE EARNED
โฌ† LEVEL UP โ€” Level
โ†’ Return to Labs