Find sessions holding large query memory grants in SQL Server
Queries reserve memory before they run. When the reservations are too big, other queries wait. This shows who holds what.
The task
Before a query with sorts or hash operations starts, it asks the resource semaphore for a workspace memory grant sized from the optimizer's row estimates. When the estimates are wrong, so is the grant: an over-granted query reserves memory it never touches, and an under-granted one spills its sort or hash to tempdb. sys.dm_exec_query_memory_grants shows every outstanding grant in real time.
The snippet
Written for SQL Server.
SELECT mg.session_id,
CASE WHEN mg.grant_time IS NULL THEN 'waiting' ELSE 'granted' END AS grant_state,
mg.request_time,
mg.requested_memory_kb / 1024 AS requested_mb,
mg.granted_memory_kb / 1024 AS granted_mb,
mg.used_memory_kb / 1024 AS used_mb,
mg.max_used_memory_kb / 1024 AS peak_mb,
mg.ideal_memory_kb / 1024 AS ideal_mb,
mg.queue_id, mg.wait_time_ms,
LEFT(t.text, 120) AS query
FROM sys.dm_exec_query_memory_grants mg
OUTER APPLY sys.dm_exec_sql_text(mg.sql_handle) t
ORDER BY mg.requested_memory_kb DESC;
它的運作方式
Each row is a request holding or waiting for a memory grant. grant_time is NULL while the request waits; then queue_id names the semaphore queue it sits in and wait_time_ms says for how long (both are NULL once granted). The sort is on requested_mb because waiting requests have no granted memory yet. Granted versus peak is the over-grant check: a query granted 2 GB that peaked at 200 MB was estimated badly, and the unused 1.8 GB can still keep other queries waiting. ideal_mb is what the optimizer wanted before the grant was capped. In SQL Server 2019 and later, memory grant feedback adjusts the grant of repeated cached plans automatically; updated statistics or a rewrite fix the cause.
Variations
SQL Server: large grants by cached plan (historical)
SQL ServerSELECT TOP 20
qs.execution_count,
qs.total_grant_kb / 1024 AS total_grant_mb,
qs.total_used_grant_kb / 1024 AS used_grant_mb,
LEFT(st.text, 120) AS query
FROM sys.dm_exec_query_stats qs
CROSS APPLY sys.dm_exec_sql_text(qs.sql_handle) st
WHERE qs.total_grant_kb > 0
ORDER BY qs.total_grant_kb DESC;
Questions that come up
Why would a query be granted 100x what it uses?
Row estimates. Stale statistics, a plan compiled for an unusual parameter value, or a predicate the optimizer cannot estimate well (a local variable, a multi-statement function) makes it expect millions of rows where thousands come back, and the grant scales with the expectation. Wide variable-length columns add to it, because the estimate assumes each is half full. UPDATE STATISTICS, OPTION (RECOMPILE) for parameter-sensitive queries, or a rewrite are the usual fixes; the MAX_GRANT_PERCENT query hint caps one query's grant.
What is the resource semaphore and how do I see contention?
It is the gatekeeper for query workspace memory. When the memory requested by new queries exceeds what is free, they wait, and the waits show up as RESOURCE_SEMAPHORE in the wait statistics and as rows with a NULL grant_time here. sys.dm_exec_query_resource_semaphores shows each semaphore's target, available and waiter counts. A few huge grants with many waiters behind them is the usual picture, and the fix is shrinking the over-grants.
Keep reading
Run these with autocomplete that knows your tables
SQLly is a native SQL workbench for macOS, Windows and Linux. Paste a snippet into a live connection and you get schema-aware IntelliSense, a plan viewer and guards against unsafe edits.