Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Original KB number: 917828
Summary
In Microsoft SQL Server, query performance might suddenly decrease after you perform certain database maintenance operations, regular transaction operations, or server reconfigure operations. This problem occurs because these operations clear the entire plan cache for the SQL Server instance.
When the plan cache is cleared, all subsequent queries must recompile. The spike in recompilations temporarily increases CPU usage and reduces query throughput until the plan cache is repopulated. This article describes the operations that cause plan cache clearing, how to identify when the problem occurs, and which Performance Monitor counters and SQL Profiler events to monitor.
Symptoms
You notice a sudden decrease in query performance. The SQL Server error log contains messages that resemble the following example:
SQL Server has encountered <N> occurrence(s) of cachestore flush for the 'Object Plans' cachestore (part of plan cache) due to some database maintenance or reconfigure operations.
SQL Server has encountered <N> occurrence(s) of cachestore flush for the 'SQL Plans' cachestore (part of plan cache) due to some database maintenance or reconfigure operations.
SQL Server has encountered <N> occurrence(s) of cachestore flush for the 'Bound Trees' cachestore (part of plan cache) due to some database maintenance or reconfigure operations.
If you run DBCC FREEPROCCACHE or DBCC FREESYSTEMCACHE to flush the plan cache, you see messages that resemble the following example:
SQL Server has encountered <N> occurrence(s) of cachestore flush for the 'SQL Plans' cachestore (part of plan cache) due to 'DBCC FREEPROCCACHE' or 'DBCC FREESYSTEMCACHE' operations.
SQL Server has encountered <N> occurrence(s) of cachestore flush for the 'Bound Trees' cachestore (part of plan cache) due to 'DBCC FREEPROCCACHE' or 'DBCC FREESYSTEMCACHE' operations.
Cause
This behavior is by design. Certain database maintenance or reconfigure operations clear the entire procedure cache. All subsequent queries must then generate new execution plans. This action temporarily increases CPU usage and decreases query performance until the cache is repopulated.
Operations that clear the plan cache
The system clears the entire plan cache when you perform any of the following database-level operations:
Set the AUTO_CLOSE database option to
ON. If no user connection references or uses the database, the background task tries to close and shut down the database automatically.Run several queries against a database that has default options, and then drop the database.
Drop a database snapshot for a source database.
Rebuild the transaction log for a database.
Restore a database backup.
Detach a database by using sp_detach_db.
Change the database state to
OFFLINEorONLINE.Specify certain options in an ALTER DATABASE statement or change server options by using the sp_configure and
RECONFIGUREstatements, as listed in the following table:Command Option ALTER DATABASECOLLATEALTER DATABASEMODIFY FILEGROUP DEFAULTALTER DATABASEMODIFY FILEGROUP READ_ONLYALTER DATABASEMODIFY FILEGROUP READ_WRITEALTER DATABASEMODIFY_NAMEALTER DATABASEOFFLINEALTER DATABASEONLINEALTER DATABASEPAGE_VERIFYALTER DATABASEREAD_ONLYALTER DATABASEREAD_WRITEsp_configureaccess check cache bucket countsp_configureaccess check cache quotasp_configureclr enabledsp_configurecost threshold for parallelismsp_configurecross db ownership chainingsp_configureindex create memorysp_configuremax degree of parallelismsp_configuremax server memorysp_configuremax text repl sizesp_configuremax worker threadssp_configuremin memory per querysp_configuremin server memorysp_configurequery governor cost limitsp_configurequery waitsp_configureremote query timeoutsp_configureuser options
Note
The system doesn't clear the procedure cache if the actual value doesn't change or if you set the new value for max server memory to 0.
Solution
Verify that plan cache clearing is causing performance problems
Use the following methods to verify that a sudden query performance decrease is caused by plan cache clearing.
Check Performance Monitor counters
To identify the problem, monitor the following SQL Server performance counters:
Performance object:
Process, Counter:% Processor Time, Instance:sqlservrThis counter value increases because of increased CPU activity from recompilations after the plan cache is cleared.
Performance object:
SQLServer:Plan Cache, Counter:Cache Object Counts, Instance:_Totaland
Performance object:
SQLServer:Plan Cache, Counter:Cache Pages, Instance:_TotalThe values of these counters suddenly decrease when the plan cache is cleared.
Performance object:
SQLServer:SQL Statistics, Counter:SQL Compilations/secThis counter value significantly increases after the plan cache is cleared.
Note
For a named instance of SQL Server, the performance object is named MSSQL$<InstanceName>:Plan Cache and MSSQL$<InstanceName>:SQL Statistics.
Check Extended Events trace
If you capture an Extended Events (XEvent) trace, enable the following events to check for cache removal:
sqlserver.sp_cache_removesqlserver.query_cache_removal_statistics(query level)
Examine the removal_reason and remove_method columns to identify the cause.
Best practices to minimize performance impact
Check error logs for cache flush patterns. To identify which operations are triggering plan cache clearings, review the SQL Server error log for cachestore flush messages.
Minimize unnecessary database configuration changes. Avoid frequent cache flushes by reducing the number of
RECONFIGUREorALTER DATABASEoperations that clear the plan cache.Schedule planned operations during maintenance windows. To reduce the effect on query performance, schedule operations such as database restores or
ALTER DATABASEchanges during low-usage periods.Clear only specific plans instead of the entire cache. Instead of flushing all cached plans, remove only the problematic plan by specifying its plan handle:
DBCC FREEPROCCACHE (<PlanHandle>);For more information, see DBCC FREEPROCCACHE (Transact-SQL).
Use database-scoped cache clearing (SQL Server 2016 and later). Instead of clearing the plan cache for the entire instance, use ALTER DATABASE SCOPED CONFIGURATION CLEAR PROCEDURE_CACHE to clear plans for a single database.