• <fieldset id="8imwq"><menu id="8imwq"></menu></fieldset>
  • <bdo id="8imwq"><input id="8imwq"></input></bdo>
    最新文章專題視頻專題問答1問答10問答100問答1000問答2000關(guān)鍵字專題1關(guān)鍵字專題50關(guān)鍵字專題500關(guān)鍵字專題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關(guān)鍵字專題關(guān)鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
    問答文章1 問答文章501 問答文章1001 問答文章1501 問答文章2001 問答文章2501 問答文章3001 問答文章3501 問答文章4001 問答文章4501 問答文章5001 問答文章5501 問答文章6001 問答文章6501 問答文章7001 問答文章7501 問答文章8001 問答文章8501 問答文章9001 問答文章9501
    當(dāng)前位置: 首頁 - 科技 - 知識百科 - 正文

    MySQLIndexConditionPushdown(ICP)性能優(yōu)化方法實(shí)例

    來源:懂視網(wǎng) 責(zé)編:小采 時(shí)間:2020-11-09 21:00:19
    文檔

    MySQLIndexConditionPushdown(ICP)性能優(yōu)化方法實(shí)例

    MySQLIndexConditionPushdown(ICP)性能優(yōu)化方法實(shí)例:一 概念介紹 Index Condition Pushdown (ICP)是MySQL 5.6 版本中的新特性,是一種在存儲引擎層使用索引過濾數(shù)據(jù)的一種優(yōu)化方式。 a 當(dāng)關(guān)閉ICP時(shí),index 僅僅是data access 的一種訪問方式,存儲引擎通過索引回表獲取的數(shù)據(jù)會傳遞到MySQL Server 層
    推薦度:
    導(dǎo)讀MySQLIndexConditionPushdown(ICP)性能優(yōu)化方法實(shí)例:一 概念介紹 Index Condition Pushdown (ICP)是MySQL 5.6 版本中的新特性,是一種在存儲引擎層使用索引過濾數(shù)據(jù)的一種優(yōu)化方式。 a 當(dāng)關(guān)閉ICP時(shí),index 僅僅是data access 的一種訪問方式,存儲引擎通過索引回表獲取的數(shù)據(jù)會傳遞到MySQL Server 層

    一 概念介紹

    Index Condition Pushdown (ICP)是MySQL 5.6 版本中的新特性,是一種在存儲引擎層使用索引過濾數(shù)據(jù)的一種優(yōu)化方式。

    a 當(dāng)關(guān)閉ICP時(shí),index 僅僅是data access 的一種訪問方式,存儲引擎通過索引回表獲取的數(shù)據(jù)會傳遞到MySQL Server 層進(jìn)行where條件過濾。

    b 當(dāng)打開ICP時(shí),如果部分where條件能使用索引中的字段,MySQL Server 會把這部分下推到引擎層,可以利用index過濾的where條件在存儲引擎層進(jìn)行數(shù)據(jù)過濾,而非將所有通過index access的結(jié)果傳遞到MySQL server層進(jìn)行where過濾.

    優(yōu)化效果:ICP能減少引擎層訪問基表的次數(shù)和MySQL Server 訪問存儲引擎的次數(shù),減少io次數(shù),提高查詢語句性能。

    二 原理

    Index Condition Pushdown is not used:

      1 Get the next row, first by reading the index tuple, and then by using the index tuple to locate and read the full table row.
      2 Test the part of the WHERE condition that applies to this table. Accept or reject the row based on the test result.
    Index Condition Pushdown is used
      1 Get the next row s index tuple (but not the full table row).
      2 Test the part of the WHERE condition that applies to this table and can be checked using only index columns.
        If the condition is not satisfied, proceed to the index tuple for the next row.
      3 If the condition is satisfied, use the index tuple to locate and read the full table row.
      4 est the remaining part of the WHERE condition that applies to this table. Accept or reject the row based on the test result.

    三 實(shí)踐案例

    a 環(huán)境準(zhǔn)備
       數(shù)據(jù)庫版本 5.6.16
       關(guān)閉緩存
       代碼如下:
         set query_cache_size=0;
         set query_cache_type=OFF;
     
       測試數(shù)據(jù)下載地址
    b 當(dāng)開啟ICP時(shí)
    代碼如下:
    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)

    此時(shí)情況下根據(jù)MySQL的最左前綴原則, first_name 可以使用索引,last_name采用了like 模糊查詢,不能使用索引。
    c 關(guān)閉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)

    當(dāng)開啟ICP時(shí) 查詢在sending data環(huán)節(jié)時(shí)間消耗是 0.000189s
    代碼如下:
    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)

    當(dāng)關(guān)閉ICP時(shí) 查詢在sending data環(huán)節(jié)時(shí)間消耗是 0.000735s
    代碼如下:
    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 開啟時(shí)整個(gè)sql 執(zhí)行時(shí)間是未開啟的2/3,sending data 環(huán)節(jié)的時(shí)間消耗前者僅是后者的1/4。
    ICP 開啟時(shí)的執(zhí)行計(jì)劃 含有 Using index condition 標(biāo)示 ,表示優(yōu)化器使用了ICP對數(shù)據(jù)訪問進(jìn)行優(yōu)化。
    代碼如下:
    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 關(guān)閉時(shí)的執(zhí)行計(jì)劃顯示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)

    案例分析

    以上面的查詢?yōu)槔P(guān)閉ICP 時(shí),存儲引擎通前綴index first_name 訪問表中225條first_name 為Anneke的數(shù)據(jù),并在MySQL server層根據(jù)last_name like '%sig' 進(jìn)行過濾
    開啟ICP 時(shí),last_name 的like '%sig'條件可以通過索引字段last_name 進(jìn)行過濾,在存儲引擎內(nèi)部通過與where條件的對比,直接過濾掉不符合條件的數(shù)據(jù)。該過程不回表,只訪問符合條件的1條記錄并返回給MySQL Server ,有效的減少了io訪問和各層之間的交互。

    ICP 關(guān)閉時(shí) ,僅僅使用索引作為訪問數(shù)據(jù)的方式。

    ICP 開啟時(shí) ,MySQL將在存儲引擎層 利用索引過濾數(shù)據(jù),減少不必要的回表,注意 虛線的using where 表示如果where條件中含有沒有被索引的字段,則還是要經(jīng)過MySQL Server 層過濾。

    四 ICP的使用限制

    1 當(dāng)sql需要全表訪問時(shí),ICP的優(yōu)化策略可用于range, ref, eq_ref,  ref_or_null 類型的訪問數(shù)據(jù)方法 。
    2 支持InnoDB和MyISAM表。
    3 ICP只能用于二級索引,不能用于主索引。
    4 并非全部where條件都可以用ICP篩選。
       如果where條件的字段不在索引列中,還是要讀取整表的記錄到server端做where過濾。
    5 ICP的加速效果取決于在存儲引擎內(nèi)通過ICP篩選掉的數(shù)據(jù)的比例。
    6 5.6 版本的不支持分表的ICP 功能,5.7 版本的開始支持。
    7 當(dāng)sql 使用覆蓋索引時(shí),不支持ICP 優(yōu)化方法。

    代碼如下:
    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服務(wù)性能優(yōu)化—my.cnf_my.ini配置說明詳解(16G內(nèi)存)
  • mysql性能優(yōu)化之索引優(yōu)化
  • 詳解MySQL性能優(yōu)化(一)
  • MySQL延遲關(guān)聯(lián)性能優(yōu)化方法
  • MySQL 5.7增強(qiáng)版Semisync Replication性能優(yōu)化
  • MySQL order by性能優(yōu)化方法實(shí)例
  • MySQL數(shù)據(jù)庫21條最佳性能優(yōu)化經(jīng)驗(yàn)
  • 聲明:本網(wǎng)頁內(nèi)容旨在傳播知識,若有侵權(quán)等問題請及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

    文檔

    MySQLIndexConditionPushdown(ICP)性能優(yōu)化方法實(shí)例

    MySQLIndexConditionPushdown(ICP)性能優(yōu)化方法實(shí)例:一 概念介紹 Index Condition Pushdown (ICP)是MySQL 5.6 版本中的新特性,是一種在存儲引擎層使用索引過濾數(shù)據(jù)的一種優(yōu)化方式。 a 當(dāng)關(guān)閉ICP時(shí),index 僅僅是data access 的一種訪問方式,存儲引擎通過索引回表獲取的數(shù)據(jù)會傳遞到MySQL Server 層
    推薦度:
    • 熱門焦點(diǎn)

    最新推薦

    猜你喜歡

    熱門推薦

    專題
    Top
    主站蜘蛛池模板: 国产精品自在在线午夜福利 | 国内揄拍高清国内精品对白| 99久久国产综合精品五月天喷水| 久久久精品人妻一区二区三区四 | 精品一区二区三区在线视频| 久久久久成人精品无码中文字幕| 亚洲精品无码成人片在线观看 | 精品国产sm捆绑最大网免费站| 久久精品亚洲乱码伦伦中文| 91麻豆精品一二三区在线| 国产亚洲精品a在线无码| 亚洲av无码国产精品色午夜字幕| 久久成人精品| 国内精品久久久久国产盗摄| 国产成人精品一区在线 | 国产精品电影在线观看| 欧美精品v欧洲精品| 尤物yw午夜国产精品视频| 欧美亚洲综合免费精品高清在线观看 | 国产高清在线精品一区二区三区| 成人国产精品高清在线观看| 99久久婷婷免费国产综合精品| 久久亚洲私人国产精品vA| 亚洲精品无码专区2| 日韩精品电影一区亚洲| 久久精品免费大片国产大片 | 999国产精品色在线播放 | 久久se精品一区二区| 2022精品天堂在线视频| 黑人巨大精品欧美| 国产精品露脸国语对白| 92国产精品午夜福利| 国内精品久久久久| 亚洲精品无码不卡| 国产91精品黄网在线观看| 国产精品麻豆入口| 国产三级国产精品国产普男人| 国内精品久久久久久久涩爱| 精品国产呦系列在线观看免费 | 日韩精品成人亚洲专区| 西瓜精品国产自在现线|