I have a question about the fastest way to perform a SQL Server query on a table, TheTable, that has the following fields: TimeStamp, Col1, Col2, Col3, Col4
I don’t maintain the database, I just can access it. I need to perform 10 calculations that are similar to:
Col2*Col3 + 5
5*POWER(Col3,7) + 4*POWER(Col2,6) + 3*POWER(Col1,5)
Then I have to find the AVG and MAX of the calculation results using data from a chosen day (there is 8 months of data in the database so far). Since the data are sampled every 0.1 seconds, 864000 rows go into each calculation. I want to make sure that the query runs as quickly as possible. Is there a better way than this:
SELECT AVG(Col2*Col3 + 5),
AVG(5*POWER(Col3,7) + 4*POWER(Col2,6) + 3*POWER(Col1,5)),
MAX(Col2*Col3 + 5),
MAX(5*POWER(Col3,7) + 4*POWER(Col2,6) + 3*POWER(Col1,5))
FROM TheTable
WHERE TimeStamp >= '2010-08-31 00:00:00:000'
AND TimeStamp < '2010-09-01 00:00:00:000'
Thanks!
You could create those as computed (calculated) columns, and set
Is Persistedto true. That will persist the calculated value to disk on insert, and make subsequent queries against those values very quick.Alternately, if you cannot modify the table schema, you could create an Indexed View that calculates the values for you.