简体   繁体   English

MYSQL 如何在特定索引页中找到键?

[英]How does MYSQL finds the key inside particular index page?

As I understand, MySQL stores clustered index in its custom tree-like data structure (similar to the B+-tree).据我了解,MySQL 将聚集索引存储在其自定义的树状数据结构中(类似于 B+ 树)。

The node of this tree is called "page" and may contain a significant number of keys.该树的节点称为“页面”,可能包含大量键。 Let's say 100. Suppose we are looking for key K1.假设是 100。假设我们正在寻找密钥 K1。 Using the tree we can find page P1, which contains key K1 or a pointer to the page to look further.使用树,我们可以找到页面 P1,其中包含键 K1 或指向该页面的指针以进一步查看。

But how then MYSQL finds the key or appropriate pointer to look within the same page?但是 MYSQL 如何找到要在同一页面中查找的键或适当的指针呢? Do keys inside the page stored in array and MYSQL uses binary search or are they stored in some local tree or It's just a linear scan of all keys inside the page?页面内的键是否存储在数组中,而 MYSQL 使用二分搜索还是它们存储在某个本地树中,或者它只是对页面内所有键的线性扫描?

Yes, MySQL uses a binary search inside a page, see the documentation :是的,MySQL 在页面内使用二进制搜索,请参阅文档

Page Directory页面目录

The Page Directory part of a page has a variable number of record pointers.页的页目录部分具有可变数量的记录指针。 Sometimes the record pointers are called "slots" or "directory slots".有时记录指针被称为“槽”或“目录槽”。 Unlike other DBMSs, InnoDB does not have a slot for every record in the page.与其他 DBMS 不同,InnoDB 没有用于页面中每条记录的插槽。 Instead it keeps a sparse directory.相反,它保留一个稀疏目录。 In a fullish page, there will be one slot for every six records.在完整的页面中,每六条记录将有一个槽。

The slots track the records' logical order (the order by key rather than the order by placement on the heap).槽跟踪记录的逻辑顺序(键顺序而不是堆上的放置顺序)。 Therefore, if the records are 'A''B''F''D' the slots will be (pointer to 'A') (pointer to 'B') (pointer to 'D') (pointer to 'F').因此,如果记录是'A''B''F''D',则插槽将是(指向'A'的指针)(指向'B'的指针)(指向'D'的指针)(指向'F'的指针) . Because the slots are in key order, and each slot has a fixed size, it's easy to do a binary search of the records on the page via the slots.因为槽是按键顺序排列的,而且每个槽都有固定的大小,所以很容易通过槽对页面上的记录进行二分搜索。

Since the Page Directory does not have a slot for every record, binary search can only give a rough position and then InnoDB must follow the "next" record pointers.由于页目录没有每条记录的槽,二进制搜索只能给出一个粗略的位置,然后 InnoDB 必须遵循“下一个”记录指针。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM