How can I clear the SQL Server query cache?


Scenario

When conducting performance testing and tuning on a new system, most of the time a number of options are outlined to potentially correct the performance problem.  To determine the best overall solution, each option is tested and the results are recorded.  As lessons are learned options may be combine for a better end result and often as data is cached the overall query performance improves.  Unfortunately, with the data in cache testing each subsequent option may lend itself to an apples to oranges comparison.  How can I ensure during each execution of a new set of code that the data is not cached?

 

Solution

If all of the performance testing is conducted in SQL Server the best approach may be to issue a CHECKPOINT and then issue the DBCC DROPCLEANBUFFERS command.  Although the CHECKPOINT process is an automatic internal system process in SQL Server and occurs on a regular basis, it is important to issue this command to write all of the dirty pages for the current database to disk and clean the buffers.  Then the DBCC DROPCLEANBUFFERS command can be executed to remove all buffers from the buffer pool.  Here is a quick code snippet to serve as an example:

USE <YOURDATABASENAME>; GO CHECKPOINT; GO DBCC DROPCLEANBUFFERS; GO

Although the CHECKPOINT and  DBCC DROPCLEANBUFFERS commands seem to be the most elegant approach because they can be included in your T-SQL test scripts, you also can achieve the same results by either restarting the SQL Server instance or restarting Windows.  If you are testing via a batch file (or similar) then you could issue ‘net stop mssqlserver’ and ‘net start mssqlserver’ DOS commands.  As a side note, you also have the option to shutdown SQL Server via the T-SQL SHUTDOWN command, but would need to restart the services via either the ‘net start’ command or via one of the GUI tools.  Although these options are possible, they are not recommended.  These last set of commands will shut down your SQL Server instance or machine, which is probably unneeded.

A few words of caution…

It is not recommended to issue the CHECKPOINT\DBCC DROPCLEANBUFFERS, the ‘net stop mssqlserver’, T-SQL SHUTDOWN command or restarting Windows on production systems just for the sake of testing.  These commands could have detrimental results to your environment.  It is recommended to only issue these types of commands in testing environments with coordination among your team due to the impact to the overall SQL Server.  In addition, keep in mind that if you do issue these commands only in test environments that if multiple tests are being conducted simultaneously issuing the CHECKPOINT and DBCC DROPCLEANBUFFERS commands may skew results for other testers.

 

Thanks for reading this article,

Next steps :

  1. Add this article to your database toolkit
  2. Share this with your colleagues because Sharing is Learning
  3. Comment below if you need any assistance