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

How does MYSQL finds the key inside particular index page?

据我了解,MySQL 在其自定义树状数据结构(类似于 B+ 树)中存储聚簇索引。

这棵树的节点称为“页面”,可能包含大量键。比方说 100。假设我们正在寻找密钥 K1。使用树我们可以找到页面 P1,其中包含键 K1 或指向页面的指针以进一步查看。

但是 MYSQL 如何找到要在同一页面内查找的键或适当的指针?页面内的键是否存储在数组中并且 MYSQL 使用二进制搜索,或者它们是否存储在某个本地树中,或者它只是对页面内所有键的线性扫描?

是的,MySQL 在页面内使用二进制搜索,参见 the documentation:

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. 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'). 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.