SQLServer 优化SQL语句 in 和not in的替代方案

感兴趣的小伙伴,下面一起跟随编程之家 jb51.cc的小编两巴掌来看看吧!

但是用IN的sql性能总是比较低的,从sql执行的步骤来分析用IN的sql与不用IN的sql有以下区别:
sql试图将其转换成多个表的连接,如果转换不成功则先执行IN里面的子查询,再查询外层的表记录,如果转换成功则直接采用多个表的连接方式查询。由此可见用IN的sql至少多了一个转换的过程。一般的sql都可以转换成功,但对于含有分组统计等方面的sql就不能转换了。 推荐在业务密集的sql当中尽量不采用IN操作符
NOT IN 此操作是强列推荐不使用的,因为它不能应用表的索引。推荐用NOT EXISTS 或(外连接+判断为空)方案代替
  在数据库中有两个表,一个是当前表Info(id,PName,remark,impdate,upstate),一个是备份数据表bakInfo(id,upstate),将当前表数据备份到备份表去,就涉及到not in 和in 操作了:
  首先,添加10万条测试数据

代码如下:

 
create procedure AddData 
as 
declare @id int 
set @id=0 
while(@id<100000) 
begin 
insert into dbo.Info(id,upstate) 
values(@id,convert(varchar,@id)+'0','abc',getdate(),0) 
set @id=@id+1 
end 
exec AddData 

使用not in 和in操作:

代码如下:

 
SET STATISTICS TIME ON 
GO 
--备份数据 
insert into bakInfo(id,upstate) 
select id,upstate from dbo.Info 
where id not in(select id from dbo.bakInfo) 
GO 
SET STATISTICS TIME OFF 

此操作执行时间:

代码如下:

 
sql Server 分析和编译时间: 
cpu 时间 = 0 毫秒,占用时间 = 3 毫秒。 
sql Server 执行时间: 
cpu 时间 = 453 毫秒,占用时间 = 43045 毫秒。 
(100000 行受影响) 
sql Server 分析和编译时间: 
cpu 时间 = 0 毫秒,占用时间 = 1 毫秒。 
--更改当前表状态 
update Info set upstate=1 where id in(select id from dbo.bakInfo) 

  此操作执行时间:

代码如下:

 
sql Server 分析和编译时间: 
cpu 时间 = 62 毫秒,占用时间 = 79 毫秒。 
sql Server 执行时间: 
cpu 时间 = 188 毫秒,占用时间 = 318 毫秒。 
(100000 行受影响) 
sql Server 分析和编译时间: 
cpu 时间 = 0 毫秒,占用时间 = 1 毫秒。 
--删除当前表数据 
delete from Info where upstate=1 and id in(select id from dbo.bakInfo) 

  此操作执行时间:

代码如下:

 
sql Server 分析和编译时间: 
cpu 时间 = 183 毫秒,占用时间 = 183 毫秒。 
sql Server 执行时间: 
cpu 时间 = 187 毫秒,占用时间 = 1506 毫秒。 
(100000 行受影响) 
sql Server 分析和编译时间: 
cpu 时间 = 0 毫秒,占用时间 = 1 毫秒。 

  使用join连接替代方案:

代码如下:

 
SET STATISTICS TIME ON 
GO 
--备份数据 
insert into bakInfo(id,upstate from 
(SELECT Info.id,Info.PName,Info.remark,Info.impdate,Info.upstate,bakInfo.id AS bakID 
FROM Info left JOIN 
bakInfo ON Info.id = bakInfo.id ) as t 
where t.bakID is null and t.upstate=0 
GO 
SET STATISTICS TIME OFF; 

  此操作执行时间:

代码如下:

 
sql Server 分析和编译时间: 
cpu 时间 = 247 毫秒,占用时间 = 247 毫秒。 
sql Server 执行时间: 
cpu 时间 = 406 毫秒,占用时间 = 475 毫秒。 
(100000 行受影响) 
sql Server 分析和编译时间: 
cpu 时间 = 0 毫秒,占用时间 = 1 毫秒。 
--更改当前表状态 
update Info set upstate=1 
FROM Info INNER JOIN 
bakInfo ON Info.id = bakInfo.id 

  此操作执行时间:

代码如下:

 
sql Server 分析和编译时间: 
cpu 时间 = 4 毫秒,占用时间 = 4 毫秒。 
sql Server 执行时间: 
cpu 时间 = 219 毫秒,占用时间 = 259 毫秒。 
(100000 行受影响) 
sql Server 分析和编译时间: 
cpu 时间 = 0 毫秒,占用时间 = 1 毫秒。 

--删除当前表数据

代码如下:

 
delete from Info 
FROM Info INNER JOIN 
bakInfo ON Info.id = bakInfo.id 
where Info.upstate=1 

  此操作执行时间:

代码如下:

 
sql Server 分析和编译时间: 
cpu 时间 = 177 毫秒,占用时间 = 177 毫秒。 
sql Server 执行时间: 
cpu 时间 = 219 毫秒,占用时间 = 550 毫秒。 
(100000 行受影响) 
sql Server 分析和编译时间: 
cpu 时间 = 0 毫秒,占用时间 = 1 毫秒。 

  可以看出使用join方案比使用not in 和in执行时间要短很多了

相关文章

SELECT a.*,b.dp_name,c.pa_name,fm_name=(CASE WHEN a.fm_n...
if not exists(select name from syscolumns where name=&am...
select a.*,pano=a.pa_no,b.pa_name,f.dp_name,e.fw_state_n...
要在 SQL Server 2019 中设置定时自动重启,可以使用 Window...
您收到的错误消息表明数据库 &#39;EastRiver&#39; 的...
首先我需要查询出需要使用SQL Server Profiler跟踪的数据库标...