Understanding the B-tree and hash data structures can help predict how different queries perform on different storage engines that use these data structures in their indexes, particularly for the 了解B-树和散列数据结构有助于预测不同查询在索引中使用这些数据结构的不同存储引擎上的执行情况,特别是对于允许您选择B-树或散列索引的MEMORY
storage engine that lets you choose B-tree or hash indexes.MEMORY
存储引擎。
A B-tree index can be used for column comparisons in expressions that use the B树索引可用于使用=
, >
, >=
, <
, <=
, or BETWEEN
operators. =
、>
、>=
、<
、<=
或BETWEEN
运算符的表达式中的列比较。The index also can be used for 如果LIKE
comparisons if the argument to LIKE
is a constant string that does not start with a wildcard character. LIKE
的参数是不以通配符开头的常量字符串,则该索引也可用于LIKE
比较。For example, the following 例如,以下SELECT
statements use indexes:SELECT
语句使用索引:
SELECT * FROMtbl_name
WHEREkey_col
LIKE 'Patrick%'; SELECT * FROMtbl_name
WHEREkey_col
LIKE 'Pat%_ck%';
In the first statement, only rows with 在第一条语句中,只有带有'Patrick' <=
are considered. key_col
< 'Patricl''Patrick' <=
的行会被考虑。key_col
< 'Patricl'In the second statement, only rows with 在第二条语句中,只有带有'Pat' <=
are considered.key_col
< 'Pau''Pat' <=
的行会被考虑。key_col
< 'Pau'
The following 以下SELECT
statements do not use indexes:SELECT
语句不使用索引:
SELECT * FROMtbl_name
WHEREkey_col
LIKE '%Patrick%'; SELECT * FROMtbl_name
WHEREkey_col
LIKEother_col
;
In the first statement, the 在第一条语句中,LIKE
value begins with a wildcard character. LIKE
值以通配符开头。In the second statement, the 在第二条语句中,LIKE
value is not a constant.LIKE
值不是常量。
If you use 如果使用... LIKE '%
and string
%'string
is longer than three characters, MySQL uses the Turbo Boyer-Moore algorithm to initialize the pattern for the string and then uses this pattern to perform the search more quickly.... LIKE '%
,并且string
%'string
长于三个字符,则MySQL使用Turbo Boyer-Moore 算法来初始化用于字符串的模式,然后使用此模式来更快地执行搜索。
A search using 如果
employs indexes if col_name
IS NULLcol_name
is indexed.col_name
已编制索引,则使用
的搜索将使用索引。col_name
IS NULL
Any index that does not span all 任何不跨AND
levels in the WHERE
clause is not used to optimize the query. WHERE
子句中的所有AND
级别的索引都不会用于优化查询。In other words, to be able to use an index, a prefix of the index must be used in every 换句话说,为了能够使用索引,必须在每个AND
group.AND
组中使用索引的前缀。
The following 以下WHERE
clauses use indexes:WHERE
子句使用索引:
... WHEREindex_part1
=1 ANDindex_part2
=2 ANDother_column
=3 /*index
= 1 ORindex
= 2 */ ... WHEREindex
=1 OR A=10 ANDindex
=2 /* optimized like "index_part1
='hello'" */ ... WHEREindex_part1
='hello' ANDindex_part3
=5 /* Can use index onindex1
but not onindex2
orindex3
*/ ... WHEREindex1
=1 ANDindex2
=2 ORindex1
=3 ANDindex3
=3;
These 这些WHERE
clauses do not use indexes:WHERE
子句不使用索引:
/*index_part1
is not used */ ... WHEREindex_part2
=1 ANDindex_part3
=2 /* Index is not used in both parts of the WHERE clause */ ... WHEREindex
=1 OR A=10 /* No index spans all rows */ ... WHEREindex_part1
=1 ORindex_part2
=10
Sometimes MySQL does not use an index, even if one is available. 有时MySQL不使用索引,即使有可用的索引。One circumstance under which this occurs is when the optimizer estimates that using the index would require MySQL to access a very large percentage of the rows in the table. 出现这种情况的一种情况是,优化器估计使用索引需要MySQL访问表中很大比例的行。(In this case, a table scan is likely to be much faster because it requires fewer seeks.) (在这种情况下,表扫描可能要快得多,因为它需要更少的搜索。)However, if such a query uses 但是,如果这样的查询使用LIMIT
to retrieve only some of the rows, MySQL uses an index anyway, because it can much more quickly find the few rows to return in the result.LIMIT
只检索一些行,MySQL还是会使用索引,因为它可以更快地找到结果中要返回的几行。
Hash indexes have somewhat different characteristics from those just discussed:散列索引的特性与刚才讨论的有些不同:
They are used only for equality comparisons that use the 它们仅用于使用=
or <=>
operators (but are very fast). =
或<=>
运算符的等于比较(但速度非常快)。They are not used for comparison operators such as 它们不用于可以找到一系列值的比较运算符,如&<
that find a range of values. lt;
。Systems that rely on this type of single-value lookup are known as “key-value stores”; to use MySQL for such applications, use hash indexes wherever possible.依赖这种类型的单值查找的系统称为“键值存储”;要将MySQL用于此类应用程序,请尽可能使用哈希索引。
The optimizer cannot use a hash index to speed up 优化器不能使用散列索引来加速ORDER BY
operations. ORDER BY
操作。(This type of index cannot be used to search for the next entry in order.)(此类型的索引不能用于按顺序搜索下一个条目。)
MySQL cannot determine approximately how many rows there are between two values (this is used by the range optimizer to decide which index to use). MySQL无法确定两个值之间大约有多少行(这由范围优化器用来决定使用哪个索引)。This may affect some queries if you change a 如果将MyISAM
or InnoDB
table to a hash-indexed MEMORY
table.MyISAM
或InnoDB
表更改为哈希索引MEMORY
表,这可能会影响某些查询。
Only whole keys can be used to search for a row. 只能使用整键搜索行。(With a B-tree index, any leftmost prefix of the key can be used to find rows.)(对于B树索引,可以使用键的任何最左侧前缀查找行。)