5分钟了解MySQL5.7对in用法有什么黑科技

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
云数据库 RDS MySQL Serverless,价值2615元额度,1个月
简介:

构建测试环境


Part1:创建测试数据库

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
[root@HE1 ~]# mysql -uroot -p
Enter  password
Welcome  to  the MySQL monitor.  Commands  end  with  or  \g.
Your MySQL  connection  id  is  5
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)
 
 
[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  3
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 [helei]>  select  version();
+ -----------------+
| version()       |
+ -----------------+
| 10.1.16-MariaDB |
+ -----------------+
1 row  in  set  (0.00 sec)



如何构建MySQL5.7测试环境和MariaDB10.1测试环境本文不做赘述,如有需要可移步:

一分钟完成MySQL5.7安装

http://suifu.blog.51cto.com/9167728/1855415

MariaDB10.1自动化部署

http://suifu.blog.51cto.com/9167728/1830575



Part2:构建测试表

1
2
3
4
5
6
7
8
9
10
11
CREATE  TABLE  helei (
id  INT  (10) UNSIGNED  NOT  NULL  AUTO_INCREMENT,
c1  INT  (10)  NOT  NULL  DEFAULT  '0' ,
c2  INT  (10) UNSIGNED  DEFAULT  NULL ,
c5  INT  (10) UNSIGNED  NOT  NULL  DEFAULT  '0' ,
c3  TIMESTAMP  NOT  NULL  DEFAULT  CURRENT_TIMESTAMP  ON  UPDATE  CURRENT_TIMESTAMP ,
c4  VARCHAR  (200)  NOT  NULL  DEFAULT  '' ,
PRIMARY  KEY  (id),
KEY  idx_c1 (c1),
KEY  idx_c2 (c2)
) ENGINE = INNODB;




对比测试结果

Part1:MyriaDB10.1.16

1
2
3
4
5
6
7
MariaDB [helei]> explain  select  from  helei  where  (id,c1)  in  ((100,2684),(101,3566));
+ ------+-------------+-------+------+---------------+------+---------+------+------+-------------+
| id   | select_type |  table  | type | possible_keys |  key   | key_len | ref  |  rows  | Extra       |
+ ------+-------------+-------+------+---------------+------+---------+------+------+-------------+
|    1 | SIMPLE      | helei |  ALL   NULL           NULL  NULL     NULL  | 5198 | Using  where  |
+ ------+-------------+-------+------+---------------+------+---------+------+------+-------------+
1 row  in  set  (0.00 sec)


可以看到,MariaDB对这条SQL总共扫描了5198行,且没有用到任何的索引。



Part2:MySQL5.7.15

1
2
3
4
5
6
7
mysql> explain  select  from  helei  where  (id,c1)  in  ((100,2684),(101,3566));
+ ----+-------------+-------+------------+-------+----------------+---------+---------+------+------+----------+-------------+
| id | select_type |  table  | partitions | type  | possible_keys  |  key      | key_len | ref  |  rows  | filtered | Extra       |
+ ----+-------------+-------+------------+-------+----------------+---------+---------+------+------+----------+-------------+
|  1 | SIMPLE      | helei |  NULL        | range |  PRIMARY ,idx_c1 |  PRIMARY  | 4       |  NULL  |    2 |    20.00 | Using  where  |
+ ----+-------------+-------+------------+-------+----------------+---------+---------+------+------+----------+-------------+
1 row  in  set , 1 warning (0.29 sec)


可以看到,MySQL5.7版本已经用到了索引,一共扫描条数为2行。这是因为该语句已经被MySQL5.7的内部优化器改写为如下图所示:

wKiom1f7NeGxpFDTAAFAu7CKX0c704.jpg




——总结——

可以看出,MariaDB版本内部优化器无法改写,因此对于这类查询只能全表扫描。经测试,MySQL5.6和MariaDB10.0/10.1都无法改写。由于笔者的水平有限,编写时间也很仓促,文中难免会出现一些错误或者不准确的地方,不妥之处恳请读者批评指正。




 本文转自 dbapower 51CTO博客,原文链接:http://blog.51cto.com/suifu/1860216
,如需转载请自行联系原作者

相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助     相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
2月前
|
关系型数据库 MySQL
MySQL中CONCAT() ,CONCAT_WS() ,GROUP_CONCAT()的用法
MySQL中CONCAT() ,CONCAT_WS() ,GROUP_CONCAT()的用法
15 2
|
5月前
|
SQL 关系型数据库 MySQL
MySQL 中exists与in及any的用法详解
MySQL 中exists与in及any的用法详解
60 3
|
2月前
|
SQL 关系型数据库 MySQL
mysql结果垂直显示-\g和\G的用法
mysql结果垂直显示-\g和\G的用法
31 0
|
2月前
|
存储 安全 关系型数据库
MySQL 临时表的用法和特性
MySQL 临时表的用法和特性
|
7月前
|
关系型数据库 MySQL Unix
【MySQL用法】MySQL 中 datetime 和 timestamp 的区别与选择
【MySQL用法】MySQL 中 datetime 和 timestamp 的区别与选择
125 0
|
7月前
|
XML Java 数据库连接
【MySQL用法】MyBatis 多对多 中间表插入数据,添加记录后获取主键ID
【MySQL用法】MyBatis 多对多 中间表插入数据,添加记录后获取主键ID
63 0
|
3月前
|
SQL 关系型数据库 MySQL
Mysql SQL的一些特殊用法记录
1、查询group by having 中having不起作用,及解决
15 0
|
5月前
|
SQL 关系型数据库 MySQL
MySQL知识汇总:MySQL函数CASE WHEN用法详解
MySQL知识汇总:MySQL函数CASE WHEN用法详解
|
5月前
|
关系型数据库 MySQL Linux
MySQL - \g 和 \G用法与区别
MySQL - \g 和 \G用法与区别
43 1
|
6月前
|
关系型数据库 MySQL
MySQL中TIMESTAMPDIFF和TIMESTAMPADD函数的用法
MySQL中TIMESTAMPDIFF和TIMESTAMPADD函数的用法