I have a general software practice question, and maybe a moderator could better phrase my title.
I’m loading out a list of paged results for a “Manage Users” page that has a bunch of filters.
In my system, there are 3000 users, and I want to show 25 per page, with a pagination control at the bottom. No problem. Here’s the issue…
I want to show at the top of the page, that there say 2300 matches for my criteria, and so what I’m doing is running the same SQL query twice — once with a limit of 25 records to build into objects (for pagination), and then again to get the unconstrained total count.
This seems non-performant. Is there a preferred way of doing this without having to run TWO SQL queries?
I solved a similar problem and went with the two query approach: the first query retrieved the current page of data (i.e., 25 rows), while the other one was used to retrieve the total count.
From your question it seems as though you are retrieving the entire dataset in your second query. As others have said, you will be much better off retrieving only the row count instead.
Grabbing only the row count in that second query should speed things up, but if you wanted to avoid running it each time you load some data, you could instead run it just once when the page is initially loaded, storing the result in the session or viewstate. This does of course mean that the count could become out of date if the number of rows changes, so you would need to evaluate the merits of accuracy against performance in this case.