Tuesday, 25 July 2017

MSSQL - How to clear all caches?

Script to clear all caches? (For example for performance debug)

/*
https://yabele.blogspot.com/2017/07/mssql-how-to-clear-all-caches.html
*/
SET NOCOUNT ON;
DECLARE @StartedAt DATETIME2 = GETDATE();
CHECKPOINT; 
DBCC DROPCLEANBUFFERS;
DBCC FREESYSTEMCACHE ('ALL');
-- Query
PRINT '    Elapsed time: ' + CAST(DATEDIFF(ms, @StartedAt, GETDATE()) AS VarChar(20)) + ' ms';