首頁 資料庫 mysql教程 关于Index Condition Pushdown特性_MySQL

关于Index Condition Pushdown特性_MySQL

May 30, 2016 pm 05:10 PM
特性

ICP简介

Index Condition Pushdown (ICP) is an optimization for the case where MySQL retrieves rows from a table using an index. Without ICP, the storage engine traverses the index to locate rows in the base table and returns them to the MySQL server which evaluates the WHEREcondition for the rows. With ICP enabled, and if parts of the WHERE condition can be evaluated by using only fields from the index, the MySQL server pushes this part of the WHERE condition down to the storage engine. The storage engine then evaluates the pushed index condition by using the index entry and only if this is satisfied is the row read from the table. ICP can reduce the number of times the storage engine must access the base table and the number of times the MySQL server must access the storage engine.

 

也就说:利用索引(二级索引)来过滤一部分where条件

 

测试

 

导入数据库

 

wget https://launchpad.net/test-db/employees-db-1/1.0.6/+download/employees_db-full-1.0.6.tar.bz2
tar jxf employees_db-full-1.0.6.tar.bz2
cd employees_db
mysql -uroot -p < employees.sql
登入後複製

表结构

mysql> show create table employees \G
*************************** 1. row ***************************
       Table: employees
Create Table: CREATE TABLE `employees` (
  `emp_no` int(11) NOT NULL,
  `birth_date` date NOT NULL,
  `first_name` varchar(14) NOT NULL,
  `last_name` varchar(16) NOT NULL,
  `gender` enum(&#39;M&#39;,&#39;F&#39;) NOT NULL,
  `hire_date` date NOT NULL,
  PRIMARY KEY (`emp_no`),
  KEY `index_bh` (`birth_date`,`hire_date`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)
登入後複製

一些表数据

mysql> select @@optimizer_switch like &#39;%index_condition_pushdown%&#39; \G
*************************** 1. row ***************************
@@optimizer_switch like &#39;%index_condition_pushdown%&#39;: 1
1 row in set (0.00 sec)

mysql> select @@optimizer_switch like &#39;%index_condition_pushdown%&#39; \G
*************************** 1. row ***************************
@@optimizer_switch like &#39;%index_condition_pushdown%&#39;: 1
1 row in set (0.00 sec)

mysql> select @@query_cache_type;
+--------------------+
| @@query_cache_type |
+--------------------+
| OFF                |
+--------------------+
1 row in set (0.01 sec)

mysql> select count(*) from employees;
+----------+
| count(*) |
+----------+
|   300024 |
+----------+
1 row in set (0.17 sec)

mysql> set profiling=1;
Query OK, 0 rows affected, 1 warning (0.00 sec)
登入後複製

建立索引

alter table employees add index index_bh (`birth_date`,`hire_date`);

查询分析

mysql> explain select *   from employees where birth_date between &#39;1955-01-01&#39; and &#39;1955-12-31&#39; and datediff(hire_date,birth_date)>12300 and first_name like &#39;S%b%&#39;;
+----+-------------+-----------+-------+---------------+----------+---------+------+-------+-------------+
| id | select_type | table     | type  | possible_keys | key      | key_len | ref  | rows  | Extra       |
+----+-------------+-----------+-------+---------------+----------+---------+------+-------+-------------+
|  1 | SIMPLE      | employees | range | index_bh      | index_bh | 3       | NULL | 46318 | Using where |
+----+-------------+-----------+-------+---------------+----------+---------+------+-------+-------------+
1 row in set (0.00 sec)

mysql> SET optimizer_switch=&#39;index_condition_pushdown=on&#39;;
Query OK, 0 rows affected (0.00 sec)

mysql> explain select *   from employees where birth_date between &#39;1955-01-01&#39; and &#39;1955-12-31&#39; and datediff(hire_date,birth_date)>12300 and first_name like &#39;S%b%&#39;;
+----+-------------+-----------+-------+---------------+----------+---------+------+-------+------------------------------------+
| id | select_type | table     | type  | possible_keys | key      | key_len | ref  | rows  | Extra                              |
+----+-------------+-----------+-------+---------------+----------+---------+------+-------+------------------------------------+
|  1 | SIMPLE      | employees | range | index_bh      | index_bh | 3       | NULL | 46318 | Using index condition; Using where |
+----+-------------+-----------+-------+---------------+----------+---------+------+-------+------------------------------------+
1 row in set (0.01 sec)
登入後複製

执行查询

mysql> show profiles;                                                                                                                 +----------+------------+------------------------------------------------------------------------------------------------------------------------------------------------------+
| Query_ID | Duration   | Query                                                                                                                                                |
+----------+------------+------------------------------------------------------------------------------------------------------------------------------------------------------+
|        1 | 0.00278025 | desc employees                                                                                                                                       |
|        2 | 0.00049775 | show create table employees                                                                                                                          |
|        3 | 0.07444550 | select *   from employees where birth_date between &#39;1955-01-01&#39; and &#39;1955-12-31&#39; and datediff(hire_date,birth_date)>12300 and first_name like &#39;S%b%&#39; |
|        4 | 0.00027500 | SET optimizer_switch=&#39;index_condition_pushdown=off&#39;                                                                                                  |
|        5 | 0.12347025 | select *   from employees where birth_date between &#39;1955-01-01&#39; and &#39;1955-12-31&#39; and datediff(hire_date,birth_date)>12300 and first_name like &#39;S%b%&#39; |
+----------+------------+------------------------------------------------------------------------------------------------------------------------------------------------------+
登入後複製

 

 

从结果可以看出来开启ICP之后确实快不少

 

启用ICP之后,可以用索引来筛选 datediff(hire_date,birth_date)>12300 记录,不需要读出整条记录

 

ICP原理

 

如下图所示(图来自MariaDB)

 

1、优化器没有使用ICP时

 

在存储引擎层,首先读取索引元组(index tuple),然后使用(index tuple)在基表中(base table)定位和读取整行数据

 

到服务器层,匹配where条件,如果该行数据满足where条件则使用,否则丢弃
 

指针向下一行移动,重复以上过程

关于Index Condition Pushdown特性_MySQL

2、使用ICP的时候

 

如果where条件的一部分能够通过使用索引中的字段进行过滤,那么服务器层将把这部分where条件Pushdown到存储引擎层

 

到存储引擎层,从索引中读取索引元组(index tuple),使用索引元组进行判断,如果没有满足where条件,则处理下一条索引元组(index tuple),只有当索引元组满足条件的时候,才会去基表中读取数据

关于Index Condition Pushdown特性_MySQL

ICP的使用条件

 

1、只能用于二级索引(secondary index)

 

2、explain显示的执行计划中type值(join 类型)为range、 ref、 eq_ref或者ref_or_null。且查询需要访问表的整行数据,即不能直接通过二级索引的元组数据获得查询结果(索引覆盖)

 

3、ICP可以用于MyISAM和InnnoDB存储引擎,不支持分区表(5.7将会解决这个问题)

 

4、ICP的加速效果取决于在存储引擎内通过ICP筛选掉的数据的比例

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您聽不到任何人,如何修復音頻
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解鎖Myrise中的所有內容
3 週前 By 尊渡假赌尊渡假赌尊渡假赌

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

win7家庭版與win7旗艦版的差異介紹 win7家庭版與win7旗艦版的差異介紹 Jul 12, 2023 pm 08:41 PM

大家都知道win7系統有很多種版本,像是win7旗艦版、win7專業版、win7家用版等,有不少用戶在家庭版和旗艦版之間糾結,不知道選擇哪個版本比較好,所以今天小編來跟大家說說win7家庭餐與win7旗艦版的差別介紹,大家一起來看看。 1.體驗不同家庭普通版使您的日常操作變得更快、更簡單,可以更快、更方便地存取使用最頻繁的程式和文件。家庭高級版讓您享有最佳的娛樂體驗,可以輕鬆欣賞和分享您喜愛的電視節目、照片、影片和音樂。旗艦版集各版本功能大全,具備Windows7家庭高級版的所有娛樂功能與專

掌握Spring MVC的關鍵概念:了解這些重要特性 掌握Spring MVC的關鍵概念:了解這些重要特性 Dec 29, 2023 am 09:14 AM

了解SpringMVC的關鍵特性:掌握這些重要的概念,需要具體程式碼範例SpringMVC是一種基於Java的Web應用開發框架,它透過模型-視圖-控制器(MVC)的架構模式來幫助開發人員建立靈活可擴展的Web應用程式。了解和掌握SpringMVC的關鍵特性將使我們能夠更有效地開發和管理我們的網路應用程式。本文將介紹一些SpringMVC的重要概念

5g的三個特性是什麼 5g的三個特性是什麼 Dec 09, 2020 am 10:55 AM

5g的三個特性是:1、高速率;在實際應用中,5G網路的速率是4G網路10倍以上。 2、低時延;5G網路的時延約幾十毫秒,比人的反應速度還要快。 3.廣連結;5G網路出現,配合其他技術,將會打造一個全新的萬物互聯景象。

選擇適用的Go版本,依需求和特性 選擇適用的Go版本,依需求和特性 Jan 20, 2024 am 09:28 AM

隨著網路的快速發展,程式語言也不斷演化和更新。其中,Go語言作為一種開源的程式語言,在近年來備受關注。 Go語言的設計目標是簡單、有效率、安全且易於開發和部署。它具有高並發、快速編譯和記憶體安全等特性,讓它在Web開發、雲端運算和大數據等領域中有著廣泛的運用。然而,目前Go語言也有不同的版本可供選擇。在選擇合適的Go語言版本時,我們需要考慮需求和特性兩個面向。首

Golang中有類似類別的物件導向特性嗎? Golang中有類似類別的物件導向特性嗎? Mar 19, 2024 pm 02:51 PM

在Golang(Go語言)中並沒有傳統意義上的類別的概念,但它提供了一種稱為結構體的資料類型,透過結構體可以實現類似類別的物件導向特性。在本文中,我們將介紹如何使用結構體實現物件導向的特性,並提供具體的程式碼範例。結構體的定義和使用首先,讓我們來看看結構體的定義和使用方式。在Golang中,結構體可以透過type關鍵字定義,然後在需要的地方使用。結構體中可以包含屬

C++ 函式的型別和特性 C++ 函式的型別和特性 Apr 11, 2024 pm 03:30 PM

C++函式有以下型別:簡單函式、const函式、靜態函式、虛函式;特性包括:inline函式、預設參數、參考回傳、重載函式。例如,calculateArea函數使用π計算給定半徑圓的面積,並將其作為輸出傳回。

java的特性是什麼 java的特性是什麼 Aug 09, 2023 pm 03:05 PM

java的特性是:1、簡單易學;2、面向對象,使得程式碼更可重複使用和維護;3、平台無關性,能在不同的作業系統上運作;4、記憶體管理,透過自動垃圾回收機制來管理記憶體;5、強型別檢查,變數在使用前必須先聲明類型;6、安全性,可以防止未經授權的存取和惡意程式碼的執行;7、多執行緒支持,能提高程式的效能和回應能力;8、異常處理,可以避免程式崩潰;9、大量的開發庫和框架;10、開源生態系統。

掌握Golang中間件的關鍵特性與應用場景 掌握Golang中間件的關鍵特性與應用場景 Mar 20, 2024 pm 06:33 PM

Golang作為一門快速、有效率的程式語言,在Web開發領域也有廣泛的應用。其中,中間件作為一種重要的設計模式,能夠幫助開發者更好地組織、管理程式碼,提高程式碼的可重複使用性和可維護性。本文將介紹Golang中中間件的關鍵特性和應用場景,並透過具體的程式碼範例來說明其用法。一、中間件的概念及作用中間件作為一種插入式的元件,位於應用程式的請求-回應處理鏈中,用

See all articles