mysql 扫描所有字段_select扫描mysql innodb表时,select只输出主键列,会不会扫描全表?...
題主假定按照主鍵檢索。我們假定是等值查詢。范圍查詢和表遍歷情形可以在文末經推導得出。
primary key
A set of columns—and by implication, the index based on this set of columns—that can uniquely identify every row in a table. As such, it must be a unique index that does not contain any NULL values.
InnoDB requires that every table has such an index (also called the clustered index or cluster index), and organizes the table storage based on the column values of the primary key.
以上說明存儲引擎是按照主鍵聚簇存儲的。
With the exception of spatial indexes,InnoDBindexes are B-tree data structures. Spatial indexes use R-trees, which are specialized data structures for indexing multi-dimensional data. Index records are stored in the leaf pages of their B-tree or R-tree data structure. The default size of an index page is 16KB.
以上說明數據是按照B+樹組織的。主鍵按照邏輯索引被存儲在葉子結點上。所以我們只能繼續去那里找數據。
An InnoDB page has seven parts:
Fil Header
Page Header
Infimum + Supremum Records
User Records
Free Space
Page Directory
Fil Trailer
以上說明物理頁中沒有現成的主鍵數據簇。
Similarly,InnoDBdoes not want to insert new rows according to the B-tree's key order (that would involve expensive shifting of large amounts of data), so it inserts new rows right after the end of the existing rows (at the top of the Free Space part) or wherever there's space left by a deleted row.
以上說明用戶數據物理上在頁內形成一個堆。
But by definition the records of a B-tree must be accessible in order by key value, so there is a record pointer in each record (the "next" field in the Extra Bytes) which points to the next record in key order. In other words, the records are a one-way linked list. SoInnoDBcan access rows in key order when searching.
以上說明這個物理堆形成一個邏輯有序的鏈表。假設這個查詢可以命中。(細節) 存儲引擎通過在頁目錄中二分查找到一個大致定位。并且最終找到了這條記錄。接下來,我們要看看如何在物理記錄中找到主鍵列數據。
The chart below shows the three parts of a physical record.
NameSizeField Start Offsets(F*1) or (F*2) bytesExtra Bytes6 bytesField Contentsdepends on content
Legend: The letter 'F' stands for 'Number Of Fields'.
The meaning of the parts is as follows:
The FIELD START OFFSETS is a list of numbers containing the information "where a field starts".
The EXTRA BYTES is a fixed-size header.
The FIELD CONTENTS contains the actual data.
以上說明一個物理記錄分為三部分。乍一看我們也許需要繼續查找主鍵在什么位置。
Clustered indexes
The clustered key (PRIMARY KEY) has one of the more complex record structures:
Cluster Key Fields: The cluster key fields, concatenated together (literally). InnoDB just concatenates the raw bytes of its internal storage formats per column type together into a single byte stream.
以上說明主鍵在物理記錄中聚簇,可以直接提取。至此題主應該了解等值查詢究竟都經歷了些什么。至于范圍查詢和表遍歷,找到第一個記錄之后按照鏈表結構依次提交給游標即可。
所以主鍵的遍歷也僅僅是不用在非鍵列中再查詢一次而已。當然在索引和聚簇數據結構的幫助下,主鍵的查詢投影開銷一定是比較小的。
References:
總結
以上是生活随笔為你收集整理的mysql 扫描所有字段_select扫描mysql innodb表时,select只输出主键列,会不会扫描全表?...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ajax实现重新绘图,如何画Flot A
- 下一篇: mysql工作台安装使用_如何安装MyS