SQL & Databases·Data in practice

Indexes and query plans

Read how the engine executes a query and add the index that helps.

Beginner 13 min read Lesson 14 of 480 Challenge included
What you will be able to do
  • Read EXPLAIN QUERY PLAN
  • Recognise a full table scan
  • Design a composite index

An index is a sorted structure that lets the engine find rows without reading the whole table. The query planner decides whether to use it.

indexes.sqlSQL
CREATE INDEX lessons_course_minutes_idx
ON lessons (course_slug, minutes);

EXPLAIN QUERY PLAN
SELECT title, minutes
FROM lessons
WHERE course_slug = 'css' AND minutes > 12
ORDER BY minutes DESC;
  • SEARCH means the engine used an index to narrow the rows.
  • SCAN means it read the table, which is fine for small tables.
  • A composite index should list equality columns first, then the sort column.
  • Indexes speed reads but add work to every insert, update and delete.
Your turn

Index for course and minutes

Easy

Create a composite index that speeds up filtering by course_slug and sorting by minutes.

SQL · starter
CREATE INDEX
Check your understanding before moving on.
Knowledge checkQuestion 1

What does EXPLAIN QUERY PLAN describe?

Select an answer to continue
Study tools

Make it yours.

Keep a private note for this lesson, then turn the key ideas into flashcards you can revisit.

Sign in to save notes and flashcards.Both are private to your account and stay with your progress.
Sign in
Finished reading?

Lock in what you just learned.

Mark this lesson complete to save your place and move one step closer to the certificate.

Next: Changing data safely
Lesson 14 of 480 · 0% of course