环境:sql server 2005
表B两个字段id(int),name(varchar(10)),在字段id上建立了索引,数据量100万,随即生成的数据。

话题比较查询条件中的in与or性能的比较,比较下面两种语句的性能。
A、select * from a where a.id in (1,2)
B、select * from a where a.id =1 union  select * from a where a.id =2
从网上的资料看,推荐使用第二种方法B。
从执行计划中可以看出语句A的开销占25%,语句B的开销占75%。
语句A对表进行了一次扫描,查找方式索引查找与rid查找。
语句B对表扫描过2次,在每一次扫描中查找方式索引查找与rid查找。

我得出的结论是在这种条件下in的性能要高于or。
实际分析计划如下:
查询性能优化 in and union
查询性能优化 in and union

相关文章:

  • 2021-12-18
  • 2021-11-14
  • 2021-09-19
  • 2021-04-23
  • 2021-05-05
猜你喜欢
  • 2021-12-09
  • 2021-11-14
  • 2022-12-23
  • 2021-12-10
相关资源
相似解决方案