8.3.1 How MySQL Uses IndexesMySQL如何使用索引

Indexes are used to find rows with specific column values quickly. 索引用于快速查找具有特定列值的行。Without an index, MySQL must begin with the first row and then read through the entire table to find the relevant rows. 如果没有索引,MySQL必须从第一行开始,然后通读整个表以找到相关的行。The larger the table, the more this costs. 桌子越大,花费就越多。If the table has an index for the columns in question, MySQL can quickly determine the position to seek to in the middle of the data file without having to look at all the data. 如果表中的列具有索引,MySQL可以快速确定要在数据文件中间寻找的位置,而不必查看所有数据。This is much faster than reading every row sequentially.这比按顺序读取每一行要快得多。

Most MySQL indexes (PRIMARY KEY, UNIQUE, INDEX, and FULLTEXT) are stored in B-trees. 大多数MySQL索引(主键索引、惟一性索引、索引和全文索引)都存储在B树中。Exceptions: Indexes on spatial data types use R-trees; MEMORY tables also support hash indexes; InnoDB uses inverted lists for FULLTEXT indexes.例外情况:空间数据类型的索引使用R-树;内存表也支持哈希索引;InnoDB使用倒排列表作为全文索引。

In general, indexes are used as described in the following discussion. 一般来说,索引的使用方法如下所述。Characteristics specific to hash indexes (as used in MEMORY tables) are described in Section 8.3.9, “Comparison of B-Tree and Hash Indexes”.第8.3.9节,“B树和哈希索引的比较”中描述了哈希索引(在内存表中使用)的特定特征。

MySQL uses indexes for these operations:MySQL使用索引执行以下操作:

Indexes are less important for queries on small tables, or big tables where report queries process most or all of the rows. 索引对于小表或报表查询处理大部分或所有行的大表的查询不太重要。When a query needs to access most of the rows, reading sequentially is faster than working through an index. 当查询需要访问大多数行时,按顺序读取比遍历索引要快。Sequential reads minimize disk seeks, even if not all the rows are needed for the query. 即使查询不需要所有的行,顺序读取也可以最小化磁盘查找。See Section 8.2.1.23, “Avoiding Full Table Scans” for details.详见第8.2.1.23节,“避免全表扫描”