select * from ( select top 10 [Title],[AddTime] from [NewS] where [CategoryID]=6 and [Area]=@Area and [State]=1 order by [Id] desc)t1 union all select * from ( select top 10 [Title],[AddTime] from [NewS] where [CategoryID]=7 and [Area]=@Area and [State]=1 order by [Id] desc)t2这样会快点不?
需要查詢執行計劃有沒有用到CategoryID的索引
Union好像只对索引列有效。 而且三楼的不对哦。还有没有其他办法
不会快. 还不如or或IN呢.
select [t2.Title],[t2.AddTime] from (select 6 as CategoryID union select 7 as CategoryID) as t1 cross join [NewS] t2 on (t1.CategoryID=t2.CategoryID)
select [t2.Title],[t2.AddTime] from (select 6 as CategoryID union select 7 as CategoryID) as t1 cross join [NewS] t2 on (t1.CategoryID=t2.CategoryID)
select * from (
select top 10 [Title],[AddTime] from [NewS]
where [CategoryID]=6 and [Area]=@Area and [State]=1 order by [Id] desc)t1
union all
select * from (
select top 10 [Title],[AddTime] from [NewS]
where [CategoryID]=7 and [Area]=@Area and [State]=1 order by [Id] desc)t2这样会快点不?
而且三楼的不对哦。还有没有其他办法
不会快. 还不如or或IN呢.
(select 6 as CategoryID
union
select 7 as CategoryID) as t1
cross join [NewS] t2
on (t1.CategoryID=t2.CategoryID)
(select 6 as CategoryID
union
select 7 as CategoryID) as t1
cross join [NewS] t2
on (t1.CategoryID=t2.CategoryID)
把or改成union all的写法,并不能提高效率。
在索引利用上做文章