对于内存有限的电脑来说,MSSQL运行一段时间后,会缓存很多数据库数据,使用本存储过程,可以设置MSSQL的使用内存变小,再恢复,从降低他的内存使用量。
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sp_ClearMemory]') AND type in (N'P', N'PC'))
Drop procedure sp_ClearMemory
GO
CREATE procedure [dbo].sp_ClearMemory @MaxMemSize INTEGER
as
begin
-- 清除所有缓存
DBCC DROPCLEANBUFFERS
-- 打开高级配置
exec sp_configure 'show advanced options', 1
-- 设置最大内存值,清除现有缓存空间
exec sp_configure 'max server memory', 256
EXEC ('RECONFIGURE WITH OVERRIDE')
-- 设置等待时间
WAITFOR DELAY '00:00:01'
-- 重新设置最大内存值
EXEC sp_configure 'max server memory', @MaxMemSize
EXEC ('RECONFIGURE WITH OVERRIDE')
-- 关闭高级配置
exec sp_configure 'show advanced options',0
end
GO
GRANT EXECUTE ON sp_ClearMemory TO PUBLIC
GO