SQL Server commands to clear caches before running a performance comparison
Personally, for a common query the 2nd and subsequent executions matter more.
Are you testing disk IO or query performance?
Assuming your query runs often and is critical, then you want to measure that under real life conditions. And you don't want to clear prod server caches each time...
If you want, you can:
DBCC DROPCLEANBUFFERS
clears clean (unmodified) pages from the buffer pool
Precede that with aCHECKPOINT
to flush any dirty pages to disk firstDBCC FLUSHPROCINDB
clears execution plans for that database
Also see (on DBA.SE)
- Solving high page load time using SQL Profiler
Late answer but may be of use to other readers
DBCC DROPCLEANBUFFERS
is an often used command for query testing and gauging query execution speed. This command (when run) leaves behind only the dirty pages, which is actually a small portion of data. It removes all the clean pages for an entire server.
Be advised that this command should not be run on production environment. Running this command will result in mostly empty buffer cache. Running any query after executing the DBCC DROPCLEANBUFFERS
command, will use physical reads to bring back the data into the cache, which is very likely going to be a lot slower than memory.
Again, treat this command similarly to DBCC FREEPROCCACHE
- it should not be run on any production server unless you absolutely know what you are doing.
This can be a useful development tool because you can run a query in a performance testing environment over and over without any changes in speed/efficiency due to caching of data in memory.
Learn more at: http://www.sqlshack.com/insight-into-the-sql-server-buffer-cache/
I was always told to use:
dbcc dropcleanbuffers;
From MSDN:
Use DBCC DROPCLEANBUFFERS to test queries with a cold buffer cache without shutting down and restarting the server.
To drop clean buffers from the buffer pool, first use CHECKPOINT to produce a cold buffer cache. This forces all dirty pages for the current database to be written to disk and cleans the buffers. After you do this, you can issue DBCC DROPCLEANBUFFERS command to remove all buffers from the buffer pool.