Lesson 7 / الدرس 7

Ask the database what it is going to do / اسأل قاعدة البيانات ماذا ستفعل

You do not have to guess whether an index is being used. EXPLAIN puts the plan in front of you before the query runs, and three of its columns answer almost every question people spend afternoons speculating about.

لست مضطرًا إلى تخمين أيُستخدم فهرس. فـEXPLAIN تضع الخطة أمامك قبل تشغيل الاستعلام، وثلاثة من أعمدتها تجيب كل سؤال تقريبًا يقضي الناس أصائل يتكهنون فيه.

Put EXPLAIN in front of a SELECT and the database tells you how it intends to answer without answering. This is the difference between an opinion about performance and a fact about it, and it costs nothing to check — the query does not run.

EXPLAIN SELECT * FROM bookings WHERE guest_id = 42;

-- Before the index:
--   type: ALL          <- read every row
--   key:  NULL         <- no index chosen
--   rows: 4832190      <- how many it expects to examine

-- After CREATE INDEX bookings_guest ON bookings (guest_id):
--   type: ref
--   key:  bookings_guest
--   rows: 3

-- Three columns, and they are the three worth learning:
--   type  HOW it will find rows
--   key   WHICH index it picked, or NULL for none
--   rows  how many it expects to look at, not how many it will return
The rows column is an estimate, and it is the one to read first. A query that returns three rows while expecting to examine five million is the whole problem, stated in one number. You do not need to understand the rest of the output to act on that.

The access types, worst to best

typeWhat it meansVerdict
ALLRead every row in the tableA full scan. Fine on a small table, fatal on a large one
indexRead every entry in an indexStill everything, just a smaller everything
rangeWalk part of an index — BETWEEN, >, INGood. This is what most filters should be
refJump to matching entries for a valueGood. A normal indexed lookup
const / eq_refAt most one row, by primary or unique keyThe best there is

ALL on a table of any size is the finding. Everything else is a matter of degree, and chasing range up to ref is rarely worth the effort — whereas turning ALL into anything at all usually changes the page from slow to instant.

Two lines in the Extra column that matter

  • Using filesort — the database could not get the rows in the order you asked for, so it fetched them and sorted them afterwards. On a large result that is memory and time. An index that already has them in that order removes it.
  • Using index — the good one, and easy to confuse with the index access type above. It means every column needed was in the index, so the table was never touched: the covering index from the last lesson, confirmed.
  • Using temporary — a working table was built to answer, usually for a GROUP BY that no index supports. Worth knowing about, rarely the first thing to fix.

Check yourself / اختبر نفسك

1. Which single number in an EXPLAIN output is worth reading first?

2. EXPLAIN reports type: ALL. What does that mean?

3. Why is a plan from your development database not enough?