国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務器之家:專注于服務器技術及軟件下載分享
分類導航

Mysql|Sql Server|Oracle|Redis|MongoDB|PostgreSQL|Sqlite|DB2|mariadb|Access|數據庫技術|

服務器之家 - 數據庫 - Mysql - MySQL Index Condition Pushdown(ICP)性能優化方法實例

MySQL Index Condition Pushdown(ICP)性能優化方法實例

2020-05-10 17:04服務器之家 Mysql

這篇文章主要介紹了MySQL Index Condition Pushdown(ICP)性能優化方法實例,本文講解了概念介紹、原理、實踐案例、案例分析、ICP的使用限制等內容,需要的朋友可以參考下

一 概念介紹

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

a 當關閉ICP時,index 僅僅是data access 的一種訪問方式,存儲引擎通過索引回表獲取的數據會傳遞到MySQL Server 層進行where條件過濾。

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

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

二 原理

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.

三 實踐案例

a 環境準備
   數據庫版本 5.6.16
   關閉緩存
  

復制代碼 代碼如下:

     set query_cache_size=0;
     set query_cache_type=OFF;


   測試數據下載地址
b 當開啟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的最左前綴原則, first_name 可以使用索引,last_name采用了like 模糊查詢,不能使用索引。
c 關閉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)

 

當開啟ICP時 查詢在sending data環節時間消耗是 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)

 

當關閉ICP時 查詢在sending data環節時間消耗是 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 開啟時整個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 關閉時 ,僅僅使用索引作為訪問數據的方式。

MySQL Index Condition Pushdown(ICP)性能優化方法實例

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

MySQL Index Condition Pushdown(ICP)性能優化方法實例

四 ICP的使用限制

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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 精品无码久久久久久国产 | 精品久久一区二区三区 | 一级黄色片欧美 | 日韩中文一区 | 国产亚洲精品久久久闺蜜 | 91亚洲国产精品 | 欧美精品一区二区三区在线四季 | 亚洲视频免费 | 久久成人精品视频 | 亚洲乱码一区二区三区在线观看 | 亚洲色视频 | 91久久精品日日躁夜夜躁国产 | 欧美激情一区二区 | 四房婷婷 | 亚洲国产精品成人va在线观看 | 久久久亚洲精品一区二区三区 | 成人在线小视频 | 中文字幕精品一区 | 色综合中文 | 97久久精品午夜一区二区 | 木耳av在线 | 欧美日韩在线视频观看 | 久久久av | 免费观看一级淫片 | 亚洲精品成人免费 | 日韩理论在线 | 综合久久av | 亚洲天堂中文字幕 | 久久噜| 美女视频一区 | 午夜看片网站 | 91网在线观看 | 亚洲天堂中文字幕 | 免费一级黄色录像 | 日本在线观看www | 最近免费观看高清韩国日本大全 | 在线国产精品一区 | a视频网站| 激情五月综合网 | 二区在线观看 | 欧美亚洲免费 |