sql 效率问题

a277467307 2015-11-26 09:58:09
itemtable 百万数据

IndexId 是主键

CategoryId 和 ItemId 联合索引

方法一

select * from ItemTable where IndexId > 100 and IndexId < 10000 and CategoryId = 130 and ItemId in ( 130, 140, 150)
union
select * from ItemTable where IndexId > 100 and IndexId < 10000 and CategoryId = 140 and ItemId in ( 130, 140, 150)
union
select * from ItemTable where IndexId > 100 and IndexId < 10000 and CategoryId = 140 and ItemId in ( 130, 140, 150)


方法二
select * from ItemTable where IndexId > 100 and IndexId < 10000 and ( (CategoryId = 130 and ItemId in ( 130, 140, 150)) or (CategoryId = 140 and ItemId in ( 130, 140, 150)) or (CategoryId = 150 and ItemId in ( 130, 140, 150)) )

哪种效率高,还有什么好的办法
...全文
158 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
引用 7 楼 a277467307 的回复:
[quote=引用 6 楼 yupeigu 的回复:] 可以把单个语句拆成这样:
select * from ItemTable where IndexId > 100 and IndexId < 10000 and CategoryId = 130 and ItemId =130
union all
select * from ItemTable where IndexId > 100 and IndexId < 10000 and CategoryId = 130 and ItemId =140
union all
select * from ItemTable where IndexId > 100 and IndexId < 10000 and CategoryId = 130 and ItemId =150
另外,你确定需要union吗,是否可以换成union all,因为我看 CategoryId 都是不一样的,难道这个字段值不一样,itemid一样,会事同一条记录?
我只是举个例子,categoryId是一大类,itemId是一小类,小类里面有相同的肯定的啊。就是国家有多少个省,有很多重名的。[/quote] 但是你是select *,而indexid又是主键,那就不可能重复了。。。
a277467307 2015-11-27
  • 打赏
  • 举报
回复
引用 6 楼 yupeigu 的回复:
可以把单个语句拆成这样:
select * from ItemTable where IndexId > 100 and IndexId < 10000 and CategoryId = 130 and ItemId =130
union all
select * from ItemTable where IndexId > 100 and IndexId < 10000 and CategoryId = 130 and ItemId =140
union all
select * from ItemTable where IndexId > 100 and IndexId < 10000 and CategoryId = 130 and ItemId =150
另外,你确定需要union吗,是否可以换成union all,因为我看 CategoryId 都是不一样的,难道这个字段值不一样,itemid一样,会事同一条记录?
我只是举个例子,categoryId是一大类,itemId是一小类,小类里面有相同的肯定的啊。就是国家有多少个省,有很多重名的。
a277467307 2015-11-26
  • 打赏
  • 举报
回复
引用 2 楼 u010192842 的回复:
您把两个语句放到查询分析器中,然后一起执行看执行计划~~一目了然!
关键我还没有那么多数据。
a277467307 2015-11-26
  • 打赏
  • 举报
回复
引用 1 楼 szx1999 的回复:
没啥区别。 方法一中没必要用union,因为CategoryId条件不同,所以结果不需要去重,用union all即可,效率会高些。不过还不如直接用方法二。
这是我想到的两种办法,不知道有没有更好的办法。
Yole 2015-11-26
  • 打赏
  • 举报
回复
您把两个语句放到查询分析器中,然后一起执行看执行计划~~一目了然!
等不到来世 2015-11-26
  • 打赏
  • 举报
回复
没啥区别。 方法一中没必要用union,因为CategoryId条件不同,所以结果不需要去重,用union all即可,效率会高些。不过还不如直接用方法二。
  • 打赏
  • 举报
回复
可以把单个语句拆成这样:
select * from ItemTable where IndexId > 100 and IndexId < 10000 and CategoryId = 130 and ItemId =130
union all
select * from ItemTable where IndexId > 100 and IndexId < 10000 and CategoryId = 130 and ItemId =140
union all
select * from ItemTable where IndexId > 100 and IndexId < 10000 and CategoryId = 130 and ItemId =150
另外,你确定需要union吗,是否可以换成union all,因为我看 CategoryId 都是不一样的,难道这个字段值不一样,itemid一样,会事同一条记录?
等不到来世 2015-11-26
  • 打赏
  • 举报
回复
语句本身没啥好优化的了。 等有数据了,试试 CategoryId in (130, 140, 150) and ItemId in ( 130, 140, 150)的写法,看是否能走组合索引,能走的话,写法就可以简化成这样了。

27,579

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