[英]Get index seek instead of scan in paging temp table?
I've created a temporary table for testing I've primary key on intindex. 我已经创建了一个临时表来测试我对intindex的主键。
When I do select * from #TmpDashboard where intindex = 1
I get index seek. 当我select * from #TmpDashboard where intindex = 1
我得到了索引查找。
but when I apply paging formula I get index scan which causing performance issues. 但是,当我应用分页公式时,会得到索引扫描,这会导致性能问题。
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
How can i convert above formula to get index seek. 我如何转换上面的公式以获得索引查找。
The "OR" in the WHERE clause is causing the scan because it makes the statement nondeterministic. WHERE子句中的“ OR”导致扫描,因为它使该语句不确定。 I would suggest to set a second set of variables that you can use to simplify the query, like this: 我建议设置第二组变量,您可以使用它们来简化查询,如下所示:
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
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 #TmpDashboard
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.