標籤:ted Nid gpo col sans post imp files body
索引覆蓋
是指 如果查詢的列恰好是索引的一部分,那麼查詢只需要在索引檔案上進行,不需要回行到磁碟再找資料.這種查詢速度非常快,稱為”索引覆蓋”,比平時的查詢少一次到磁碟讀資料的操作。(索引正好覆蓋到查詢的資料)
例如下面:
mysql> use exam9;Database changedmysql> 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=‘000406aa1b89461d8cfd85fb0e5d9e01‘;+------------------------+| optioncontent |+------------------------+| 基礎工程完畢進行回填後 |+------------------------+1 row in set (0.03 sec)mysql> select optionid from options where optionid=‘000406aa1b89461d8cfd85fb0e5d9e01‘;+----------------------------------+| 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=‘000406aa1b89461d8cfd85fb0e5d9e01‘ || 2 | 0.03624525 | select optionid from options where optionid=‘000406aa1b89461d8cfd85fb0e5d9e01‘ |+----------+------------+-------------------------------------------------------------------------------------+2 rows in set, 1 warning (0.00 sec)mysql> explainselect optionid from options where optionid=‘000406aa1b89461d8cfd85fb0e5d9e01‘\GERROR 1064 (42000): You have an error in your SQL syntax; check the manual thatcorresponds to your MySQL server version for the right syntax to use near ‘explainselect optionid from options where optionid=‘000406aa1b89461d8cfd85fb0e5d‘ atline 1mysql> explain select optionid from options where optionid=‘000406aa1b89461d8cfd85fb0e5d9e01‘\G #explain分析語句*************************** 1. row *************************** id: 1 select_type: SIMPLE table: options partitions: NULL type: constpossible_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=‘000406aa1b89461d8cfd85fb0e5d9e01‘\G #分析語句*************************** 1. row *************************** id: 1 select_type: SIMPLE table: options partitions: NULL type: constpossible_keys: PRIMARY key: PRIMARY key_len: 122 ref: const rows: 1 filtered: 100.00 Extra: NULL1 row in set, 1 warning (0.03 sec)
【Mysql最佳化】索引覆蓋