8.3.5 Column Indexes列索引

The most common type of index involves a single column, storing copies of the values from that column in a data structure, allowing fast lookups for the rows with the corresponding column values. 最常见的索引类型是一列,将该列中的值的副本存储在数据结构中,从而可以快速查找具有相应列值的行。The B-tree data structure lets the index quickly find a specific value, a set of values, or a range of values, corresponding to operators such as =, >, , BETWEEN, IN, and so on, in a WHERE clause.B树数据结构允许索引快速查找特定值、一组值或一系列值,这些值对应于运算符,如在WHERE子句中的=>BETWEENIN等。

The maximum number of indexes per table and the maximum index length is defined per storage engine. 每个表的最大索引数和最大索引长度是按存储引擎定义的。See Chapter 15, The InnoDB Storage Engine, and Chapter 16, Alternative Storage Engines. 参见第15章,InnoDB存储引擎第16章,替代存储引擎All storage engines support at least 16 indexes per table and a total index length of at least 256 bytes. 所有存储引擎都支持每个表至少16个索引,索引总长度至少为256字节。Most storage engines have higher limits.大多数存储引擎都有更高的限制。

For additional information about column indexes, see Section 13.1.15, “CREATE INDEX Statement”.有关列索引的更多信息,请参阅第13.1.15节,“创建索引语句”

Index Prefixes索引前缀

With col_name(N) syntax in an index specification for a string column, you can create an index that uses only the first N characters of the column. 使用字符串列索引规范中的col_name(N)语法,可以创建只使用该列前N个字符的索引。Indexing only a prefix of column values in this way can make the index file much smaller. 以这种方式只索引列值的前缀可以使索引文件小得多。When you index a BLOB or TEXT column, you must specify a prefix length for the index. BLOBTEXT列编制索引时,必须为索引指定前缀长度。For example:例如:

CREATE TABLE test (blob_col BLOB, INDEX(blob_col(10)));

Prefixes can be up to 767 bytes long for InnoDB tables that use the REDUNDANT or COMPACT row format. 对于使用REDUNDANT(冗余)或COMPACT(紧凑)行格式的InnoDB表,前缀最长可达767字节。The prefix length limit is 3072 bytes for InnoDB tables that use the DYNAMIC or COMPRESSED row format. 对于使用DYNAMIC(动态)或COMPRESSED(压缩)行格式的InnoDB表,前缀长度限制为3072字节。For MyISAM tables, the prefix length limit is 1000 bytes.对于MyISAM表,前缀长度限制为1000字节。

Note注意

Prefix limits are measured in bytes, whereas the prefix length in CREATE TABLE, ALTER TABLE, and CREATE INDEX statements is interpreted as number of characters for nonbinary string types (CHAR, VARCHAR, TEXT) and number of bytes for binary string types (BINARY, VARBINARY, BLOB). 前缀限制以字节为单位,而CREATE TABLE语句、ALTER TABLE语句和CREATE INDEX语句中的前缀长度被解释为非二进制字符串类型(CHARVARCHARTEXT)的字符数和二进制字符串类型(BINARYVARBINARYBLOB)的字节数。Take this into account when specifying a prefix length for a nonbinary string column that uses a multibyte character set.当为使用多字节字符集的非二进制字符串列指定前缀长度时,请考虑这一点。

If a search term exceeds the index prefix length, the index is used to exclude non-matching rows, and the remaining rows are examined for possible matches.如果搜索项超过索引前缀长度,则使用索引排除不匹配的行,并检查其余行是否可能匹配。

For additional information about index prefixes, see Section 13.1.15, “CREATE INDEX Statement”.有关索引前缀的更多信息,请参阅第13.1.15节,“创建索引语句”

FULLTEXT Indexes全文索引

FULLTEXT indexes are used for full-text searches. FULLTEXT索引用于全文搜索。Only the InnoDB and MyISAM storage engines support FULLTEXT indexes and only for CHAR, VARCHAR, and TEXT columns. 只有InnoDBMyISAM存储引擎支持全文索引,并且只支持CHARVARCHARTEXT列。Indexing always takes place over the entire column and column prefix indexing is not supported. 索引总是发生在整个列上,不支持列前缀索引。For details, see Section 12.10, “Full-Text Search Functions”.有关详细信息,请参阅第12.10节,“全文搜索功能”

Optimizations are applied to certain kinds of FULLTEXT queries against single InnoDB tables. 优化应用于针对单个InnoDB表的特定类型的全文查询。Queries with these characteristics are particularly efficient:具有这些特征的查询特别有效:

  • FULLTEXT queries that only return the document ID, or the document ID and the search rank.仅返回文档ID或文档ID和搜索排名的全文查询。

  • FULLTEXT queries that sort the matching rows in descending order of score and apply a LIMIT clause to take the top N matching rows. 全文查询,按分数降序排列匹配行,并应用LIMIT子句获取前N个匹配行。For this optimization to apply, there must be no WHERE clauses and only a single ORDER BY clause in descending order.要应用此优化,必须没有WHERE子句,并且只有一个按降序排列的ORDER BY子句。

  • FULLTEXT queries that retrieve only the COUNT(*) value of rows matching a search term, with no additional WHERE clauses. 只检索与搜索项匹配的行的COUNT(*)值的全文查询,没有附加的WHERE子句。Code the WHERE clause as WHERE MATCH(text) AGAINST ('other_text'), without any > 0 comparison operator.WHERE子句编码为WHERE MATCH(text) AGAINST ('other_text'),不带任何>0比较运算符。

For queries that contain full-text expressions, MySQL evaluates those expressions during the optimization phase of query execution. 对于包含全文表达式的查询,MySQL会在查询执行的优化阶段评估这些表达式。The optimizer does not just look at full-text expressions and make estimates, it actually evaluates them in the process of developing an execution plan.优化器不只是查看全文表达式并进行估计,它实际上是在开发执行计划的过程中对它们进行评估。

An implication of this behavior is that EXPLAIN for full-text queries is typically slower than for non-full-text queries for which no expression evaluation occurs during the optimization phase.此行为的一个含义是,对于在优化阶段没有表达式求值的非全文查询,全文查询的EXPLAIN速度通常慢于非全文查询。

EXPLAIN for full-text queries may show Select tables optimized away in the Extra column due to matching occurring during optimization; in this case, no table access need occur during later execution.全文查询的EXPLAIN可能会在Extra列中显示Select tables optimized away,因为匹配发生在优化过程中;在这种情况下,在以后的执行过程中不需要进行表访问。

Spatial Indexes空间索引

You can create indexes on spatial data types. 可以在空间数据类型上创建索引。MyISAM and InnoDB support R-tree indexes on spatial types. MyISAMInnoDB支持空间类型的R树索引。Other storage engines use B-trees for indexing spatial types (except for ARCHIVE, which does not support spatial type indexing).其他存储引擎使用B树对空间类型进行索引(除了不支持空间类型索引的ARCHIVE)。

Indexes in the MEMORY Storage Engine内存存储引擎中的索引

The MEMORY storage engine uses HASH indexes by default, but also supports BTREE indexes.MEMORY存储引擎默认使用HASH索引,但也支持BTREE索引。