How do you fix queries that only run slow until they're cached How do you fix queries that only run slow until they're cached sql-server sql-server

How do you fix queries that only run slow until they're cached


May I suggest that you inspect the execution plan for the queries that are responsible for your poor performance issues.

You need to identify, within the execution plan, which steps have the highest cost and why. It could be that your queries are performing a table scan, or that an inappropriate index is being used for example.

There is a very detailed, free ebook available from the RedGate website that concentrates specifically on understanding the contents of execution plans.

https://www.red-gate.com/Dynamic/Downloads/DownloadForm.aspx?download=ebook1

You may find that there is a particular execution plan that you would like to be used for your query. You can force which execution plan is used for a query in SQL Server using query hints. This is quite an advanced concept however and should be used with discretion. See the following Microsoft White Paper for more details.

http://www.microsoft.com/technet/prodtechnol/sql/2005/frcqupln.mspx

I would also not recommend that you clear the procedure cache on your production environment as this will be detrimental to the performance of all other queries on the platform that are not currently experience performance issues.

If you are executing a stored procedure for example you can ensure that a new execution plan is calculated for each execution of the procedure by using the WITH RECOMPILE command.

For overall performance tuning information, there are some excellent resources over at Brent Ozar’s blog.

http://www.brentozar.com/sql-server-performance-tuning/

Hope this helps. Cheers.


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:

DBCC DROPCLEANBUFFERS   DBCC FREEPROCCACHE  

EDIT: I checked with the SQL Server documentation I have and this is at least true for SQL Server 2000.


Use can use

DBCC DROPCLEANBUFFERS   DBCC FREEPROCCACHE

But only use this in your development environment whilst tuning the queries for deployment to a live server.