Azure sql database ALTER数据库作用域配置清除过程\u缓存不删除缓存?

Azure sql database ALTER数据库作用域配置清除过程\u缓存不删除缓存?,azure-sql-database,Azure Sql Database,我有一个azure PaaS数据库,希望清除缓存以测试某些SP。因此,我从Internet上找到了一些脚本: -- 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 pro

我有一个azure PaaS数据库,希望清除缓存以测试某些SP。因此,我从Internet上找到了一些脚本:

-- 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左右,因此它不会下降


我没有收到任何错误(没有权限被拒绝等),因此该命令如何不清理缓存?

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

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

康纳坎宁安
架构师,SQL

我联系了Microsoft支持人员,现在已经理解了

尝试在AdventureWorks数据库上运行以下T-SQL

-- create test procedure
create or alter procedure pTest1
as begin 
  select * from salesLT.product where ProductCategoryID =27
end
go


-- exec the procedure once.
exec pTest1


-- check for cached plan for this specific procedure - cached plan exists
select * from sys.dm_exec_cached_plans p 
cross apply sys.dm_exec_sql_text(p.plan_handle) st
where p.objtype = 'proc' and st.objectid = OBJECT_ID('pTest1')


-- clear plan cache
ALTER DATABASE SCOPED CONFIGURATION CLEAR PROCEDURE_CACHE;

-- check for cached plan for this specific procedure - not exists anymore
select * from sys.dm_exec_cached_plans p 
cross apply sys.dm_exec_sql_text(p.plan_handle) st
where p.objtype = 'proc' and st.objectid = OBJECT_ID('pTest1')


-- cleanup
drop procedure pTest1
select 'cleanup complete' 
通过此示例,我们可以确认数据库的计划缓存已清除,但是sys.dm_exec_cached_plans是服务器范围的,并提供来自其他数据库(内部系统数据库)的结果,即对于这些数据库,未使用CLEAR PROCEDURE_cache命令清除缓存