Clean Your Buffers for Accurate Performance Testing | Database Journal

Clean Your Buffers for Accurate Performance Testing

Written By
Gregory Larsen
Gregory Larsen
Mar 2, 2017
2 minute read

In order to do accurate performance testing between multiple runs of a SQL Server command or scripts you need to remember to clean the buffer, procedure and system cache between each test run.  Without cleaning the buffer and caches between runs, your test query may run faster because some or all of the data needed is found in memory.  To accomplish cleaning/clearing the buffers and caches you will need to run a number of different commands.

The first thing you want to do is to flush all the dirty pages from the buffer cache.  Dirty pages are those pages that have been updated in the buffer pool but have not been written to disk.  To get these dirty pages out of the buffer pool you just need to run a CHECKPOINT command.  By running the CHECKPOINT command all dirty pages for the current database will be written to disk. 

The second thing is to remove all the pages from the buffer pool.  You want to do this so that each test run will not find any existing pages in the buffer pool.  By cleaning the buffer pool before each test run SQL Server will have to re-read the data it needs from disk.  To clean the buffer pool you execute the command: DBCC DROPCLEANBUFFERS. 

Next you should remove your execution plans from the procedure cache.  To remove all the plans from the procedure cache you can run the “DBCC FREEPROCCACHE” statement.  Or you can pass parameters, like a plan handle to remove just a single cached plan.  I find it easiest to remove all cached plans, by just running the “DBCC FREEPROCCACHE” statement. Of course, I only do this in an isolated testing environment, because this removes all plan and cache information for all batches that are running, and not just my test queries.

Lastly you should consider clearing the system caches as well.  To accomplish this, you can run the “DBCC FREESYSTEMCACHE(‘ALL’)” statement.   Cleaning this cache, as well as all the other items discussed, will allow you to compare your execution performance between multiple runs of your TSQL command or scripts.

See all articles by Greg Larsen

Gregory Larsen

Gregory A. Larsen is a DBA at Washington State Department of Health (DOH). Greg is responsible for maintaining SQL Server and other database management software. Greg works with customers and developers to design and implement database changes, and solve database/application related problems. Greg builds homegrown solutions to simplify and streamline common database management tasks, such as capacity management.

Database Journal Logo

DatabaseJournal.com publishes relevant, up-to-date and pragmatic articles on the use of database hardware and management tools and serves as a forum for professional knowledge about proprietary, open source and cloud-based databases--foundational technology for all IT systems. We publish insightful articles about new products, best practices and trends; readers help each other out on various database questions and problems. Database management systems (DBMS) and database security processes are also key areas of focus at DatabaseJournal.com.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.