MySQL Index Condition Pushdown

來源:互聯網
上載者:User

標籤:str   type   har   nbsp   版本   過程   ann   利用   mission   

Index Condition Pushdown (ICP)是MySQL 5.6 版本中的新特性,是一種在儲存引擎層使用索引過濾資料的一種最佳化方式。
【Index Condition Pushdown】

       當關閉ICP時,index 僅僅是data access 的一種訪問方式,儲存引擎通過索引回表擷取的資料會傳遞到MySQL Server 層進行where條件過濾。
       當開啟ICP時,如果部分where條件能使用索引中的欄位,MySQL Server 會把這部分下推到引擎層,可以利用index過濾的where條件在儲存引擎層進行資料過濾,而非將所有通過index access的結果傳遞到MySQL server層進行where過濾.
       最佳化效果:ICP能減少引擎層訪問基表的次數和MySQL Server 訪問儲存引擎的次數,減少io次數,提高查詢語句效能。

 

  • 實踐案例

      當開啟ICP時 

mysql> SET profiling = 1;Query OK, 0 rows affected, 1 warning (0.00 sec)mysql> select * from employees where first_name=‘Anneke‘ and last_name like ‘%sig‘ ;+--------+------------+------------+-----------+--------+------------+| emp_no | birth_date | first_name | last_name | gender | hire_date |+--------+------------+------------+-----------+--------+------------+| 10006  | 1953-04-20 | Anneke     | Preusig   | F      | 1989-06-02 |+--------+------------+------------+-----------+--------+------------+1 row in set (0.00 sec)mysql> show profiles;+----------+------------+--------------------------------------------------------------------------------+| Query_ID | Duration   | Query                                                                          |+----------+------------+--------------------------------------------------------------------------------+| 1        | 0.00060275 | select * from employees where first_name=‘Anneke‘ and last_name like ‘%sig‘    |+----------+------------+--------------------------------------------------------------------------------+3 rows in set, 1 warning (0.00 sec)mysql> show profile cpu,block io for query 1;+----------------------+----------+----------+------------+--------------+---------------+| Status               | Duration | CPU_user | CPU_system | Block_ops_in | Block_ops_out |+----------------------+----------+----------+------------+--------------+---------------+| starting             | 0.000094 | 0.000000 | 0.000000   | 0            | 0             || checking permissions | 0.000011 | 0.000000 | 0.000000   | 0            | 0             || Opening tables       | 0.000025 | 0.000000 | 0.000000   | 0            | 0             || init                 | 0.000044 | 0.000000 | 0.000000   | 0            | 0             || System lock          | 0.000014 | 0.000000 | 0.000000   | 0            | 0             || optimizing           | 0.000021 | 0.000000 | 0.000000   | 0            | 0             || statistics           | 0.000093 | 0.000000 | 0.000000   | 0            | 0             || preparing            | 0.000024 | 0.000000 | 0.000000   | 0            | 0             || executing            | 0.000006 | 0.000000 | 0.000000   | 0            | 0             || Sending data         | 0.000189 | 0.000000 | 0.000000   | 0            | 0             || end                  | 0.000019 | 0.000000 | 0.000000   | 0            | 0             || query end            | 0.000012 | 0.000000 | 0.000000   | 0            | 0             || closing tables       | 0.000013 | 0.000000 | 0.000000   | 0            | 0             || freeing items        | 0.000034 | 0.000000 | 0.000000   | 0            | 0             || cleaning up          | 0.000007 | 0.000000 | 0.000000   | 0            | 0             |+----------------------+----------+----------+------------+--------------+---------------+15 rows in set, 1 warning (0.00 sec)

      當關閉ICP時

實踐案例mysql> set optimizer_switch=‘index_condition_pushdown=off‘;Query OK, 0 rows affected (0.00 sec)mysql> SET profiling = 1;Query OK, 0 rows affected, 1 warning (0.00 sec)mysql> select * from employees where first_name=‘Anneke‘ and last_name like ‘%sig‘ ;+--------+------------+------------+-----------+--------+------------+| emp_no | birth_date | first_name | last_name | gender | hire_date |+--------+------------+------------+-----------+--------+------------+| 10006  | 1953-04-20 | Anneke     | Preusig   | F      | 1989-06-02 |+--------+------------+------------+-----------+--------+------------+1 row in set (0.00 sec)mysql> SET profiling = 0;Query OK, 0 rows affected, 1 warning (0.00 sec)mysql> show profiles;+----------+------------+--------------------------------------------------------------------------------+| Query_ID | Duration   | Query                                                                          |+----------+------------+--------------------------------------------------------------------------------+| 2        | 0.00097000 | select * from employees where first_name=‘Anneke‘ and last_name like ‘%sig‘    |+----------+------------+--------------------------------------------------------------------------------+6 rows in set, 1 warning (0.00 sec)mysql> show profile cpu,block io for query 2;+----------------------+----------+----------+------------+--------------+---------------+| Status               | Duration | CPU_user | CPU_system | Block_ops_in | Block_ops_out |+----------------------+----------+----------+------------+--------------+---------------+| starting             | 0.000045 | 0.000000 | 0.000000   | 0            | 0             || checking permissions | 0.000007 | 0.000000 | 0.000000   | 0            | 0             || Opening tables       | 0.000015 | 0.000000 | 0.000000   | 0            | 0             || init                 | 0.000024 | 0.000000 | 0.000000   | 0            | 0             || System lock          | 0.000009 | 0.000000 | 0.000000   | 0            | 0             || optimizing           | 0.000012 | 0.000000 | 0.000000   | 0            | 0             || statistics           | 0.000049 | 0.000000 | 0.000000   | 0            | 0             || preparing            | 0.000016 | 0.000000 | 0.000000   | 0            | 0             || executing            | 0.000005 | 0.000000 | 0.000000   | 0            | 0             || Sending data         | 0.000735 | 0.001000 | 0.000000   | 0            | 0             || end                  | 0.000008 | 0.000000 | 0.000000   | 0            | 0             || query end            | 0.000008 | 0.000000 | 0.000000   | 0            | 0             || closing tables       | 0.000009 | 0.000000 | 0.000000   | 0            | 0             || freeing items        | 0.000023 | 0.000000 | 0.000000   | 0            | 0             || cleaning up          | 0.000007 | 0.000000 | 0.000000   | 0            | 0             |+----------------------+----------+----------+------------+--------------+---------------+15 rows in set, 1 warning (0.00 sec)

  從上面的profile 可以看出ICP 開啟時整個sql 執行時間是未開啟的2/3,sending data 環節的時間消耗前者僅是後者的1/4。

       ICP 開啟時的執行計畫 含有 Using index condition 標示 ,表示最佳化器使用了ICP對資料訪問進行最佳化。 

