我创建了一个临时表,用于测试intindex上的主键。
当我做select * from #TmpDashboard where intindex = 1时,我得到索引查找。
但是,当我应用分页公式时,会得到索引扫描,这会导致性能问题。
Declare @Currentpage INT = 1
,@Pagesize INT = 10
select * from #TmpDashboard
WHERE (@Pagesize = 0 OR (intIndex BETWEEN ((@CurrentPage - 1) * @PageSize) + 1 AND (@CurrentPage * @PageSize))) --gives index scan如何转换上述公式,得到索引查找。
发布于 2014-07-03 18:57:21
WHERE子句中的"OR“导致扫描,因为它使语句不确定。我建议设置第二组变量,可以用来简化查询,如下所示:
Declare @Currentpage INT = 1
,@Pagesize INT = 10;
declare @start int = Case when @pagesizee = 0 then 0 else ((@CurrentPage - 1) * @PageSize) + 1 end
, @end int = case when @pagesize > 0 then @CurrentPage * @PageSize else (select max(intindex) from #tmpdashboard) end
select * from #TmpDashboard
WHERE intindex between @start and @end发布于 2014-07-03 17:02:28
create table #TmpDashboard (intindex int primary key)
select * from #TmpDashboard where intindex = 1
Declare @Currentpage INT, @Pagesize INT;
select @Currentpage= 1, @Pagesize = 10;
select * from #TmpDashboard
WHERE intIndex BETWEEN ((@CurrentPage - 1) * @PageSize) + 1 AND (@CurrentPage * @PageSize)
drop table #TmpDashboardhttps://stackoverflow.com/questions/24559310
复制相似问题