SQL:测试比较exists和in的执行效率

目录

1.select 具体字段和select *的效率

2.exists 和 in的效率(关联字段都是索引列)


 

任务表(Task)中,大概有30列,大概有35万的数据量。

1.select 具体字段和select *的效率

set statistics time on

select * from  x.dbo.et_task

select id,name from  x.dbo.et_task

select id,name,createtime,lastupdatetime from  x.dbo.et_task

set statistics time off

查询结果集是345789行

 

Select *

71671 ms

70068 ms

85452 ms

Select id,name

13793 ms

15803 ms

12520 ms

Select

17281 ms

17096 ms

17678 ms

 

可以看出,select 会检索所有行,随着列的增加,时间也会随之增加。

 

2.exists 和 in的效率(关联字段都是索引列)

 

这时候我需要引出一个项目表(project)和任务明细表(taskassignmentdetail),项目表大概有311条数据,任务明细表大概有65W条数据,我们利用大表和小表的嵌套测试下对应的效率。

我们需要查询正在执行状态的项目对应的任务的数据

set statistics time on

select * from x.dbo.et_task a  where exists (select id from   x.dbo.et_taskassignmentdetail b where a.id=b.taskid)

select * from x.dbo.et_task a  where id in  (select taskid from   x.dbo.et_taskassignmentdetail b)

set statistics time off

set statistics time on

select * from x.dbo.et_task a where exists  (select id  from x.dbo.et_project  b where a.projectid=b.id)

select  * from x.dbo.et_task a where projectid in (select id  from x.dbo.et_project  b)set

statistics time off
 

et_task(A 大)

et_project(B 小)

et_task(A小)

et_task_detail (B大)

exists

5781

2533 ms

exists

4939

2469 ms

exists

3966

2388 ms

in

5976

2507 ms

in

4578

2463 ms

in

3298

2301 ms

比较下来,其实in和exists的效率差不多。

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值