I am using latest SQL Server. In my DB, I have close to one million rows, the below pagination query is taking 8 seconds to return data. When I remove totalcount column, its returning in 1 sec, so any better approach to get totalcount in same query with better performance?
Note:For brevity, I put my query as below where I do have filters and all for the below query
DECLARE @pageNum INT = 1;
DECLARE @pageSize INT = 25;
select
t1.col1,t1.col2,t2.col3,t2.col4
,count(t1.col1) over() as totalCount
from mytable t1
left join mysecondtable t2 on t1.col1=t2.col3
ORDER BY t1.col1
OFFSET (@pageNum-1)*@pageSize ROWS
FETCH NEXT @pageSize ROWS ONLY;