Indexes and query plans
مُقدَّمفي هذا الدرس
| English | المعنى |
|---|---|
| B-tree/biː triː/ | |
| query plan/ˈkwɪərɪ plæn/ | خطة الاستعلام |
| fan-out/fæn aʊt/ | the number of children a B-tree node has |
| leftmost-prefix/ˈleftməʊst ˈpriːfɪks/ | a composite index can be used only for searches on its first columns, in order |
| covering index/ˈkʌvərɪŋ ˈɪndeks/ | an index that holds every column a query needs, so the table itself is not read |
| selective/sɪˈlektɪv/ | (of a condition or index) matching only a small fraction of the rows |
| hash index/hæʃ ˈɪndeks/ | an index that finds an exact key in about one probe but keeps no order, so it cannot answer range queries |
The sorted shortcut
- A B-tree B树 index keeps keys sorted in a balanced tree whose nodes have many children. Our order-4 teaching tree holds 15 keys in 2 levels: any key takes 2 node reads, against up to 15 reads one by one.
- The query plan 查询计划 is the pipeline the engine chooses.
EXPLAIN QUERY PLANprints it, and the plan, not the query text, decides how much work is done.

A fan-out-100 B-tree of height 3 covers — keys
الصعوبة 1/5 · 100 عملة + مكافأة صغيرة عند الإنجاز الأول اليوم
m to the h: 100 cubed is a million - 3 reads per lookup.
Fan-out arithmetic
- A node with $k$ keys points to $k + 1$ children. With a fan-out 扇出 of about $m$, a tree of height $h$ covers $m^h$ entries. With fan-out 100:
| Levels (reads per lookup) | 1 | 2 | 3 | 4 |
|---|---|---|---|---|
| Keys covered | 100 | $10^4$ | $10^6$ | $10^8$ |
- A lookup in a million rows takes 3 reads, for every key (splits keep the tree balanced).
SELECT title FROM Course WHERE credits = 20: with no index the plan saysSCAN Course; withINDEX ON Course(credits)it saysSEARCH Course USING INDEX idx (credits=?). On 6 rows both are instant; on 6 million rows it is 3 reads against 6 million.
EXPLAIN QUERY PLAN without an index prints —
الصعوبة 1/5 · 100 عملة + مكافأة صغيرة عند الإنجاز الأول اليوم
The engine says what it will do: SCAN is the full-table read.
The write tax
- Each index adds work to every insert and delete. Our schema with 3 indexes writes 4 structures per insert (the table plus 3 trees). A log table with 5 indexes and 10 million inserts a day maintains 50 million index entries every day.
- Index what you query, drop what you do not, and read the plan, not the query.
WHERE UPPER(name) = …cannot use a plain index onname, because the computed value does not match what the index stores.
A table with 3 indexes costs each insert —
الصعوبة 1/5 · 100 عملة + مكافأة صغيرة عند الإنجاز الأول اليوم
Table + 3 trees = 4 structures: every index is a loan the writes repay.
A plain index on name lets WHERE UPPER(name) = 'ADA' search instead of scan.
الصعوبة 1/5 · 100 عملة + مكافأة صغيرة عند الإنجاز الأول اليوم
The index is sorted by name, not by UPPER(name), so the engine must compute the function on every row: SCAN. An index on the expression itself, or a stored upper-case column, fixes it.
Composite indexes and selectivity
- An index on
(dept, credits)is sorted by dept, then by credits. It servesWHERE dept = ? AND credits = ?andWHERE dept = ?, but notWHERE credits = ?alone: this is the leftmost-prefix 最左前缀 rule. - If the index also holds every column the query needs, it is a covering index 覆盖索引 and the table is never read.
- Indexes pay off for selective 选择性高的 queries. Fetching $30\%$ of the rows through an index costs a random read per row, which is slower than one sequential scan, so the planner is right to ignore the index.
A B-tree has fan-out 1000. How many levels does it need to index one billion keys?
الصعوبة 2/5 · 140 عملة + مكافأة صغيرة عند الإنجاز الأول اليوم
1000^3 = 10^9, so three levels suffice.
Which query can use an index on (dept, credits)?
الصعوبة 1/5 · 100 عملة + مكافأة صغيرة عند الإنجاز الأول اليوم
Only a condition on the leftmost column, dept, can use the sorted order.
Hash index or B-tree?
- A hash index 哈希索引 finds an exact key in about one probe, but it keeps no order. It cannot answer
credits > 15,BETWEENorORDER BY. - A B-tree needs a few more reads for an exact match, but it answers all of those. That is why the B-tree is the default, and hash indexes are used only for pure equality lookups.
A hash index speeds up WHERE credits BETWEEN 10 AND 20.
الصعوبة 1/5 · 100 عملة + مكافأة صغيرة عند الإنجاز الأول اليوم
Hashing destroys order, so ranges need a scan or a B-tree.
Worked example: how tall is a real B+-tree?
- An 8 KiB page holding 16-byte entries (an 8-byte key and an 8-byte pointer) has a fan-out of about 512. Three levels cover $512^3 \approx 1.3 \times 10^8$ keys.
- So 100 million rows need only $\mathbf{3}$ levels. With the root and the second level cached in memory, a lookup costs one disk read. The fan-out, not the number of rows, sets the height.
Fan-out 512: fewest levels that index 100 million keys?
الصعوبة 2/5 · 140 عملة + مكافأة صغيرة عند الإنجاز الأول اليوم
$512^2 \approx 2.6 \times 10^5$ is too few; $512^3 \approx 1.3 \times 10^8$ is enough.
8 KiB pages with 16-byte entries: entries per page?
الصعوبة 2/5 · 140 عملة + مكافأة صغيرة عند الإنجاز الأول اليوم
$8192/16 = 512$.
Marks that slip away
- Indexing everything. Every index adds a write to each insert: a log table with $5$ indexes writes $6$ structures per row.
- Judging by the query text. Read the plan:
SCANreads every row,SEARCH … USING INDEXwalks the tree. - Forgetting fan-out. With fan-out $100$, three levels cover $10^6$ keys. The height, not $n$, sets the number of reads.
Recap
- A B-tree with fan-out $m$ covers $m^h$ keys in $h$ levels: $100^3 = 10^6$.
- The plan decides the work: read
EXPLAIN QUERY PLAN, not the query text. - Every index adds a cost to every write; index what you query.
ما المشكلة؟ لا يرى ذلك سوى فريق الموقع.