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 B树数据结构允许索引快速查找特定值、一组值或一系列值,这些值对应于运算符,如在WHERE子句中的=
, >
, ≤
, BETWEEN
, IN
, and so on, in a WHERE
clause.=
、>
、≤
,BETWEEN
、IN
等。
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节,“创建索引语句”。
With 使用字符串列索引规范中的
syntax in an index specification for a string column, you can create an index that uses only the first col_name
(N
)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. BLOB
或TEXT
列编制索引时,必须为索引指定前缀长度。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字节。
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
语句中的前缀长度被解释为非二进制字符串类型(CHAR
、VARCHAR
、TEXT
)的字符数和二进制字符串类型(BINARY
、VARBINARY
、BLOB
)的字节数。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 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. InnoDB
和MyISAM
存储引擎支持全文索引,并且只支持CHAR
、VARCHAR
和TEXT
列。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:具有这些特征的查询特别有效:
仅返回文档ID或文档ID和搜索排名的全文查询。FULLTEXT
queries that only return the document ID, or the document ID and the search rank.
全文查询,按分数降序排列匹配行,并应用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(
, without any text
) AGAINST ('other_text
')> 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
,因为匹配发生在优化过程中;在这种情况下,在以后的执行过程中不需要进行表访问。
You can create indexes on spatial data types. 可以在空间数据类型上创建索引。MyISAM
and InnoDB
support R-tree indexes on spatial types. MyISAM
和InnoDB
支持空间类型的R树索引。Other storage engines use B-trees for indexing spatial types (except for 其他存储引擎使用B树对空间类型进行索引(除了不支持空间类型索引的ARCHIVE
, which does not support spatial type indexing).ARCHIVE
)。