MySQL-5.7-8.3.1 How MySQL Uses Indexes

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 KEYUNIQUEINDEX, and FULLTEXT) are stored in B-trees. Exceptions: Indexes on spatial data types use R-trees; MEMORY tables also support hash indexesInnoDB uses inverted lists for FULLTEXT indexes.

大多数MySQL索引(PRIMARY KEY、UNIQUE、INDEX和FULLTEXT)存储在b -tree中。异常:空间数据类型的索引使用r -树;MEMORY表也支持hash索引;InnoDB使用倒排表作为FULLTEXT索引。

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.8, “Comparison of B-Tree and Hash Indexes”.

特定于哈希索引(如在MEMORY表中使用的)的特征在8.3.8节“B-Tree和哈希索引的比较”中描述。

MySQL uses indexes for these operations:

MySQL对这些操作使用索引:

To find the rows matching a WHERE clause quickly.

快速查找与WHERE子句匹配的行。

To eliminate rows from consideration. If there is a choice between multiple indexes, MySQL normally uses the index that finds the smallest number of rows (the most selective index).

从考虑中排除行。如果要在多个索引之间进行选择,MySQL通常使用查找最小行数的索引(最具选择性的索引)。

If the table has a multiple-column index, any leftmost prefix of the index can be used by the optimizer to look up rows.

如果表有多列索引,那么优化器可以使用索引最左边的任何前缀来查找行。

For example, if you have a three-column index on (col1, col2, col3), you have indexed search capabilities on (col1)(col1, col2), and (col1, col2, col3).

例如,如果在(col1, col2, col3)上有一个三列索引,则在(col1)、(col1, col2)和(col1, col2, col3)上有索引搜索功能。

For more information, see Section 8.3.5, “Multiple-Column Indexes”.

To retrieve rows from other tables when performing joins.

执行联接时从其他表检索行

MySQL can use indexes on columns more efficiently if they are declared as the same type and size. In this context, VARCHAR and CHAR are considered the same if they are declared as the same size.

如果列声明为相同的类型和大小,MySQL可以更有效地使用列上的索引。在这种情况下,如果VARCHAR和CHAR声明为相同的大小,则认为它们是相同的。

For example, VARCHAR(10) and CHAR(10) are the same size, but VARCHAR(10) and CHAR(15) are not.

例如,VARCHAR(10)和CHAR(10)的大小相同,但VARCHAR(10)和CHAR(15)的大小不同。

For comparisons between nonbinary string columns, both columns should use the same character set. For example, comparing a utf8 column with a latin1 column precludes use of an index.

对于非二进制字符串列之间的比较,两个列应该使用相同的字符集。例如,比较utf8列和latin1列就排除了索引的使用。

Comparison of dissimilar columns (comparing a string column to a temporal or numeric column, for example) may prevent use of indexes if values cannot be compared directly without conversion.

如果不进行转换就不能直接比较值,那么对不同列进行比较(例如,将字符串列与临时列或数字列进行比较)可能会阻止索引的使用。

For a given value such as 1 in the numeric column, it might compare equal to any number of values in the string column such as '1'' 1''00001', or '01.e1'. This rules out use of any indexes for the string column.

对于一个给定的值,比如数字列中的1,它可能会与字符串列中的任意数量的值进行比较,比如'1'、'1'、'00001'或'01.e1'。这就排除了对字符串列使用任何索引。

To find the MIN() or MAX() value for a specific indexed column key_col.

查找指定索引列key_col的MIN()或MAX()值。

This is optimized by a preprocessor that checks whether you are using WHERE key_part_N = constant on all key parts that occur before key_col in the index.

这是由一个预处理器优化的,该预处理器检查您是否在索引中key_col之前的所有关键部分上使用WHERE key_part_N = constant。

In this case, MySQL does a single key lookup for each MIN() or MAX() expression and replaces it with a constant. If all expressions are replaced with constants, the query returns at once.

在这种情况下,MySQL对每个MIN()或MAX()表达式执行一个键查找,并用一个常量替换它。如果所有表达式都被常量替换,查询将立即返回。

For example:

 

To sort or group a table if the sorting or grouping is done on a leftmost prefix of a usable index (for example, ORDER BY key_part1key_part2). If all key parts are followed by DESC, the key is read in reverse order.

如果排序或分组是在可用索引(例如,ORDER BY key_part1, key_part2)的最左边的前缀上完成的,则对表进行排序或分组。如果所有关键字后面都跟着DESC,则按相反的顺序读取关键字。

See Section 8.2.1.14, “ORDER BY Optimization”, and Section 8.2.1.15, “GROUP BY Optimization”.

In some cases, a query can be optimized to retrieve values without consulting the data rows.

在某些情况下,可以优化查询以在不查询数据行的情况下检索值。

(An index that provides all the necessary results for a query is called a covering index.) If a query uses from a table only columns that are included in some index, the selected values can be retrieved from the index tree for greater speed:

(为查询提供所有必要结果的索引称为覆盖索引。)如果一个查询只从一个表中使用包含在某个索引中的列,则可以从索引树中检索所选的值,以获得更高的速度:

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.20, “Avoiding Full Table Scans” for details. 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值