时间:2021-07-01 10:21:17 帮助过:10人阅读
set optimizer_switch = ‘index_condition_pushdown=off‘; set optimizer_switch = ‘index_condition_pushdown=on‘;
测试过程
1.环境准备#mysql 5.6.25 #关闭结果缓存 mysql> set global query_cache_size=0; mysql> set query_cache_type=off; #查看表结构 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(‘M‘,‘F‘) NOT NULL, `hire_date` date NOT NULL, PRIMARY KEY (`emp_no`), KEY `idx_first_last_name` (`first_name`,`last_name`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 1 row in set (0.00 sec) mysql>
2.开启ICP后进行测试
mysql> set profiling = 1; mysql> select * from employees where first_name=‘Anneke‘ and last_name like ‘%sig‘ ; mysql> explain select * from employees where first_name=‘Anneke‘ and last_name like ‘%sig‘ ; mysql> show profiles; mysql> show profile cpu,block io for query 1;
3.关闭ICP后进行测试
mysql> set optimizer_switch=‘index_condition_pushdown=off‘; mysql> set profiling = 1; mysql> select * from employees where first_name=‘Anneke‘ and last_name like ‘%sig‘ ; mysql> explain select * from employees where first_name=‘Anneke‘ and last_name like ‘%sig‘ ; mysql> show profiles; mysql> show profile cpu,block io for query 1;
4.结果比较
开启ICP后的执行计划:执行计划中extra部分的内容是"using index condition"
mysql> explain select * from employees where first_name=‘Anneke‘ and last_name like ‘%sig‘ ; +----+-------------+-----------+------+---------------------+---------------------+---------+-------+------+-----------------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+-----------+------+---------------------+---------------------+---------+-------+------+-----------------------+ | 1 | SIMPLE | employees | ref | idx_first_last_name | idx_first_last_name | 44 | const | 224 | Using index condition | +----+-------------+-----------+------+---------------------+---------------------+---------+-------+------+-----------------------+
关闭ICP后的执行计划:执行计划中extra部分的内容是"using where"
mysql> explain select * from employees where first_name=‘Anneke‘ and last_name like ‘%sig‘ ; +----+-------------+-----------+------+---------------------+---------------------+---------+-------+------+-------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+-----------+------+---------------------+---------------------+---------+-------+------+-------------+ | 1 | SIMPLE | employees | ref | idx_first_last_name | idx_first_last_name | 44 | const | 224 | Using where | +----+-------------+-----------+------+---------------------+---------------------+---------+-------+------+-------------+
开启ICP后的profile内容:Sending data部分的值是0.000212s
mysql> show profile cpu,block io for query 1; +----------------------+----------+----------+------------+--------------+---------------+ | Status | Duration | CPU_user | CPU_system | Block_ops_in | Block_ops_out | +----------------------+----------+----------+------------+--------------+---------------+ | starting | 0.000114 | 0.000000 | 0.000000 | 0 | 0 | | checking permissions | 0.000007 | 0.000000 | 0.000000 | 0 | 0 | | Opening tables | 0.000018 | 0.000000 | 0.000000 | 0 | 0 | | init | 0.000034 | 0.000000 | 0.000000 | 0 | 0 | | System lock | 0.000008 | 0.000000 | 0.000000 | 0 | 0 | | optimizing | 0.000023 | 0.000000 | 0.000000 | 0 | 0 | | statistics | 0.000383 | 0.000000 | 0.000000 | 0 | 0 | | preparing | 0.000019 | 0.000000 | 0.000000 | 0 | 0 | | executing | 0.000002 | 0.000000 | 0.000000 | 0 | 0 | | Sending data | 0.000212 | 0.000000 | 0.000000 | 0 | 0 | | end | 0.000004 | 0.000000 | 0.000000 | 0 | 0 | | query end | 0.000004 | 0.000000 | 0.000000 | 0 | 0 | | closing tables | 0.000006 | 0.000000 | 0.000000 | 0 | 0 | | freeing items | 0.000020 | 0.000000 | 0.000000 | 0 | 0 | | cleaning up | 0.000011 | 0.000000 | 0.000000 | 0 | 0 | +----------------------+----------+----------+------------+--------------+---------------+
关闭ICP后的profile内容:Sending data部分的值是0.010990s
mysql> show profile cpu,block io for query 1; +----------------------+----------+----------+------------+--------------+---------------+ | Status | Duration | CPU_user | CPU_system | Block_ops_in | Block_ops_out | +----------------------+----------+----------+------------+--------------+---------------+ | starting | 0.000165 | 0.000000 | 0.000000 | 0 | 0 | | checking permissions | 0.000022 | 0.000000 | 0.000000 | 0 | 0 | | Opening tables | 0.000027 | 0.000000 | 0.000000 | 0 | 0 | | init | 0.000039 | 0.000000 | 0.000000 | 0 | 0 | | System lock | 0.000008 | 0.000000 | 0.000000 | 0 | 0 | | optimizing | 0.000037 | 0.001000 | 0.000000 | 0 | 0 | | statistics | 0.000483 | 0.001000 | 0.000000 | 0 | 0 | | preparing | 0.000022 | 0.000000 | 0.000000 | 0 | 0 | | executing | 0.000002 | 0.000000 | 0.000000 | 0 | 0 | | Sending data | 0.010990 | 0.007999 | 0.002000 | 0 | 0 | | end | 0.000009 | 0.000000 | 0.000000 | 0 | 0 | | query end | 0.000005 | 0.000000 | 0.000000 | 0 | 0 | | closing tables | 0.000008 | 0.000000 | 0.000000 | 0 | 0 | | freeing items | 0.000028 | 0.000000 | 0.000000 | 0 | 0 | | cleaning up | 0.000014 | 0.000000 | 0.000000 | 0 | 0 | +----------------------+----------+----------+------------+--------------+---------------+
其它:
当sql使用覆盖索引时,不支持ICP优化方法
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_first_last_name | idx_first_last_name | 94 | const,const | 1 | Using where; Using index | +----+-------------+-----------+------+---------------------+---------------------+---------+-------------+------+--------------------------+ 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_first_last_name | idx_first_last_name | 94 | const,const | 1 | Using index condition | +----+-------------+-----------+------+---------------------+---------------------+---------+-------------+------+-----------------------+
MySQL 5.6新特性 -- Index Condition Pushdown
标签:check com 评估 query condition 取数 技术分享 oss statistic