mysql关于select where in的效率

很多大牛公司建议select where in中不要超过200,有的说不要超过500.
那么问题来了,使用子查询的时候如果结果级超过了500呢。
今天做了个测试

下面是测试结果,分别是两个语句查询10次所用的时间
select * from cp_bill_info where id in(select id from cp_bill_info where id< 501)

第*次查询耗时
10.434s
20.468s
30.456s
40.460s
50.451s
60.435s
70.427s
80.481s
90.482s
100.520s

select * from cp_bill_info where id in(1,2,3,……500)

第*次查询耗时
10.559s
20.565s
30.554s
40.549s
50.550s
60.550s
70.541s
80.493s
90.562s
100.531s

从结果来看用子查询的效率要优于直接写入查询范围。
另外还测试了

select * from cp_bill_info where id in(select id from cp_bill_info where id< 201)
select * from cp_bill_info where id in(select id from cp_bill_info where id< 101)
select * from cp_bill_info where id in(select id from cp_bill_info where id< 1001)

10次的耗时也维持在0.4**s也就是说子查询对于范围的影响很小。
但是使用写入查询范围的方式,当范围越多时,耗时会随之增加。

select * from cp_bill_info where id in(1,2,3,……1000)

用时在0.8**s。

select * from cp_bill_info where id in(1,2,3,……100)
select * from cp_bill_info where id in(1,2,3,……200)

比500的范围缩小用时在0.03左右。

总结:select where in使用子查询时,子查询中结果集的大小对查询速度影响很小。但是直接写入查询范围的时候最好控制在500个以内,越多效率越低

  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值