mysql半连接_mysql表的半连接,反连接导致的mysql性能优化剖析

news/2024/7/7 19:08:09

[导读] 关于Oracle的半连接,反连接,我一直认为这是一个能讲很长时间的话题,所以在我的新书《Oracle DBA工作笔记》中讲性能优化的时候,我花...

关于Oracle的半连接,反连接,我一直认为这是一个能讲很长时间的话题,所以在我的新书《Oracle DBA工作笔记》中讲性能优化的时候,我花了不少的笔墨做了阐述,结果在做MySQL性能优化的时候,优化思路切换到MySQL层面,我发现要说的东西要更多。总体来看,这部分的优化细节MySQL还在路上,不同的版本中都能够一窥其中的变化,可以看到在不断改进。

在表的连接上,半连接,反连接本身很平常,但是统计信息的不够丰富导致执行计划的评估中可能会出现较大差别,会很可能把半连接,反连接的实现方式和执行路径的差异放大,导致SQL性能变差,同时MySQL里面in和exists的差距也在减小。

我就简化一下我的描述,拿MySQL 5.6版本的一些差别来说明。算是对5.5和5.7的承上启下。

我们创建一个表t_fund_info,数据量在两百万,创建另外一个表t_user_login_record数据量和t_fund_info一样。 t_fund_info有主键字段account,t_user_login_record没有索引。

SQL语句如下:select account

from t_fund_info

where money >= 300

and account not in (select distinct (account)

from t_user_login_record

where add_time >= "2016-06-01");

里面的列select_type PRIMARY代表子查询中的最外层查询,此处不是主键查询。而SUBQUERY代表是子查询内层查询的第一个SELECT,结果不会依赖于外部查询的结果集。

从type为ALL代表是全表扫描,所以这样一个查询两个表都是全表扫描,在MySQL内部解析的时候是怎么分解的呢。我们通过explain extended的方式来得到更详细的信息。/* select#1 */

select test . t_fund_info . account AS account

from test . t_fund_info

where ((test . t_fund_info . money >= 300) and

(not (

(test . t_fund_info . account, test . t_fund_info .

account in

(

( /* select#2 */

select test . t_user_login_record . account

from test . t_user_login_record

where (test . t_user_login_record . add_time >= "2016-06-01")), <

primary_index_lookup >

(test . t_fund_info . account in 

table > on 

where((test . t_fund_info . account = materialized - subquery .

account))))))))

可以看到启用了临时表,查取了子查询的数据作为后续的缓存处理数据.

这样的处理,究竟对性能提升有多大呢,其实不大,而且性能改进也很有限。

我们换一个思路,那就是使用not existsexplain extended select t1.account from t_fund_info t1 where t1.money

>=300 and  not exists (select distinct(t2.account) from

t_user_login_record t2 where t1.account=t2.account and t2.add_time

>="2016-06-01");这种方式在MySQL是如何分解的呢。

select test . t1 . account AS account

from test . t_fund_info t1

where ((test . t1 . money >= 300) and

(not

(exists ( /* select#2 */

select test . t2 . account

from test . t_user_login_record t2

where ((test . t1 . account = test . t2 . account) and

(test . t2 . add_time >= "2016-06-01"))))))

可以看到几乎没有做什么特别的改动。

这一点在5.5,5.6,5.7中都是很相似的处理思路。

当然这种方式相对来说性能提升都不大。一个局限就在于统计信息不够丰富,所以自动评估就会出现很大的差距。

这个地方我们稍放一放,我们添加一个索引之后再来看看。create index ind_account_id2 on t_user_login_record(account);

然后使用not in的方式查看解析的详情。select test . t_fund_info . account AS account

from test . t_fund_info

where ((test . t_fund_info . money >= 300) and

(not (

(test . t_fund_info .

account, 

(

( (test . t_fund_info . account) in t_user_login_record on

ind_account_id2

where((test . t_user_login_record . add_time >= "2016-06-01") and

( (test . t_fund_info . account) = test .

t_user_login_record . account))))))))

可以看到这个方式有了索引,not in和not exits的解析方式很相似。有一个差别就是在子查询外有了的处理方式。

我们来看看两者的差别,同样的步骤,有了索引之后,估算的key_len(使用索引的长度)为182,估算行数为1-----------------+---------+------+---------

key             | key_len | ref  | rows

