I have a table which maintains performance data of a system, each record is a call made to some important method and consists of the method name, its duration and a token - each request to the system is given a unique token and so all of the records with the same token are the same request, e.g:
CallName Duration Token
----------- ----------- -----------
GetData 121 12345
Process 800 12345
SaveData 87 12345
GetData 97 ABCDE
Process 652 ABCDE
SaveData 101 ABCDE
I am interested in aggregate data grouped by the Token and the CallName, for example:
-- The total duration of each request, in descending order
SELECT Token, SUM(Duration) FROM Requests GROUP BY Token ORDER BY SUM(Duration) DESC
-- The average duration of each call, in descending order
SELECT CallName, AVG(Duration) FROM Requests GROUP BY CallName ORDER BY AVG(Duration) DESC
Now this table is potentially very large and I'm only ever going to be interested in the top few records of each query and so I've implemented paging for both of these queries. The trouble is that because these queries involve aggregate functions SQL server ends up doing a table scan anyway.
Surely other people have had this problem before?
What I really need here is an "index" on SUM(Duration) grouped by Token, i.e. a table where I can do things like:
SELECT Token, SumToken FROM RequestTokens ORDER BY SumToken DESC
- Is this a really bad idea?
- If so, is there a better way?
- What would be the best way to do this? Would triggers on INSERT / UPDATE / DELETE work (where I update the aggregate values based on the old values and the changed data), or would I be better off manually updating my "index" when updating this table?
Triggers are the best solution I've come up with so far, but I can already see this being a deadlock / consistency nightmare! :-S