In my app I need to display search results. It fetches the first 250 results, but also needs to display the total number of results.
This slows everything down, because I can't just stop fetching results after the first 250 rows, I need to loop through them all (millions) just to count them.
Another approach is to exit the loop after the first 250 rows, and do a second COUNT() query after that. But some quick benchmarking showed that the COUNT() query doesn't take advantage of the fact that it has executed the same query before.
Is there any way to optimize a scenario like this? Would it help if I combined the counting and the fetching in one query instead of two seperate ones? And if so, how would I do that?
Edit: Sorry, duplicate of How do I count the number of rows returned in my SQLite reader in C#?