This article brings you relevant knowledge about mysql, which mainly introduces the relevant content about index pushdown. Index condition pushdown is also called index pushdown. The full English name is Index Condition Pushdown. , referred to as ICP, is used to optimize data queries. Let’s take a look at it. I hope it will be helpful to everyone.
Recommended learning: mysql video tutorial
SELECT statement execution process
MySQL The database consists of the Server layer and the Engine layer:
-
ServerLayer: There areSQLanalyzer,SQLoptimizer,SQLexecutor, which are responsible for the specific execution process ofSQLstatements. -
EngineLayer: Responsible for storing specific data, such as the most commonly usedInnoDBstorage engine, and for storing temporary data in memory TheTempTableengine for result sets.

Establishes a connection to
MySQLthrough the client/server communication protocol.-
Query Cache:
- If
Query Cacheis enabled and the query cache is fully queried If the sameSQLstatement is used, the query results will be returned directly to the client; - If
Query Cacheis not turned on or the exact sameSQL# is not queried ## The statement will be parsed syntactically and semantically by the parser and a parse tree will be generated.
- If
- The parser generates a new parse tree.
- The query optimizer generates an execution plan.
- The query execution engine executes the
SQL
statement. At this time, the query execution engine will determine the storage engine type of the table in theSQLstatement and the correspondingAPIThe interface interacts with the underlying storage engine cache or physical files to obtain query results. After filtering byMySQL Server, the query results are cached and returned to the client.If
Query Cache
is enabled,SQLstatements and results will be completely saved toQuery Cache. If the sameSQLstatement is executed in the future, the result will be returned directly.
Tips: MySQL 8.0 has removed query cache (query cache module).
Because the hit rate of the query cache will be very low. Query cache invalidations are very frequent: whenever there is an update to a table, all query caches on that table are cleared.What is index pushdown?
Index Condition Pushdown: Referred to as ICP, it reduces # by pushing down the index filtering conditions to the storage engine. ##MySQL The number of times the storage engine accesses the base table and MySQL The number of times the service layer accesses the storage engine. Index pushdown VS covering index:
reduce the number of table returns, but in different ways
- Covered index:
- When the index contains the required fields (
SELECT XXX), no more fields will be returned to the table to query.
Index pushdown: - Make a judgment first on the fields included in the index,
Directly filter out records that do not meet the conditions , and reduce table returns number of rows.
To understand how
works, start with a query SQL: Give a chestnut: Query the records whose name starts with la
18
SELECT * FROM user WHERE name LIKE 'la%' AND age = 18;
has these records:
How the index scan is performed when
is not enabled: Locate and read the corresponding data row. (Actually: just return the table)
- Make judgments on the fields in
- WHERE and filter out rows that do not meet the conditions.
Using
, the index scan is performed as follows: Get the index element Group.
- Make judgment on the fields in
- WHERE and filter in the index column.
For indexes that meet the conditions, return the table to query the entire row. Make judgments on the fields in - WHERE and filter out rows that do not meet the conditions.
动手实验:
实验:使用 MySQL 版本 8.0.16
-- 表创建 CREATE TABLE IF NOT EXISTS `user` ( `id` VARCHAR(64) NOT NULL COMMENT '主键 id', `name` VARCHAR(50) NOT NULL COMMENT '名字', `age` TINYINT NOT NULL COMMENT '年龄', `address` VARCHAR(100) NOT NULL COMMENT '地址', PRIMARY KEY (id) ) ENGINE=InnoDB DEFAULT CHARSET utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT '用户表'; -- 创建索引 CREATE INDEX idx_name_age ON user (name, age); -- 新增数据 INSERT INTO user (id, name, age, address) VALUES (1, 'tt', 14, 'linhai'); INSERT INTO user (id, name, age, address) VALUES (2, 'lala', 18, 'linhai'); INSERT INTO user (id, name, age, address) VALUES (3, 'laxi', 30, 'linhai'); INSERT INTO user (id, name, age, address) VALUES (4, 'lawa', 40, 'linhai'); -- 查询语句 SELECT * FROM user WHERE name LIKE 'la%' AND age = 18;
新增数据如下:

- 关闭
ICP,再调用EXPLAIN查看语句:
-- 将 ICP 关闭 SET optimizer_switch = 'index_condition_pushdown=off'; -- 查看确认 show variables like 'optimizer_switch'; -- 用 EXPLAIN 查看 EXPLAIN SELECT * FROM user WHERE name LIKE 'la%' AND age = 18;

- 开启
ICP,再调用EXPLAIN查看语句:
-- 将 ICP 打开 SET optimizer_switch = 'index_condition_pushdown=on'; -- 查看确认 show variables like 'optimizer_switch'; -- 用 EXPLAIN 查看 EXPLAIN SELECT * FROM user WHERE name LIKE 'la%' AND age = 18;

