I have some queries that are causing timeouts in our live environment. (>30 seconds)
If I run profiler and grab the exact SQL being run and run it from Management Studio then they take a long time to run the first time and then drop to a few hundred miliseconds each run after that.
This is obviously SQL caching the data and getting it all in memory.
I’m sure there are optimisations that can be made to the SQL that will make it run faster.
My question is, how can I ‘fix’ these queries when the second time I run it the data has already been cached and is fast?
According to http://morten.lyhr.dk/2007/10/how-to-clear-sql-server-query-cache.html, you can run the following to clear the cache:
EDIT: I checked with the SQL Server documentation I have and this is at least true for SQL Server 2000.