Home >Database >Mysql Tutorial >sql not in与left join百万级数据测试比较

sql not in与left join百万级数据测试比较

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-06-07 17:48:592069browse

这各测试也只有在百万级以上的数据库才会有比较明显的区别,我们今天利用我的应用实现来比较sql中not in与left join语句在百万级数据测试,这个很有用的各位朋友可以收藏。

两张表 组织架构表(Organise) 和 工资发放历史记录表 (WagePerMonthHis)

两张表通过 Organise.Item_id 和 WagePerMonthHis.OrgIdS 进行关联

Organise表(以下简称O表)中大约有6000条记录11个字段 ,WagePerMonthHis(以下简称W表)计有 125万条记录 和 25个字段

 

原程序中一段如下的语句

是查询所有不在W表的组织架构层级为2的记录

 代码如下 复制代码

OrgId as 公司编码,OrgName as 公司名称

from Organise

where OrgLev=2

and item_id not in

(select OrgidS from WagesPerMonthHis

where WagesYear='2010' and WagesMonth=

'01' Group by OrgidS,OrgNameS)

order by Orgid

 

语句执行要33秒之久,服务器的配置是比较高的:16核心4CPU,24G内存,且内存和CPU在执行时都没有出现瓶颈,开始以为是

 代码如下 复制代码

(select OrgidS from WagesPerMonthHis

where WagesYear='2010' and WagesMonth=

'01' Group by OrgidS,OrgNameS)  

 这条语句执行缓慢所致,单独执行这条却发现执行速度很快,大约不到2秒就出来了,于是症结出来了,是not in 这个全扫描关键词带来的性能下降.最直接的是导致页面失去响应,一个关键功能使用不了.

 

试了not exist语句,发现效果是一样的,并不象网上所说可以提高很多性能.

 

于是重新优化语句如下

 代码如下 复制代码

select a.OrgId as 公司编码,a.OrgName as 公司名称,a.item_id

from Organise a

left outer join (select distinct b.OrgIdS from WagesPerMonthHis b

where WagesYear='2010' and WagesMonth='01') as b

on a.item_id = b.OrgidS

where a.OrgLev = 2

and b.OrgIdS is Null

order by 公司编码

 

改用左外连接(其实左连接也可以)后,整个语句执行速度为400ms, 33秒与400ms 我想是很多人没想到的.

 

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn