5分钟了解MySQL/MariaDB新特性之索引下推优化
2016-10-13 09:48
603 查看
MySQL/MariaDB新特性之索引下推优化
索引下推优化
Part1:index_condition_pushdown
index_condition_pushdown(ICP)默认开启,可以通过命令:
show variables like 'optimizer_switch'\G来查看,如下图所示:
Part2:原理简述这一特性从MariaDB5.3/MySQL5.6起,开始生效,我们在执行查询计划的时候,看到的Using index condition特性,简称为ICP。它可以提高检索速度,提高从server层到存储引擎层的调用速度,并减少了存储引擎访问表的次数,从而提高了数据库的整体性能。总之一句话,当你看到执行计划中出现了using index condition的时候,说明效率好!很好!非常好!就对了~
实战
Part1:ICP在MySQL5.6.25下的表现
[root@HE1 ~]# mysql -uroot -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 1 Server version: 5.6.25-log MySQL Community Server (GPL) Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> select version(); +------------+ | version() | +------------+ | 5.6.25-log | +------------+ 1 row in set (0.00 sec) mysql> use helei; Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed mysql> show create table helei_stu\G\ *************************** 1. row *************************** Table: helei_stu Create Table: CREATE TABLE `helei_stu` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `name` varchar(10) NOT NULL DEFAULT '', `class` tinyint(3) unsigned NOT NULL DEFAULT '0', `score` tinyint(3) unsigned NOT NULL DEFAULT '0', PRIMARY KEY (`id`), KEY `idx_class_score` (`class`,`score`), KEY `idx_name` (`name`) ) ENGINE=InnoDB AUTO_INCREMENT=56 DEFAULT CHARSET=utf8 1 row in set (0.00 sec) mysql> explain select * from helei_stu where class=2 and score >60; +----+-------------+-----------+-------+-----------------+-----------------+---------+------+------+-----------------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+-----------+-------+-----------------+-----------------+---------+------+------+-----------------------+ | 1 | SIMPLE | helei_stu | range | idx_class_score | idx_class_score | 2 | NULL | 8 | Using index condition | +----+-------------+-----------+-------+-----------------+-----------------+---------+------+------+-----------------------+ 1 row in set (0.04 sec)
可以看出,在MySQL5.6.25中,执行结果如下图所示:
Part2:ICP在MySQL5.7.15下的表现
[root@HE1 ~]# mysql -uroot -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 22 Server version: 5.7.15-log MySQL Community Server (GPL) Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> select version(); +------------+ | version() | +------------+ | 5.7.15-log | +------------+ 1 row in set (0.00 sec) mysql> show create table helei_stu\G *************************** 1. row *************************** Table: helei_stu Create Table: CREATE TABLE `helei_stu` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `name` varchar(10) NOT NULL DEFAULT '', `class` tinyint(3) unsigned NOT NULL DEFAULT '0', `score` tinyint(3) unsigned NOT NULL DEFAULT '0', PRIMARY KEY (`id`), KEY `idx_class_score` (`class`,`score`), KEY `idx_name` (`name`) ) ENGINE=InnoDB AUTO_INCREMENT=56 DEFAULT CHARSET=utf8 1 row in set (0.00 sec) mysql> explain select * from helei_stu where class=2 and score >60; +----+-------------+-----------+------------+-------+-----------------+-----------------+---------+------+------+----------+-----------------------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+-------------+-----------+------------+-------+-----------------+-----------------+---------+------+------+----------+-----------------------+ | 1 | SIMPLE | helei_stu | NULL | range | idx_class_score | idx_class_score | 2 | NULL | 8 | 100.00 | Using index condition | +----+-------------+-----------+------------+-------+-----------------+-----------------+---------+------+------+----------+-----------------------+ 1 row in set, 1 warning (0.00 sec)
可以看出,在MySQL5.7.15中,执行结果如下图所示:
Part3:ICP在MariaDB10.1.16下的表现
[root@HE3 ~]# /usr/local/mariadb/bin/mysql -uroot -S /tmp/mariadb.sock Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 26 Server version: 10.1.16-MariaDB MariaDB Server Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MariaDB [(none)]> select version(); +-----------------+ | version() | +-----------------+ | 10.1.16-MariaDB | +-----------------+ 1 row in set (0.00 sec) MariaDB [(none)]> use helei; Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed MariaDB [helei]> show create table helei_stu\G\ *************************** 1. row *************************** Table: helei_stu Create Table: CREATE TABLE `helei_stu` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `name` varchar(10) NOT NULL DEFAULT '', `class` tinyint(3) unsigned NOT NULL DEFAULT '0', `score` tinyint(3) unsigned NOT NULL DEFAULT '0', PRIMARY KEY (`id`), KEY `idx_class_score` (`class`,`score`), KEY `idx_name` (`name`) ) ENGINE=InnoDB AUTO_INCREMENT=56 DEFAULT CHARSET=utf8 1 row in set (0.00 sec) MariaDB [helei]> explain select * from helei_stu where class=2 and score >60; +------+-------------+-----------+-------+-----------------+-----------------+---------+------+------+-----------------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +------+-------------+-----------+-------+-----------------+-----------------+---------+------+------+-----------------------+ | 1 | SIMPLE | helei_stu | range | idx_class_score | idx_class_score | 2 | NULL | 8 | Using index condition | +------+-------------+-----------+-------+-----------------+-----------------+---------+------+------+-----------------------+ 1 row in set (0.00 sec) MariaDB [helei]> select version(); +-----------------+ | version() | +-----------------+ | 10.1.16-MariaDB | +-----------------+ 1 row in set (0.00 sec)
可以看出,在MariaDB10.1.16中,执行结果如下图所示:
——总结——
在MySQL5.5中,无这一特性,所以会先根据class=1来查找记录,最后根据score>60过滤。而MySQL5.6/MySQL5.7和MariaDB 5.3/5.5/10.0/10.1 版本中,在查找class=1的同时,会根据score>60进行过滤,所以这里看到的是using index condition。由于笔者的水平有限,编写时间也很仓促,文中难免会出现一些错误或者不准确的地方,不妥之处恳请读者批评指正。
相关文章推荐
- 5分钟了解与部署mariaDB(mysql)主从复制
- MySQL 索引优化全攻略
- mysql优化概述(范式.索引.定位慢查询)
- 「mysql优化专题」90%程序员面试都用得上的索引优化手册(5)
- 一次mysql的查询索引的优化
- MySQL 性能优化---索引及优化
- MySQL索引类型及优化
- MySQL索引使用方法和性能优化
- 【MySQL】(1)索引优化分析
- MySQL 索引优化全攻略
- MySQL索引原理及慢查询优化
- MySql优化--使用索引优化
- 理解MySQL——索引与优化
- Mysql5.7—mysql性能优化-索引、语句、配置(运维必备)
- Mysql 索引优化
- 浅谈MySQL优化索引技巧
- mysql慢查询优化篇之复合索引
- Mysql索引优化浅谈
- 由浅入深探究mysql索引结构原理、性能分析与优化
- mysql性能优化之索引优化