SQL Server not releasing memory after query executes

后端 未结 5 1552
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-08 07:14

I think I have a basic question here that many might have encountered. When I run a query in SQL Server it will load in memory all the data it needs for query execution (for

5条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-08 07:48

    Old question, but figured i'd add my two cents. Mostly a riff on what was in the above answer using dsql to automatically roll back to the previous value after shrinking the memory amount. It's ugly, but it works.

    IF OBJECT_ID(N'tempdb..##globaltemp') IS NOT NULL
    BEGIN
        DROP TABLE ##globaltemp
    END
    
    SELECT TOP 1 value
    INTO ##globaltemp
    FROM sys.configurations
    WHERE NAME LIKE '%server memory%'
    ORDER BY NAME
    OPTION (RECOMPILE);
    
    EXEC sys.sp_configure N'max server memory (MB)'
        , N'1024'
    GO
    
    RECONFIGURE
    WITH OVERRIDE
    GO
    
    DECLARE @dsql AS VARCHAR(20)
    
    SELECT @dsql = cast(value AS NVARCHAR(20))
    FROM ##globaltemp
    
    EXEC sys.sp_configure N'max server memory (MB)'
        , @dsql
    GO
    
    RECONFIGURE
    WITH OVERRIDE
    GO
    

提交回复
热议问题