I have a table like
FieldA, FieldB, FieldC, FieldD, TheDate, Count
I have a web app which provides a dashboard of the top few Count
by each of the fields. Not wanting to prematurely optimize, the original, brute force queries for these charts are like:
SELECT TOP 10
FieldA,
SUM(Counts) AS Counts
FROM TheTable
WHERE @StartDate <= TheDate AND TheDate <= @EndDate
GROUP BY FieldA
ORDER BY Counts
And the same for the other fields. But the server ends up selecting by date range independently for each chart and when there is a lot of data, the system is bogging down.
It seems wrong to get all the data in the app (once) then do the summary by the columns locally. And maybe the RDBMS caches a lot of the records so the second through fourth chart are more efficient that the first.
I'm using Azure SQL and neither SQL Server Management Studio nor DataGrip suggest any missing indices which could help.
Any thoughts on techniques to do multiple, similar options on the same data? In general or for Azure SQL. Thanks.