I have a basic grid with paging enabled in my web application. This grid is populated with SQL data via the web API using Dapper. In my API controller, I run two separate requests: one to retrieve the rows (which are shown in my grid), and one to retrieve the total number of entries (to be displayed in my paging controls). And it works. However, I am trying to optimize my queries.
My first query, which retrieves rows, returns only 50 rows at a time (using OFFSET and FETCH to provide a search call:
SELECT DISTINCT T_INDEX.* FROM T_INDEX INNER JOIN T_INDEXCALLER ON T_INDEX.IndexId = T_INDEXCALLER.IndexId WHERE...
My second query retrieves the number of ALL rows, but uses the same tables, the same joins, and the same WHERE :
SELECT COUNT(DISTINCT T_INDEX.IndexId) FROM T_INDEX INNER JOIN T_INDEXCALLER ON T_INDEX.IndexId = T_INDEXCALLER.IndexId WHERE...
As I said, this works. And it takes about 2.5 seconds per request, for a total of 5 + seconds. A lag is not the end of the world, in any way, but I would like to cut this time in half.
I wanted to know if there is a way to get 50 rows and get the total number of ALL rows in one query. I understand that these two questions do two separate things. But I think there might be a βwayβ to customize these two queries and combine them into one, since the table, join, and WHERE clauses are identical between them.
source share