Indexes and query plans
引入
6:35
数据库
数据库是关于某个领域的一组真实陈述。这门课介绍如何建模、查询、保护和治理数据。前三个单元介绍如何建模和组织数据:关系设计、SQL 和规范化。接下来的两个单元介绍如何利用索引和事务,让数据访问快速且安全。最后一个单元介绍数据治理。这里的每条查询都已在文档记录的示例模式上执行过。
英文讲解 · 可切换中英文字幕
本课内容
| English | 释义 | 拼音 |
|---|---|---|
| B-tree/biː triː/ | B树 | B shù |
| query plan/ˈkwɪərɪ plæn/ | 查询计划 | chá xún jì huà |
| fan-out/fæn aʊt/ | 扇出 | shàn chū |
| selective/sɪˈlektɪv/ | 选择性高的 | xuǎn zé xìng gāo de |
| leftmost-prefix/ˈleftməʊst ˈpriːfɪks/ | 最左前缀 | zuì zuǒ qián zhuì |
| covering index/ˈkʌvərɪŋ ˈɪndeks/ | 覆盖索引 | fù gài suǒ yǐn |
| hash index/hæʃ ˈɪndeks/ | 哈希索引 | hā xī suǒ yǐn |
有序捷径
- B树索引在平衡多路树中保持键的有序性:我们的阶数4教学树包含15个键,共2层——任何位于2节点中的键都对应15个连续条目。
- 查询计划是引擎选定的执行管道:
EXPLAIN QUERY PLAN用于打印该计划,而工作量的代价由计划本身(而非查询文本)决定。


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$ |
- In the simplified fan-out model, reaching a leaf in a million-entry tree takes about 3 node reads; fetching matching table rows may require additional reads.
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 usually quick; on 6 million rows the index may save work for a selective query, but matching table rows still need to be fetched.
EXPLAIN QUERY PLAN without an index prints —
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
The engine says what it will do: SCAN is the full-table read.
写入成本
- 每个索引都会增加每次插入和删除的开销:我们的模式带有 3 个索引,每次写入需维护 4 个结构(表 + 3 棵树)。一个拥有 5 个索引的日志表,若每日处理 10 万次插入,则每天需维护 50 万条索引条目。
- 索引你查询的内容,移除你不用的内容,并查看执行计划而非查询语句本身。
WHERE UPPER(name) = …会击败普通索引 — 计算列无法匹配。
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 efficiently servesWHERE dept = ? AND credits = ?andWHERE dept = ?. A condition oncreditsalone generally cannot use a direct leftmost-prefix lookup, although some engines may use a skip-scan. - 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 supports a direct leftmost-prefix lookup using 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.
出了什么问题?只有网站团队能看到这些信息。