Published on

May 8, 2021

Managing SQL Server Cache

SQL Server cache plays a crucial role in optimizing query performance. However, there are instances where caching can cause performance issues. In this article, we will explore different ways to manage the SQL Server cache effectively.

1. Flushing the Plan Cache

One common approach to managing the SQL Server cache is by flushing the plan cache. This can be done using the DBCC FREEPROCCACHE command. The command allows you to clear the entire plan cache, a specific plan, or a SQL Server resource pool.

To flush the entire plan cache for a SQL Server instance, you can use the following command:

DBCC FREEPROCCACHE

If you want to flush the plan cache for an entire instance but suppress the output messages, you can use the following command:

DBCC FREEPROCCACHE WITH NO_INFOMSGS;

You can also flush a specific resource pool by first checking how much memory is being used for each resource pool using the following query:

SELECT name AS 'Pool Name', 
       cache_memory_kb/1024.0 AS [cache_memory_MB], 
       used_memory_kb/1024.0 AS [used_memory_MB] 
FROM sys.dm_resource_governor_resource_pools;

Once you have identified the specific resource pool to flush, you can use the following command:

DBCC FREEPROCCACHE ('LimitedIOPool');

Additionally, you can flush a single query plan by obtaining the plan handle from the plan cache and using it in the following command:

DBCC FREEPROCCACHE (0x040011004A2CC30E204881F30200000001000000000000000000000000000000000000000000000000000000);

2. Flushing the Database Plan Cache

If you want to clear the plan cache for a specific database, you can use the DBCC FLUSHPROCINDB command. This command allows you to flush the plan cache for a particular database.

Here is an example of how to flush the database plan cache for a database named “MyDB”:

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

3. Clearing System Caches

The DBCC FREESYSTEMCACHE command can be used to release unused cache entries from all caches or from a specific Resource Governor pool. This command is useful for manually removing unused entries from the cache.

Here are some examples of using the DBCC FREESYSTEMCACHE command:

DBCC FREESYSTEMCACHE ('ALL') WITH MARK_IN_USE_FOR_REMOVAL
DBCC FREESYSTEMCACHE ('SQL Plans')
DBCC FREESYSTEMCACHE ('Temporary Tables & Table Variables')
DBCC FREESYSTEMCACHE ('userdatabase')
DBCC FREESYSTEMCACHE ('tempdb')

4. Other Cache Management Techniques

There are a few other techniques you can use to manage the SQL Server cache:

  • DBCC FREESESSIONCACHE: This command flushes the distributed query connection cache used by distributed queries against an instance of SQL Server.
  • DBCC FLUSHAUTHCACHE: This command flushes the database authentication cache, which maintains information regarding login and firewall rules for the current user database.
  • sp_recompile: By passing a specific object name, such as a procedure, trigger, table, view, or function, to the sp_recompile stored procedure, you can ensure that the object is recompiled the next time it is run.
  • ALTER DATABASE: In SQL Server 2016 and later versions, you can use the ALTER DATABASE statement to clear the plan cache for the current database.
  • DBCC DROPCLEANBUFFERS: This command allows you to test queries with a cold buffer cache without restarting the server.

By utilizing these techniques, you can effectively manage the SQL Server cache and optimize query 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.