×

sql · level 3 · unit 7 · lesson 1

  1. meet it
  2. guess first
  3. your turn
  4. fix it
  5. nailed it

Tap a green word if you get lost.

meet it

A helps queries that start with its first column.

CREATE INDEX idx_depot_day ON parcels (depot, day); builds a composite index. It is sorted by depot, then by day within each depot, like a phone book sorted by surname, then first name.

It helps a search on depot, or on depot and day together. A search on day alone cannot use it, in the same way a phone book cannot find everyone called Sam. This is the leftmost column rule.

When the index holds every column a query needs, it is a , and the plan says USING COVERING INDEX.

CREATE INDEX idx_depot_day ON parcels (depot, day);

EXPLAIN QUERY PLAN
SELECT * FROM parcels WHERE depot = 'North' AND day >= '2024-07-03';

EXPLAIN QUERY PLAN
SELECT * FROM parcels WHERE day = '2024-07-03';

Wrapping a column in a function, as in lower(depot), also stops its index helping.