-----------------+---------+------+---------

NULL            | NULL    | NULL | 1875524

ind_account_id2 | 182     | func |       1

而之前没有索引的时候,这个结果差别就很大了,是190多万。------+---------+------+---------

key  | key_len | ref  | rows

------+---------+------+---------

NULL | NULL    | NULL | 1875524

NULL | NULL    | NULL | 1945902

而顺带看看有了索引之后,not exists的方式是否会有改变。/* select#1 */

select test . t1 . account AS account

from test . t_fund_info t1

where ((test . t1 . money >= 300) and

(not

(exists ( /* select#2 */

select test . t2 . account

from test . t_user_login_record t2

where ((test . t1 . account = test . t2 . account) and

(test . t2 . add_time >= "2016-06-01"))))))

以上可以看出,和没有添加索引的解析方式没有差别。哪里会差别呢,就是执行的估算行数上,有天壤之别。

所以通过这样一个反连接的小例子,可以看出来存在索引的时候,not in会内部转换为not exists的处理方式,而not exists的方式在存在索引和不存在,两者通过执行计划可以看出很大的差别,其中的一个瓶颈点就在于估算的行数。


http://lihuaxi.xjx100.cn/news/276682.html

相关文章

visualstudio发布网站到服务器,发布到网站 - Visual Studio (Windows) | Microsoft Docs

使用 Visual Studio 将 Web 应用发布到网站01/29/2019本文内容可以使用“发布”工具将 ASP.NET、ASP.NET Core、.NET Core 和 Python 应用从 Visual Studio 发布到网站。 对于 Node.js&#xff0c;支持这些步骤但用户界面不同。先决条件安装有 Visual Studio 2019 并具有所选语…

AI拟音师出击,轻松骗过人类观众:你听到的电影音效可能来自它们

机器之心报道编辑&#xff1a;陈萍「我听见雨滴落在青青草地&#xff0c;我听见远方下课钟声响起……」多么浪漫的场景&#xff0c;但你有想过雨滴声和下课钟声是 AI 自动合成的吗&#xff1f;近日&#xff0c;一个叫做 AutoFoley 的机器学习程序横空出世&#xff0c;给电影拟音…

python django web项目的构建步骤(一)

Django 一个开放源代码的Web应用框架&#xff0c;由Python写成。采用了MVC的软件设计模式&#xff0c;即模型M&#xff0c;视图V和控制器C。它最初是被开发来用于管理劳伦斯出版集团旗下的一些以新闻内容为主的网站的。并于2005年7月在BSD许可证下发布。 1、安装好python djang…

导出表结构

http://wenku.baidu.com/view/979d6e3fee06eff9aef80766.html 选择&#xff1a; list of tables list of table columns 指定layout name data type length comment转载于:https://www.cnblogs.com/gaohuag/p/3304874.html

mysql random_Mysql中随机函数笔记

1&#xff0c;测试表结构&#xff1a;mysql> desc test_user;----------------------------------------------------------| Field | Type | Null | Key | Default | Extra |----------------------------------------------------------| id | int(11) | NO | PRI | NULL …

pythonrequest方法_解决Python requests 报错方法集锦

python版本和ssl版本都会导致 requests在请求https网站时候会出一些错误&#xff0c;最好使用新版本。 1 Python2.6x use requests 一台老Centos机器上跑着古老的应用&#xff0c;加了一个新模块之后报错 报错 InsecurePlatformWarning: A true SSLContext object is not avail…

科普:教你如何看懂 JavaGC 日志

点击上方“方志朋”&#xff0c;选择“设为星标”回复”666“获取新整理的面试资料来源&#xff1a;https://url.cn/5cvXPfUJVM GC 相关的参数-XX:PrintGC 输出 GC 日志 -XX:PrintGCDetails 输出 GC 的详细日志 -XX:PrintGCTimeStamps 输出 GC 的时间戳&#xff08;以基准时间的…

卡尔曼滤波:究竟滤了谁?

点击上方“小白学视觉”&#xff0c;选择加"星标"或“置顶”重磅干货&#xff0c;第一时间送达本文转自|AI算法与图像处理在SLAM系统中&#xff0c;后端优化部分有两大流派。一派是基于马尔科夫性假设的滤波器方法&#xff0c;认为当前时刻的状态只与上一时刻的状态有…