Published on

December 29, 2018

Clearing Cache in SQL Server

When it comes to optimizing the performance of your SQL Server database, there are various techniques and strategies you can implement. One such technique is clearing the cache of the server. However, it is important to understand the implications of clearing the cache and use the appropriate commands to avoid any negative impact on the overall performance.

In a recent Comprehensive Database Performance Health Check, we made several improvements to our database. After implementing these changes, we wanted to see how our improvements fared by clearing the cache. However, it is crucial to note that clearing the cache for all databases on the server is not recommended during business hours, as it can put unnecessary pressure on the SQL Server to recreate all the plans, potentially impacting performance.

During the discussion about clearing the cache, a senior DBA at our organization quickly jumped to the Query Editor in SQL Server Management Studio (SSMS) and wrote the following command:

DBCC FREEPROCCACHE

Upon seeing this, I immediately intervened and explained that running the above command would drop the plan cache for all databases on the server, which is not what we intended. Instead, we only needed to clear the cache for a single database.

To clear the cache for a single database, you can use the following command:

USE DatabaseToClearCache;
DECLARE @dbid INT = DB_ID();
DBCC FLUSHPROCINDB (@dbId);

If you are using SQL Server 2016 or a later version, you can also run the following command:

USE DatabaseToClearCache;
ALTER DATABASE SCOPED CONFIGURATION CLEAR PROCEDURE_CACHE

It is important to note that clearing the cache for a single database should only be done in extreme conditions. In most cases, it is not necessary and can potentially disrupt the normal functioning of the SQL Server.

By understanding the appropriate commands and their implications, you can effectively manage the cache in your SQL Server database and optimize its performance.

Click to rate this post!
[Total: 0 Average: 0]

Let's work together

Send us a message or book free introductory meeting with us using button below.