码迷,mamicode.com
首页 > 数据库 > 详细

【Mysql优化】索引覆盖

时间:2018-03-17 23:27:00      阅读:298      评论:0      收藏:0      [点我收藏+]

标签:ted   Nid   gpo   col   sans   post   imp   files   body   

索引覆盖

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

 

例如下面:

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优化】索引覆盖

标签:ted   Nid   gpo   col   sans   post   imp   files   body   

原文地址:https://www.cnblogs.com/qlqwjy/p/8593076.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!