1. 程式人生 > >【Mysql優化】索引覆蓋

【Mysql優化】索引覆蓋

des rom def 文件 ins nta style for resp

索引覆蓋

  是指 如果查詢的列恰好是索引的一部分,那麽查詢只需要在索引文件上進行,不需要回行到磁盤再找數據.這種查詢速度非常快,稱為”索引覆蓋”,比平時的查詢少一次到磁盤讀數據的操作。(索引正好覆蓋到查詢的數據)

例如下面:

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優化】索引覆蓋