I've created a temporary table for testing I've primary key on intindex.
When I do select * from #TmpDashboard where intindex = 1
I get index seek.
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. 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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.