Tuning SQL Server Query Memory Grants

By Tom Nonmacher

Managing memory usage in SQL Server is critical to maximizing the performance of your database. One of the most important aspects of memory usage is the allocation of memory grants to SQL Server queries. If not properly configured, memory grants can cause server performance to suffer. This blog will guide you through the process of tuning SQL Server query memory grants using SQL Server 2016 and 2017, MySQL 5.7, DB2 11.1, and Azure SQL.

In SQL Server 2016 and 2017, the Dynamic Memory Grant feature allows SQL Server to adjust the amount of memory assigned to a query based on the actual memory requirements of the query. In the past, SQL Server would calculate the memory grant size based on an estimate of the number of rows returned by the query. This could lead to under or over-estimation of the memory requirements. With Dynamic Memory Grants, SQL Server will adjust memory grants in real-time, ensuring that each query has the right amount of memory.


-- SQL Server 2016/2017 code to enable Dynamic Memory Grants
ALTER DATABASE SCOPED CONFIGURATION SET MEMORY_OPTIMIZED_ELEVATE_TO_SNAPSHOT = ON;

In MySQL 5.7, the Performance Schema provides information about memory usage by SQL queries. The events_statements_summary_by_digest table holds information about SQL statement performance, including the memory used by each query. By analyzing this data, you can identify queries that are using a large amount of memory and tune them accordingly.


-- MySQL 5.7 code to query memory usage by SQL statements
SELECT digest, COUNT_STAR, SUM_MEMORY_USED
FROM performance_schema.events_statements_summary_by_digest
ORDER BY SUM_MEMORY_USED DESC;

In DB2 11.1, the STMM (Self-Tuning Memory Manager) dynamically adjusts the memory allocation for database workloads. By default, STMM is enabled and manages the database memory allocations. However, you can tune the memory allocation manually by turning off STMM and adjusting the configuration parameters.


-- DB2 11.1 code to disable STMM and manually set the memory allocation
UPDATE DATABASE CONFIGURATION FOR sample USING SELF_TUNING_MEM OFF;
UPDATE DATABASE CONFIGURATION FOR sample USING DATABASE_MEMORY 10000;

For Azure SQL, the Resource Governor provides control over the allocation of CPU, I/O, and memory resources. You can define resource pools with specific memory allocations, and then assign these pools to workloads. This allows you to ensure that critical workloads have the resources they need to perform optimally.


-- Azure SQL code to create a resource pool with specific memory allocation
CREATE RESOURCE POOL HighPriorityPool
WITH ( MAX_MEMORY_PERCENT = 70 );
CREATE WORKLOAD GROUP HighPriorityWorkload
USING HighPriorityPool;

By effectively managing and tuning your SQL Server query memory grants, you can greatly improve the performance and stability of your database. While the specific techniques and tools vary between SQL Server, MySQL, DB2, and Azure SQL, the principles remain the same: monitor your memory usage, adjust your memory grants as needed, and ensure that your critical workloads have the resources they need.




88A4A2
Please enter the code from the image above in the box below.