Mysql优化索引覆盖

Posted Qiao_Zhi

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Mysql优化索引覆盖相关的知识,希望对你有一定的参考价值。

索引覆盖

  是指 如果查询的列恰好是索引的一部分,那么查询只需要在索引文件上进行,不需要回行到磁盘再找数据.这种查询速度非常快,称为”索引覆盖”,比平时的查询少一次到磁盘读数据的操作。(索引正好覆盖到查询的数据)

 

例如下面:

mysql> use exam9;
Database changed
mysql> desc options;
+----------------+---------------+------+-----+---------+-------+
| Field          | Type          | Null | Key | Default | Extra |
+----------------+---------------+------+-----+---------+-------+
| optionId       | varchar(40)   | NO   | PRI | NULL    |       |
| questionId     | varchar(40)   | YES  | MUL | NULL    |       |
| optionContent  | varchar(2000) | YES  |     | NULL    |       |
| optionWithTag  | varchar(2000) | YES  |     | NULL    |       |
| optionSequence | varchar(2)    | YES  |     | NULL    |       |
| isAnswer       | varchar(2)    | YES  |     | NULL    |       |
| description    | varchar(300)  | YES  |     | NULL    |       |
+----------------+---------------+------+-----+---------+-------+
7 rows in set (0.00 sec)

mysql> reset query cache;      #清空缓存
Query OK, 0 rows affected (0.00 sec)

mysql> set profiling=on;    #打开profiling功能
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> select optioncontent from options where optionid=‘000406aa1b89461d8cfd85f
b0e5d9e01‘;
+------------------------+
| optioncontent          |
+------------------------+
| 基础工程完毕进行回填后 |
+------------------------+
1 row in set (0.03 sec)

mysql> select optionid from options where optionid=‘000406aa1b89461d8cfd85fb0e5d
9e01‘;
+----------------------------------+
| optionid                         |
+----------------------------------+
| 000406aa1b89461d8cfd85fb0e5d9e01 |
+----------------------------------+
1 row in set (0.03 sec)

mysql> show profiles;  #显示概要信息
+----------+------------+-------------------------------------------------------
------------------------------+
| Query_ID | Duration   | Query
                              |
+----------+------------+-------------------------------------------------------
------------------------------+
|        1 | 0.03480675 | select optioncontent from options where optionid=‘0004
06aa1b89461d8cfd85fb0e5d9e01‘ |
|        2 | 0.03624525 | select optionid from options where optionid=‘000406aa1
b89461d8cfd85fb0e5d9e01‘      |
+----------+------------+-------------------------------------------------------
------------------------------+
2 rows in set, 1 warning (0.00 sec)

mysql> explainselect optionid from options where optionid=‘000406aa1b89461d8cfd8
5fb0e5d9e01‘\G
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use near ‘expla
inselect optionid from options where optionid=‘000406aa1b89461d8cfd85fb0e5d‘ at
line 1
mysql> explain select optionid from options where optionid=‘000406aa1b89461d8cfd
85fb0e5d9e01‘\G    #explain分析语句
*************************** 1. row ***************************
           id: 1
  select_type: SIMPLE
        table: options
   partitions: NULL
         type: const
possible_keys: PRIMARY
          key: PRIMARY
      key_len: 122
          ref: const
         rows: 1
     filtered: 100.00
        Extra: Using index  #表示索引覆盖
1 row in set, 1 warning (0.03 sec)

mysql> explain select optioncontent from options where optionid=‘000406aa1b89461
d8cfd85fb0e5d9e01‘\G    #分析语句
*************************** 1. row ***************************
           id: 1
  select_type: SIMPLE
        table: options
   partitions: NULL
         type: const
possible_keys: PRIMARY
          key: PRIMARY
      key_len: 122
          ref: const
         rows: 1
     filtered: 100.00
        Extra: NULL
1 row in set, 1 warning (0.03 sec)

 

以上是关于Mysql优化索引覆盖的主要内容,如果未能解决你的问题,请参考以下文章

mysql--索引优化

MySQL索引优化

MySQL索引优化

MySQL索引优化

Mysql性能优化之覆盖索引

MYSQL性能调优05_覆盖索引索引下推如何选择合适的索引Order by与Group by优化索引设计原则