Sunday, January 29, 2012

How to flush cache in Oracle and MS SQL Server

Sometimes we need to test the performance of queries with a cold buffer cache without shutting down and restarting the database server. 

Oracle
alter system flush shared_pool flushes the library cache in Oracle SGA.
alter system flush buffer_cache flushes the buffer cache in the SGA. This command is not available prior to 10g. 
Oracle 9i had an undocumented command to flush the buffer cache,
alter session set events = 'immediate trace name flush_cache';

MS SQL Server 
DBCC FREEPROCCACHE removes all elements from the cache in SQL Server.
CHECKPOINT; DBCC DROPCLEANBUFFERS flushes the buffers in SQL Server. Please notice that DBCC DROPCLEANBUFFERS only removes clean buffers from the buffer pool. We need to run CHECKPOINT first to force all dirty pages in the buffer to be written to the disk and then remove all clean buffer pages.