Most MySQL indexes (PRIMARY KEY
, UNIQUE
, INDEX
, and FULLTEXT
) are stored in B-trees. Exceptions: Indexes on spatial data types use R-trees; MEMORY
tables also support hash indexes; InnoDB
uses inverted lists for FULLTEXT
indexes
B-Tree
A tree data structure that is popular for use in database indexes. The structure is kept sorted at all times, enabling fast lookup for exact matches (equals operator) and ranges (for example, greater than, less than, and BETWEEN
operators). This type of index is available for most storage engines, such as InnoDB
and MyISAM
.
Because B-tree nodes can have many children, a B-tree is not the same as a binary tree, which is limited to 2 children per node.
Contrast with hash index, which is only available in the MEMORY
storage engine. The MEMORY
storage engine can also use B-tree indexes, and you should choose B-tree indexes for MEMORY
tables if some queries use range operators.
B-tree index: comparison of columns that can be used in regular expressions , , , , , or operators. If the argument to is not a wildcard to the beginning of the constant string, the index can also be used for comparison.=
>
>=
<
<=
BETWEEN
LIKE
LIKE
Hash index: only used for equality comparisons, fast.
MySQL use indexes to:
-
WHERE
Quick Find clause matching rows . -
Eliminate rows from consideration. If you can choose between multiple indexes, MySQL is often used to find the minimum number of rows index (the most selective index).
-
If the table has a multi-column index, the optimizer can use any leftmost prefix of the index to find the row. For example, if you have a three-column index
(col1, col2, col3)
, you have indexed search capabilities(col1)
,(col1, col2)
as well(col1, col2, col3)
. For more information, see Section 8.3.5, "Multi-column index" . -
Retrieve a row from another table when performing the coupling. If declared the same type and size, MySQL can be more effectively used in the index column. In this case, and is considered to be the same if they are declared as the same size. For example, and they are the same size, however , and not.
VARCHAR
CHAR
VARCHAR(10)
CHAR(10)
VARCHAR(10)
CHAR(15)
For the comparison between the non-binary string column, two columns use the same character set. For example, a pair
utf8
Lie to alatin1
Lie were more likely to preclude the use of the index.If not, without converting direct comparison value, the comparator different columns (e.g., columns string is compared with the time or number column) may prevent the use of the index. For a given value, as
1
in the sequence of numbers, it may be more columns in the string is equal to, for example, any number of values'1'
, ,' 1'
,'00001'
or'01.e1'
. This precludes the possibility of using any index of a string column. -
Finding a particular index column of or value . This is optimized by a preprocessor, the preprocessor Check if you are using the index of all key parts appeared before . In this case, MySQL for each expression or expression once key lookup, and replace it with a constant. If all expressions are replaced with constants, the query will return immediately. E.g:
MIN()
MAX()
key_col
WHERE
key_part_N
=constant
key_col
MIN()
MAX()
SELECT MIN(key_part2),MAX(key_part2) FROM tbl_name WHERE key_part1=10;
-
If the packet is a sort or leftmost prefix available indexes (e.g. ) completed on the table to sort or group . If all the key back part has, reads the key in reverse order. See Section 8.2.1.14 "by optimizing sorting" and section 8.2.1.15 Festival "by optimizing packet" .
ORDER BY
key_part1
,key_part2
DESC
-
In some cases, a query can be optimized to retrieve values without the need to query the data line. (All the necessary result for a query index called cover index .) If the query using only some columns in the table contained in the index, a selected value can be retrieved from the index tree, to improve speed:
SELECT key_part3 FROM tbl_name WHERE key_part1=1
Not so important for the report query the importance of addressing most or all rows of small tables or large table, index. When a query needs to access most of the rows are sequentially read faster than processing the index. Sequential read can reduce disk to find the greatest extent possible, even if the query does not need all rows. About details , please see Section 8.2.1.20 "avoid full table scan" .