由上实验可知,区别是否开启 ICP: Exira 字段中的 Using index condition

更进一步,来看下 ICP 带来的性能提升:
通过访问数据文件的次数
-- 1. 清空 status 状态 flush status; -- 2. 查询 SELECT * FROM user WHERE name LIKE 'la%' AND age = 18; -- 3. 查看 handler 状态 show status like '%handler%';
对比开启 ICP 和 关闭 ICP: 关注 Handler_read_next 的值
-- 开启 ICP flush status; SELECT * FROM user WHERE name LIKE 'la%' AND age = 18; show status like '%handler%'; +----------------------------|-------+ | Variable_name | Value | +----------------------------|-------+ | Handler_commit | 1 | | Handler_delete | 0 | | Handler_discover | 0 | | Handler_external_lock | 2 | | Handler_mrr_init | 0 | | Handler_prepare | 0 | | Handler_read_first | 0 | | Handler_read_key | 1 | | Handler_read_last | 0 | | Handler_read_next | 1 | <---重点 | Handler_read_prev | 0 | | Handler_read_rnd | 0 | | Handler_read_rnd_next | 0 | | Handler_rollback | 0 | | Handler_savepoint | 0 | | Handler_savepoint_rollback | 0 | | Handler_update | 0 | | Handler_write | 0 | +----------------------------|-------+ 18 rows in set (0.00 sec) -- 关闭 ICP flush status; SELECT * FROM user WHERE name LIKE 'la%' AND age = 18; show status like '%handler%'; +----------------------------|-------+ | Variable_name | Value | +----------------------------|-------+ | Handler_commit | 1 | | Handler_delete | 0 | | Handler_discover | 0 | | Handler_external_lock | 2 | | Handler_mrr_init | 0 | | Handler_prepare | 0 | | Handler_read_first | 0 | | Handler_read_key | 1 | | Handler_read_last | 0 | | Handler_read_next | 3 | <---重点 | Handler_read_prev | 0 | | Handler_read_rnd | 0 | | Handler_read_rnd_next | 0 | | Handler_rollback | 0 | | Handler_savepoint | 0 | | Handler_savepoint_rollback | 0 | | Handler_update | 0 | | Handler_write | 0 | +----------------------------|-------+ 18 rows in set (0.00 sec)
由上实验可知:
- 开启
ICP:Handler_read_next等于 1,回表查 1 次。 - 关闭
ICP:Handler_read_next等于 3,回表查 3 次。
这实验跟上面的栗子就对应上了。
索引下推限制
根据官网可知,索引下推 受以下条件限制:
当需要访问整个表行时,
ICP用于range、ref、eq_ref和ref_or_nullICP可以用于InnoDB和MyISAM表,包括分区表InnoDB和MyISAM表。对于
InnoDB表,ICP仅用于二级索引。ICP的目标是减少全行读取次数,从而减少I/O操作。对于InnoDB聚集索引,完整的记录已经读入InnoDB缓冲区。在这种情况下使用ICP不会减少I/O。在虚拟生成列上创建的二级索引不支持
ICP。InnoDB支持虚拟生成列的二级索引。引用子查询的条件不能下推。
引用存储功能的条件不能被按下。存储引擎不能调用存储的函数。
触发条件不能下推。
不能将条件下推到包含对系统变量的引用的派生表。(
MySQL 8.0.30及更高版本)。
小结下:
-
ICP仅适用于 二级索引。 -
ICP目标是 减少回表查询。 -
ICP对联合索引的部分列模糊查询非常有效。
拓展:虚拟列
CREATE TABLE UserLogin ( userId BIGINT, loginInfo JSON, cellphone VARCHAR(255) AS (loginInfo->>"$.cellphone"), PRIMARY KEY(userId), UNIQUE KEY idx_cellphone(cellphone) );
列 cellphone :就是一个虚拟列,它是由后面的函数表达式计算而成,本身这个列不占用任何的存储空间,而索引 idx_cellphone 实质是一个函数索引。
好处: 在写 SQL 时可以直接使用这个虚拟列,而不用写冗长的函数。
举个栗子: 查询手机号
-- 不用虚拟列 SELECT * FROM UserLogin WHERE loginInfo->>"$.cellphone" = '13988888888' -- 使用虚拟列 SELECT * FROM UserLogin WHERE cellphone = '13988888888'
推荐学习:mysql视频教程
The above is the detailed content of Understanding MySQL index pushdown in one article. For more information, please follow other related articles on the PHP Chinese website!
Troubleshooting MySQL Corrupted Tables and Data RecoveryJul 21, 2025 am 02:14 AMCommon causes of table corruption include system crashes, insufficient storage, file system errors, abnormal service termination and unstable storage engines. The detection methods include using the CHECKTABLE command, viewing error logs and regular checks. Repair methods include REPAIRTABLE command, myisamchk tool repair and recovery from backup. Notes include backup first and then operation, confirming the storage engine type, using professional tools, and monitoring server stability.
Optimizing MySQL for Real-time Dashboards and ReportingJul 21, 2025 am 02:12 AMTooptimizeMySQLforreal-timedashboards,focusonsmartindexing,queryoptimization,schemadesign,andcaching.1)Useindexeswisely—createcompositeandcoveringindexeswhereneededbutavoidover-indexing.2)Optimizequeriesbyselectingonlynecessarycolumns,limitingresults
Securing MySQL Connections with SSL/TLS EncryptionJul 21, 2025 am 02:08 AMWhy do I need SSL/TLS encryption MySQL connection? Because unencrypted connections may cause sensitive data to be intercepted, enabling SSL/TLS can prevent man-in-the-middle attacks and meet compliance requirements; 2. How to configure SSL/TLS for MySQL? You need to generate a certificate and a private key, modify the configuration file to specify the ssl-ca, ssl-cert and ssl-key paths and restart the service; 3. How to force SSL when the client connects? Implemented by specifying REQUIRESSL or REQUIREX509 when creating a user; 4. Details that are easily overlooked in SSL configuration include certificate path permissions, certificate expiration issues, and client configuration requirements.
Implementing MySQL Database Governance FrameworksJul 21, 2025 am 02:06 AMThe key to implementing the MySQL database governance framework is to clarify the goals, sort out the status quo and select appropriate tools and strategies. 1. Permission control should follow the principle of minimum authority, use role management to unified authority allocation, and regularly audit permissions to reduce risks; 2. Introduce SQL audit platform to implement pre-online inspections, standardize changes processes and record historical changes to avoid unaudited high-risk operations; 3. Establish a performance monitoring system, covering basic and business indicators, set up a hierarchical alarm mechanism, and recommend the use of Prometheus Grafana to achieve visual monitoring; 4. Formulate a backup strategy that combines full and incremental quantities, regularly verify the effectiveness of backups and conducts recovery drills, establish an automatic backup and manual confirmation mechanism to prevent formalistic backups. The above four directions
Optimizing MySQL for Real-time Fraud DetectionJul 21, 2025 am 01:59 AMTooptimizeMySQLforreal-timefrauddetection,configuresmartindexing,chooseInnoDBasthestorageengine,andtunesystemsettingsforhighthroughput.1)Usecompositeandcoveringindexestospeedupfrequentquerieswithoutover-indexing.2)SelectInnoDBforrow-levellocking,ACID
Optimizing MySQL Regular Expression SearchesJul 21, 2025 am 01:59 AMThe key points of optimizing the performance of MySQL regular query include: 1. Avoid full table scanning, narrowing the search scope through ordinary WHERE conditions or simplifying regular expressions; 2. Optimizing regular writing methods, such as using ^abc instead of �c%, avoiding greedy matching; 3. Reasonable use of generating column indexes, preprocessing and storing regular logic, and improving query efficiency.
Optimizing MySQL Server Configuration VariablesJul 21, 2025 am 01:58 AMMySQL performance optimization requires adjusting server configuration variables to improve efficiency and stability. First, it is necessary to set the memory parameters reasonably, such as innodb_buffer_pool_size, no more than 70% to 80% of the physical memory, key_buffer_size, tmp_table_size, etc., selectively adjust according to the engine; secondly, optimize connection and concurrency parameters, such as max_connections to avoid connection bottlenecks, thread_stack controls thread resource occupation, and innodb_thread_concurrency matches the number of CPU cores; secondly, balance log and persistence performance, such as innodb_flush_log_at_trx_c
Real-time Data Replication from MySQL using DebeziumJul 21, 2025 am 01:55 AMDebezium is an effective tool for real-time replication of MySQL. It captures data changes by parsing MySQL's binlog logs and pushes them to downstream systems such as Kafka. 1.Debezium is a log-based open source data change capture platform, suitable for MySQL because it can extract insert, update, and delete operations with low latency and low impact. 2. Preconditions include: MySQL5.7, setting binlog format to ROW, optional GTID, configuring replication user permissions, and installing Kafka and KafkaConnect. 3. The configuration needs to provide connection information JSON file, specify the database address, user, listened library table, snapshot mode and other parameters.


Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Dreamweaver CS6
Visual web development tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Chinese version
Chinese version, very easy to use









