#databases #sqlserver #technicalDebt #entityFramework
So... At work, our product has many data grids, and uses Entity Framework, most of the grids are paged.
Paging is done via await orderedQuery. Skip((page-1)*pageSize). Take(pageSize).TolistAsync()
Page 2 loads had always been slow. EF "optimises" the generated query to use one parameter for the second page because page 2 is ... .Skip((1-1)*pageSize).Take(pageSize) ...
Both resolve to the same value. EF says "same value, same parameter!" Find whatever.
Except... SQL server builds and uses a different (and more costly) plan for the resulting OFFSET \@p ROWS LIMIT \@p that is consistently worse than it they had didn't values that produces OFFSET \@p1 ROWS LIMIT \@p2
Apparently it's been a known problem for a long time.
Until someone as dumb as me asks... Why not Skip as much as normal and Take one extra then don't use it the extra.
(await orderedQuery. Skip((page-1)*pageSize). Take(pageSize+1).TolistAsync()).Take(pageSize)
/1



