SQL SERVER – Stored Procedure – Clean Cache and Clean Buffer

Clean Cache commands affect different parts of SQL Server memory. I separate buffer-pool testing from execution-plan testing.

A gouache ceramic workshop separates wet ceramic pieces in a basin from dry pattern molds in a fitted wooden rack.

The buffer pool holds database pages. The plan cache holds reusable execution plans. Clearing one doesn’t clear the other. I check which cache a test requires before running a command.

-- Isolated test instance only. These commands can affect unrelated workloads.
CHECKPOINT;
DBCC DROPCLEANBUFFERS;
DBCC FREEPROCCACHE;

CHECKPOINT writes dirty pages for the current database. DROPCLEANBUFFERS removes clean buffers and supported columnstore cached objects. It does not mean every dirty page from every database has first been written.

FREEPROCCACHE evicts plans. Later executions may compile again. That cost affects other work on a shared instance and can make a timing comparison misleading.

Keep the test conditions comparable

Run the same query and data under documented warm and cold conditions. Record logical reads, physical reads, elapsed time, and the plan. Don’t assume a faster second run proves that changing the query was responsible.

These commands don’t measure application performance by themselves. I keep the query, data and other test conditions comparable. Otherwise, a warmer cache can receive credit for an improvement the query never made.

Reference: DBCC DROPCLEANBUFFERS.

Related reading

Cache clearing is not a tuning shortcut, it is a controlled test condition.

Published by Pinal Dave on SQLAuthority. More of my work at pinaldave.com.


Discover more from SQL Authority with Pinal Dave

Subscribe to get the latest posts sent to your email.

SQL Scripts, SQL Server, SQL Server DBCC, SQL Server Security, SQL Stored Procedure, SQL Utility
Previous Post
SQL SERVER – Fix: Error Msg 128 The name is not permitted in this context. Only constants, expressions, or variables allowed here. Column names are not permitted.
Next Post
SQL SERVER – @@IDENTITY vs SCOPE_IDENTITY() vs IDENT_CURRENT – Retrieve Last Inserted Identity of Record

Related Posts

24 Comments. Leave new

  • Thanks for useful imformation.

    Reply
  • You have not mention CHECKPOINT before run DBCC DROPCLEANBUFFERS.
    As per MSDN, use CHECKPOINT to produce a cold buffer cache. DBCC DROPCLEANBUFFERS command remove all buffers from the buffer pool.
    That means, Not DROPCLEANBUFFERS produce a cold buffer cache, it is CHECKPOINT. And we use DROPCLEANBUFFERS to remove pages from buffer pool. Cold buffer does not mean Buffer is clear.

    Reply
  • Hi Pinal Dave

    I have a Store Procedure running and this SP running with batch processing. with condition normal, the process around 10-15 minute. but If I try a several time, the process slow time to time, the process take a time until 30-45 minute.
    I have do the clear cache with this command.
    DBCC SHRINKDATABASE (ForestWork, TRUNCATEONLY);
    use ForestWork
    DBCC SHRINKFILE([ForestWork_log], 0, TRUNCATEONLY)
    DBCC DROPCLEANBUFFERS
    DBCC FREEPROCCACHE
    DBCC FREESESSIONCACHE
    DBCC FREESYSTEMCACHE (‘ALL’) WITH MARK_IN_USE_FOR_REMOVAL;

    After clear cache, the process still slow, but sometime the process back to normal about 10-15 minute. but it is only occasionally

    Do you know, what we should do? is any cache or log still need to clear again?

    Please let me know, if you have any idea.

    Best Regards,
    Heri

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.