【Mysql最佳化】索引覆蓋

來源:互聯網
上載者:User

標籤: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最佳化】索引覆蓋

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.