mysql index usage

Index advantages:
  1. Indexes greatly reduce the amount of data the server needs to scan
  2. Indexes help the server avoid sorting and temporary tables
  3. Indexes can turn random I/O into sequential I/O
Explain command attribute description:
mysql> desc  select * from test where id = 1;
+----+-------------+-------+-------+---------------+---------+---------+-------+------+-------+
| id | select_type | table | type  | possible_keys | key     | key_len | ref   | rows | Extra |
+----+-------------+-------+-------+---------------+---------+---------+-------+------+-------+
|  1 | SIMPLE      | test  | const | PRIMARY       | PRIMARY | 4       | const |    1 |       |
+----+-------------+-------+-------+---------------+---------+---------+-------+------+-------+

id

SELECT identifier, which is the query sequence number of the SELECT

select_type

SELECT type, which can be any of the following:

  1. SIMPLE : Simple SELECT (without using UNION or subqueries)
  2. PRIMARY : the outermost SELECT
  3. UNION : the second or subsequent SELECT statement in a UNION
  4. DEPENDENT UNION : The second or subsequent SELECT statement in the UNION, depending on the outer query
  5. UNION RESULT : the result of the UNION
  6. SUBQUERY : The first SELECT in a subquery
  7. DEPENDENT SUBQUERY : The first SELECT in a subquery, depending on the outer query
  8. DERIVED : SELECT of the derived table (subquery of the FROM clause)

table

the table referenced by the output row

type

Join type. The various join types are given below, ordered from best to worst:

  1. system : The table has only one row (=system table). This is a special case of the const join type.
  2. const : The table has at most one matching row, which will be read at the start of the query. Since there is only one row, the column values ​​in this row can be considered constant by the rest of the optimizer. const tables are fast because they are only read once!
  3. eq_ref : For each combination of rows from the preceding table, read a row from this table. This is probably the best join type, except for const types.
  4. ref : For each combination of rows from the preceding table, all rows with matching index values ​​will be read from this table.
  5. ref_or_null : This join type is like ref, but with the addition that MySQL can specifically search for rows containing NULL values.
  6. index_merge : This join type indicates that the index merge optimization method is used.
  7. unique_subquery : This type replaces the ref of the IN subquery of the following form: value IN (SELECT primary_key FROM single_table WHERE some_expr) unique_subquery is an indexed lookup function that can completely replace subqueries with higher efficiency.
  8. index_subquery : This join type is similar to unique_subquery. IN subqueries can be replaced, but only for non-unique indexes in subqueries of the form: value IN (SELECT key_column FROM single_table WHERE some_expr)
  9. range : Retrieve only a given range of rows, using an index to select rows.
  10. index : This join type is the same as ALL, except that only the index tree is scanned. This is usually faster than ALL because index files are usually smaller than data files.
  11. ALL : For each combination of rows from the previous table, perform a full table scan.

possible_keys

Indicates which index MySQL can use to find rows in this table

key

Shows the keys (indexes) that MySQL actually decided to use. If no index is selected, the key is NULL.

key_len

Displays the key length that MySQL decided to use. If the key is NULL, the length is NULL.

ref

Shows which column or constant is used with key to select rows from the table.

rows

Displays the number of rows that MySQL thinks it must examine when executing the query. Multiplying data across multiple rows gives an estimate of the number of rows to process.

filtered

Shows a percentage estimate of the number of rows filtered by the criteria.

Extra

This column contains the details of the query being resolved by MySQL

  1. Distinct : MySQL stops searching for more rows for the current row combination after finding the first matching row.
  2. Not exists : MySQL can perform LEFT JOIN optimization on the query. After finding a row that matches the LEFT JOIN standard, it will not check more rows in the table for the previous row combination.
  3. range checked for each record (index map: #) : MySQL did not find a good index to use, but found that if the column value from the previous table is known, maybe a partial index can be used.
  4. Using filesort:MySQL需要额外的一次传递,以找出如何按排序顺序检索行。
  5. Using index:从只使用索引树中的信息而不需要进一步搜索读取实际的行来检索表中的列信息。
  6. Using temporary:为了解决查询,MySQL需要创建一个临时表来容纳结果。
  7. Using where:WHERE 子句用于限制哪一个行匹配下一个表或发送到客户。
  8. Using sort_union(...), Using union(...), Using intersect(...):这些函数说明如何为index_merge联接类型合并索引扫描。
  9. Using index for group-by:类似于访问表的Using index方式,Using index for group-by表示MySQL发现了一个索引,可以用来查 询GROUP BY或DISTINCT查询的所有列,而不要额外搜索硬盘访问实际的表。

使用索引:

  • 当表数据量很少的时候可以不使用索引,此时使用索引反而会影响速度(字典表等)
  • 查询使用group by或order by时,在该属性上建立索引
  • 联合join查询时,在关联属性上建立索引
  • 索引列尽量不要存在null值,可以使用not null创建列,因为Mysql难以优化可null列的查询,它会使索引、索引的统计信息以及比较运算更加复杂。可null列需要更多的存储空间,还需要mysql内部进行特殊处理。可null列被索引后,每条记录都需要一个额外的字节,还会导致MyIsam引擎中固定大小的索引变成可变大小的索引。
  • 使用短索引,索引应该尽量小,在字节少的列上建立索引。如果是大文本字段上需要索引,可建立前缀索引(建立索引时指定索引列的长度,如:alter table test add idx_name(name(10)),只取name的前10来创建索引,这样做加快了查询,减小了索引文件大小,insert速度得到提升)
  • like查询时注意写法,like “%zha%”不使用索引,like “zha%”使用索引(尽量不要使用like查询)
  • 不要在查询列上进行计算(如:where a+1 > 1)
  • 注意多列索引的使用,介绍如下:

B-TREE多列索引限制:

  1. 如果不是按照索引的最左列开始查找,则无法使用索引
  2. 不能跳过索引中的列查找,否则无法使用该列索引(如where c1=1 and c3=2跳过了c2,则只使用c1索引,跳过的列c3无法使用索引)
  3. 如果查询中有某个列的范围查询,则其右边所有列都无法使索引

B-TREE多列索引使用:

  • 当服务器对多个索引做相交操作时(通常为多个and),意味着需要一个包含所有相关列的多列索引,而不是多个独立的单列索引(Explain中Extra出现Using intersect(...)时即为索引交叉操作
  • 当服务器对多个索引做联合操作时(通常为多个or),需要耗费大量的CPU和内存资源在算法的缓存、排序和合并操作上。特别是当其中有些索引的选择性不高,需要合并扫描返回的大量数据的时候
  • 将选择性最高的列放到索引最前列
 

 

 

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326693304&siteId=291194637