mysql> explain select * from employees where first_name=‘Anneke‘ and last_name like ‘%nta‘ ;+----+-------------+-----------+------+---------------+--------------+---------+-------+------+-----------------------+| id | select_type | table     | type | possible_keys | key          | key_len | ref   | rows | Extra                 |+----+-------------+-----------+------+---------------+--------------+---------+-------+------+-----------------------+| 1  | SIMPLE      | employees | ref  | idx_emp_fnln  | idx_emp_fnln | 44      | const | 224  | Using index condition |+----+-------------+-----------+------+---------------+--------------+---------+-------+------+-----------------------+1 row in set (0.00 sec)

   ICP 關閉時的執行計畫顯示use where

mysql> explain select * from employees where first_name=‘Anneke‘ and last_name like ‘%nta‘ ;+----+-------------+-----------+------+---------------+--------------+---------+-------+------+-------------+| id | select_type | table     | type | possible_keys | key          | key_len | ref   | rows | Extra       |+----+-------------+-----------+------+---------------+--------------+---------+-------+------+-------------+| 1  | SIMPLE      | employees | ref  | idx_emp_fnln  | idx_emp_fnln | 44      | const | 224  | Using where |+----+-------------+-----------+------+---------------+--------------+---------+-------+------+-------------+1 row in set (0.00 sec)

  以上面的查詢為例關閉ICP 時,儲存引擎通首碼index first_name 訪問表中225條first_name 為Anneke的資料,並在MySQL server層根據last_name like ‘%sig‘ 進行過濾;開啟ICP 時,last_name 的like ‘%sig‘條件可以通過索引欄位last_name 進行過濾,在儲存引擎內部通過與where條件的對比,直接過濾掉不合格資料。該過程不回表,只訪問合格1條記錄並返回給MySQL Server ,有效減少了io訪問和各層之間的互動。

 

  • ICP的使用限制 
  1. 當sql需要全表訪問時,ICP的最佳化策略可用於range, ref, eq_ref, ref_or_null 類型的訪問資料方法 。
  2. 支援InnoDB和MyISAM表。
  3. ICP只能用於二級索引,不能用於主索引。
  4. 並非全部where條件都可以用ICP篩選。
  5. 如果where條件的欄位不在索引列中,還是要讀取整表的記錄到server端做where過濾。
  6. ICP的加速效果取決於在儲存引擎內通過ICP篩選掉的資料的比例。
  7. 5.6 版本的不支援分表的ICP 功能,5.7 版本的開始支援。
  8. 當sql 使用覆蓋索引時,不支援ICP 最佳化方法。 
mysql> explain select * from employees where first_name=‘Anneke‘ and last_name=‘Porenta‘ ;+----+-------------+-----------+------+---------------+--------------+---------+-------------+------+-----------------------+| id | select_type | table     | type | possible_keys | key          | key_len | ref         | rows | Extra                 |+----+-------------+-----------+------+---------------+--------------+---------+-------------+------+-----------------------+| 1  | SIMPLE | employees      | ref  | idx_emp_fnln  | idx_emp_fnln | 94      | const,const | 1    | Using index condition |+----+-------------+-----------+------+---------------+--------------+---------+-------------+------+-----------------------+1 row in set (0.00 sec)mysql> explain select first_name,last_name from employees where first_name=‘Anneke‘ and last_name=‘Porenta‘ ;+----+-------------+-----------+------+---------------+--------------+---------+-------------+------+--------------------------+| id | select_type | table     | type | possible_keys | key          | key_len | ref         | rows | Extra                    |+----+-------------+-----------+------+---------------+--------------+---------+-------------+------+--------------------------+| 1  | SIMPLE      | employees | ref  | idx_emp_fnln  | idx_emp_fnln | 94      | const,const | 1    | Using where; Using index |+----+-------------+-----------+------+---------------+--------------+---------+-------------+------+--------------------------+1 row in set (0.00 sec)

  

  

 

MySQL Index Condition Pushdown

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.