Introduction
When optimizing SQL Server performance, it’s crucial to understand how memory grants are allocated to query plan operators. Excessive memory grants can lead to inefficient resource utilization and impact overall system performance. In this article, we’ll explore practical T-SQL code examples and techniques to determine which operators are contributing the most to memory grants in SQL Server query plans.
Using sys.dm_exec_query_memory_grants DMV
One effective approach is to analyze the query plan using the sys.dm_exec_query_memory_grants DMV (Dynamic Management View). This DMV provides valuable insights into memory grant information for currently executing queries. By joining this DMV with other relevant DMVs, such as sys.dm_exec_requests and sys.dm_exec_query_stats, you can obtain detailed information about memory grants at the operator level.
Here’s an example T-SQL query that retrieves the top memory grant contributors for a specific query:
SELECT TOP 10
qp.query_plan,
mg.dop,
mg.request_time,
mg.grant_time,
mg.requested_memory_kb,
mg.granted_memory_kb,
mg.required_memory_kb,
mg.used_memory_kb,
mg.max_used_memory_kb
FROM sys.dm_exec_query_memory_grants mg
CROSS APPLY sys.dm_exec_query_plan(mg.plan_handle) qp
WHERE mg.session_id = <session_id>
ORDER BY mg.requested_memory_kb DESC;
In this query, replace <session_id> with the actual session ID of the query you want to analyze. The query retrieves the top 10 operators with the highest requested memory grants, along with relevant memory grant metrics such as requested, granted, required, and used memory.

Analyzing Query Plan XML
To further investigate the memory grant distribution across operators, you can use the sys.dm_exec_query_plan DMF (Dynamic Management Function) to extract the query plan XML. By parsing the XML, you can identify the specific operators and their memory grant properties. Here’s an example of how to retrieve the query plan XML:
SELECT qp.query_plan
FROM sys.dm_exec_query_stats qs
CROSS APPLY sys.dm_exec_query_plan(qs.plan_handle) qp
WHERE qs.query_hash = <query_hash>;
Replace <query_hash> with the query hash of the query you want to examine. Once you have the query plan XML, you can use XQuery or XML parsing techniques to extract the memory grant information for each operator.
Obtaining Query Hash
To obtain the query hash for a specific query, you can use the sys.dm_exec_requests DMV or the sys.dm_exec_query_stats DMV, depending on whether the query is currently executing or has completed execution.
- For currently executing queries:
SELECT r.query_hash
FROM sys.dm_exec_requests r
WHERE r.session_id = <session_id>;
Replace <session_id> with the session ID of the query you want to examine.
- For completed queries:
SELECT qs.query_hash
FROM sys.dm_exec_query_stats qs
CROSS APPLY sys.dm_exec_sql_text(qs.sql_handle) st
WHERE st.text LIKE '%<query_text>%';
Replace <query_text> with a unique portion of the query text you want to find the query hash for.
Alternatively, you can use the sys.dm_exec_cached_plans DMV to retrieve the query hash based on the plan handle:
SELECT cp.query_hash
FROM sys.dm_exec_cached_plans cp
WHERE cp.plan_handle = <plan_handle>;
Replace <plan_handle> with the actual plan handle of the query.
Once you have the query hash, you can use it in the query plan analysis query mentioned earlier to retrieve the query plan XML.
Conclusion
By combining the information from these DMVs and query plan analysis, you can gain valuable insights into which operators are consuming the most memory grants. Armed with this knowledge, you can focus your optimization efforts on the most impactful areas, such as improving indexing, rewriting queries, or adjusting database settings to achieve better memory grant efficiency.
Remember, optimizing memory grants is an iterative process that requires careful analysis and testing. By leveraging the power of T-SQL and query plan analysis, you can identify and address memory grant issues, ultimately leading to improved SQL Server performance and resource utilization.
Visit the official Microsoft documentation to learn more about Memory Grants in SQL Server.