首页 > 解决方案 > ALTER DATABASE SCOPED CONFIGURATION CLEAR PROCEDURE_CACHE 不删除缓存?

问题描述

我有一个 azure PaaS 数据库,想清除缓存以测试一些 SP。于是从网上找了一些脚本:

-- run this script against a user database, not master

-- count number of plans currently in cache

select count(*) from sys.dm_exec_cached_plans;

-- Executing this statement will clear the procedure cache in the current database, which means that all queries will have to recompile.

ALTER DATABASE SCOPED CONFIGURATION CLEAR PROCEDURE_CACHE;

-- count number of plans in cache now, after they were cleared from cache

select count(*) from sys.dm_exec_cached_plans;

-- list available plans

select * from sys.dm_exec_cached_plans;

select * from sys.dm_exec_procedure_stats 

但是,缓存的数量始终在 600-800 左右,所以它并没有下降。

我没有收到任何错误(没有权限被拒绝等),那么这个命令如何不清理缓存?

标签: azure-sql-database

解决方案


我还没有时间通过​​代码进行调试以确保 100% 确定,但根据我对系统的理解,很可能仅仅改变数据库模式版本(发生在任何 alter database 命令上)将使条目中的条目无效缓存下次使用。由于过程缓存是实例范围的,因此任何清除与数据库关联的条目的尝试都需要一个接一个地遍历所有条目,而不是仅仅释放整个缓存。

因此,您可以将其视为使整个缓存无效,但在重新编译或系统的其他部分通过以后的操作回收内存时懒惰地从缓存中删除条目。

Conor Cunningham 架构师,SQL


推荐阅读