Skip to content

Commit c067e9c

Browse files
authored
Merge pull request #20622 from WilliamDAssafMSFT/20211105-update-query-memory-grants-examples
20211105 1133 improve examples, linkage
2 parents fe2f197 + 66227dd commit c067e9c

2 files changed

Lines changed: 86 additions & 64 deletions

File tree

Lines changed: 58 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
---
22
description: "sys.dm_exec_query_memory_grants (Transact-SQL)"
3-
title: "sys.dm_exec_query_memory_grants (Transact-SQL) | Microsoft Docs"
3+
title: "sys.dm_exec_query_memory_grants (Transact-SQL)"
44
ms.custom: ""
5-
ms.date: "05/19/2020"
5+
ms.date: "11/05/2021"
66
ms.prod: sql
77
ms.prod_service: "database-engine, sql-database"
88
ms.reviewer: ""
@@ -17,22 +17,21 @@ dev_langs:
1717
- "TSQL"
1818
helpviewer_keywords:
1919
- "sys.dm_exec_query_memory_grants dynamic management view"
20-
ms.assetid: 2c417747-2edd-4e0d-8a9c-e5f445985c1a
2120
author: WilliamDAssafMSFT
2221
ms.author: wiassaf
2322
monikerRange: "=azuresqldb-current||>=sql-server-2016||>=sql-server-linux-2017||=azuresqldb-mi-current|| >= aps-pdw-2016 || = azure-sqldw-latest"
2423
---
2524
# sys.dm_exec_query_memory_grants (Transact-SQL)
2625
[!INCLUDE [sql-asdb-asa-pdw](../../includes/applies-to-version/sql-asdb-asa-pdw.md)]
2726

28-
Returns information about all queries that have requested and are waiting for a memory grant or have been given a memory grant. Queries that do not require a memory grant will not appear in this view. For example, sort and hash join operations have memory grants for query execution, while queries without an **ORDER BY** clause will not have a memory grant.
27+
Returns information about all queries that have requested and are waiting for a memory grant or have been given a memory grant. Queries that do not require a memory grant will not appear in this view. For example, sort and hash join operations have memory grants for query execution, while queries without an `ORDER BY` clause will not have a memory grant.
2928

30-
In [!INCLUDE[ssSDSfull](../../includes/sssdsfull-md.md)], dynamic management views cannot expose information that would impact database containment or expose information about other databases the user has access to. To avoid exposing this information, every row that contains data that doesn't belong to the connected tenant is filtered out. In addition, the values in the columns **scheduler_id**, **wait_order**, **pool_id**, **group_id** are filtered; the column value is set to NULL.
29+
In [!INCLUDE[ssSDSfull](../../includes/sssdsfull-md.md)], dynamic management views cannot expose information that would impact database containment or expose information about other databases the user has access to. To avoid exposing this information, every row that contains data that doesn't belong to the connected tenant is filtered out. In addition, the values in the columns `scheduler_id`, `wait_order`, `pool_id`, `group_id` are filtered; the column value is set to NULL.
3130

3231
> [!NOTE]
3332
> To call this from [!INCLUDE[ssSDWfull](../../includes/sssdwfull-md.md)] or [!INCLUDE[ssPDW](../../includes/sspdw-md.md)], use the name `sys.dm_pdw_nodes_exec_query_memory_grants`. [!INCLUDE[synapse-analytics-od-unsupported-syntax](../../includes/synapse-analytics-od-unsupported-syntax.md)]
3433
35-
|Column name|Data type|Description|
34+
|**Column name**|**Data type**|**Description**|
3635
|-----------------|---------------|-----------------|
3736
|**session_id**|**smallint**|ID (SPID) of the session where this query is running.|
3837
|**request_id**|**int**|ID of the request. Unique in the context of the session.|
@@ -41,19 +40,19 @@ monikerRange: "=azuresqldb-current||>=sql-server-2016||>=sql-server-linux-2017||
4140
|**request_time**|**datetime**|Date and time when this query requested the memory grant.|
4241
|**grant_time**|**datetime**|Date and time when memory was granted for this query. NULL if memory is not granted yet.|
4342
|**requested_memory_kb**|**bigint**|Total requested amount of memory in kilobytes.|
44-
|**granted_memory_kb**|**bigint**|Total amount of memory actually granted in kilobytes. Can be NULL if the memory is not granted yet. For a typical situation, this value should be the same as **requested_memory_kb**. For index creation, the server may allow additional on-demand memory beyond initially granted memory.|
45-
|**required_memory_kb**|**bigint**|Minimum memory required to run this query in kilobytes. **requested_memory_kb** is the same or larger than this amount.|
43+
|**granted_memory_kb**|**bigint**|Total amount of memory actually granted in kilobytes. Can be NULL if the memory is not granted yet. For a typical situation, this value should be the same as `requested_memory_kb`. For index creation, the server may allow additional on-demand memory beyond initially granted memory.|
44+
|**required_memory_kb**|**bigint**|Minimum memory required to run this query in kilobytes. `requested_memory_kb` is the same or larger than this amount.|
4645
|**used_memory_kb**|**bigint**|Physical memory used at this moment in kilobytes.|
4746
|**max_used_memory_kb**|**bigint**|Maximum physical memory used up to this moment in kilobytes.|
4847
|**query_cost**|**float**|Estimated query cost.|
4948
|**timeout_sec**|**int**|Time-out in seconds before this query gives up the memory grant request.|
50-
|**resource_semaphore_id**|**smallint**|Non-unique ID of the resource semaphore on which this query is waiting.<br /><br /> **Note:** This ID is unique in versions of [!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)] that are earlier than [!INCLUDE[ssKatmai](../../includes/sskatmai-md.md)]. This change can affect troubleshooting query execution. For more information, see the "Remarks" section later in this topic.|
49+
|**resource_semaphore_id**|**smallint**|Non-unique ID of the resource semaphore on which this query is waiting.<br /><br /> **Note:** This ID is unique in versions of [!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)] that are earlier than [!INCLUDE[ssKatmai](../../includes/sskatmai-md.md)]. This change can affect troubleshooting query execution. For more information, see the "Remarks" section later in this article.|
5150
|**queue_id**|**smallint**|ID of waiting queue where this query waits for memory grants. NULL if the memory is already granted.|
52-
|**wait_order**|**int**|Sequential order of waiting queries within the specified **queue_id**. This value can change for a given query if other queries get memory grants or time out. NULL if memory is already granted.|
51+
|**wait_order**|**int**|Sequential order of waiting queries within the specified `queue_id`. This value can change for a given query if other queries get memory grants or time out. NULL if memory is already granted.|
5352
|**is_next_candidate**|**bit**|Candidate for next memory grant.<br /><br /> 1 = Yes<br /><br /> 0 = No<br /><br /> NULL = Memory is already granted.|
5453
|**wait_time_ms**|**bigint**|Wait time in milliseconds. NULL if the memory is already granted.|
55-
|**plan_handle**|**varbinary(64)**|Identifier for this query plan. Use **sys.dm_exec_query_plan** to extract the actual XML plan.|
56-
|**sql_handle**|**varbinary(64)**|Identifier for [!INCLUDE[tsql](../../includes/tsql-md.md)] text for this query. Use **sys.dm_exec_sql_text** to get the actual [!INCLUDE[tsql](../../includes/tsql-md.md)] text.|
54+
|**plan_handle**|**varbinary(64)**|Identifier for this query plan. Use `sys.dm_exec_query_plan` to extract the actual XML plan.|
55+
|**sql_handle**|**varbinary(64)**|Identifier for [!INCLUDE[tsql](../../includes/tsql-md.md)] text for this query. Use `sys.dm_exec_sql_text` to get the actual [!INCLUDE[tsql](../../includes/tsql-md.md)] text.|
5756
|**group_id**|**int**|ID for the workload group where this query is running.|
5857
|**pool_id**|**int**|ID of the resource pool that this workload group belongs to.|
5958
|**is_small**|**tinyint**|When set to 1, indicates that this grant uses the small resource semaphore. When set to 0, indicates that a regular semaphore is used.|
@@ -70,17 +69,24 @@ On [!INCLUDE[ssNoVersion_md](../../includes/ssnoversion-md.md)], requires `VIEW
7069
On [!INCLUDE[ssSDSfull](../../includes/sssdsfull-md.md)], requires the `VIEW DATABASE STATE` permission in the database.
7170

7271
## Remarks
73-
A typical debugging scenario for query time-out may look like the following:
72+
73+
Queries that use dynamic management views that include `ORDER BY` or aggregates may increase memory consumption and thus contribute to the problem they are troubleshooting.
74+
75+
The Resource Governor feature enables a database administrator to distribute server resources among resource pools, up to a maximum of 64 pools. Beginning with [!INCLUDE[ssKatmai](../../includes/sskatmai-md.md)], each pool behaves like a small independent server instance and requires two semaphores. The number of rows that are returned from `sys.dm_exec_query_resource_semaphores` can be up to 20 times more than the rows that are returned in [!INCLUDE[ssVersion2005](../../includes/ssversion2005-md.md)].
76+
77+
## Examples
78+
79+
A typical debugging scenario for query time-out may investigate the following:
7480

7581
- Check overall system memory status using [sys.dm_os_memory_clerks](../../relational-databases/system-dynamic-management-views/sys-dm-os-memory-clerks-transact-sql.md), [sys.dm_os_sys_info](../../relational-databases/system-dynamic-management-views/sys-dm-os-sys-info-transact-sql.md), and various performance counters.
7682

77-
- Check for query-execution memory reservations in **sys.dm_os_memory_clerks** where `type = 'MEMORYCLERK_SQLQERESERVATIONS'`.
83+
- Check for query-execution memory reservations in `sys.dm_os_memory_clerks` where `type = 'MEMORYCLERK_SQLQERESERVATIONS'`.
7884

79-
- Check for queries waiting<sup>1</sup> for grants using **sys.dm_exec_query_memory_grants**.
85+
- Check for queries waiting<sup>1</sup> for grants using `sys.dm_exec_query_memory_grants`:
8086

8187
```sql
8288
--Find all queries waiting in the memory queue
83-
SELECT * FROM sys.dm_exec_query_memory_grants where grant_time is null
89+
SELECT * FROM sys.dm_exec_query_memory_grants WHERE grant_time IS NULL;
8490
```
8591

8692
<sup>1</sup> In this scenario, the wait type is typically RESOURCE_SEMAPHORE. For more information, see [sys.dm_os_wait_stats &#40;Transact-SQL&#41;](../../relational-databases/system-dynamic-management-views/sys-dm-os-wait-stats-transact-sql.md).
@@ -95,27 +101,39 @@ On [!INCLUDE[ssSDSfull](../../includes/sssdsfull-md.md)], requires the `VIEW DAT
95101
GO
96102
```
97103

98-
- Further examine memory-intensive queries using [sys.dm_exec_requests](../../relational-databases/system-dynamic-management-views/sys-dm-exec-requests-transact-sql.md).
99-
100-
```sql
101-
--Find top 5 queries by average CPU time
102-
SELECT TOP 5 total_worker_time/execution_count AS [Avg CPU Time],
103-
plan_handle, query_plan
104-
FROM sys.dm_exec_query_stats AS qs
105-
CROSS APPLY sys.dm_exec_query_plan(qs.plan_handle)
106-
ORDER BY total_worker_time/execution_count DESC;
107-
GO
108-
```
109-
110-
- If a runaway query is suspected, examine the Showplan from [sys.dm_exec_query_plan](../../relational-databases/system-dynamic-management-views/sys-dm-exec-query-plan-transact-sql.md) and batch text from [sys.dm_exec_sql_text](../../relational-databases/system-dynamic-management-views/sys-dm-exec-sql-text-transact-sql.md).
111-
112-
Queries that use dynamic management views that include `ORDER BY` or aggregates may increase memory consumption and thus contribute to the problem they are troubleshooting.
113-
114-
The Resource Governor feature enables a database administrator to distribute server resources among resource pools, up to a maximum of 64 pools. Beginning with [!INCLUDE[ssKatmai](../../includes/sskatmai-md.md)], each pool behaves like a small independent server instance and requires 2 semaphores. The number of rows that are returned from **sys.dm_exec_query_resource_semaphores** can be up to 20 times more than the rows that are returned in [!INCLUDE[ssVersion2005](../../includes/ssversion2005-md.md)].
115-
116-
## See Also
117-
[sys.dm_exec_query_resource_semaphores &#40;Transact-SQL&#41;](../../relational-databases/system-dynamic-management-views/sys-dm-exec-query-resource-semaphores-transact-sql.md)
118-
[sys.dm_os_wait_stats &#40;Transact-SQL&#41;](../../relational-databases/system-dynamic-management-views/sys-dm-os-wait-stats-transact-sql.md)
119-
[Execution Related Dynamic Management Views and Functions &#40;Transact-SQL&#41;](../../relational-databases/system-dynamic-management-views/execution-related-dynamic-management-views-and-functions-transact-sql.md)
120-
[Thread and Task Architecture Guide](../../relational-databases/thread-and-task-architecture-guide.md)
121-
104+
- If a runaway query is suspected, examine the Showplan in the `query_plan` column from [sys.dm_exec_query_plan](../../relational-databases/system-dynamic-management-views/sys-dm-exec-query-plan-transact-sql.md) and query batch `text` from [sys.dm_exec_sql_text](../../relational-databases/system-dynamic-management-views/sys-dm-exec-sql-text-transact-sql.md). Further examine memory-intensive queries currently executing, using [sys.dm_exec_requests](../../relational-databases/system-dynamic-management-views/sys-dm-exec-requests-transact-sql.md).
105+
106+
```sql
107+
--Active requests with memory grants
108+
SELECT
109+
--Session data
110+
s.[session_id], s.open_transaction_count
111+
--Memory usage
112+
, r.granted_query_memory, mg.grant_time, mg.requested_memory_kb, mg.granted_memory_kb, mg.required_memory_kb, mg.used_memory_kb, mg.max_used_memory_kb
113+
--Query
114+
, query_text = t.text, input_buffer = ib.event_info, query_plan_xml = qp.query_plan, request_row_count = r.row_count, session_row_count = s.row_count
115+
--Session history and status
116+
, s.last_request_start_time, s.last_request_end_time, s.reads, s.writes, s.logical_reads, session_status = s.[status], request_status = r.status
117+
--Session connection information
118+
, s.host_name, s.program_name, s.login_name, s.client_interface_name, s.is_user_process
119+
FROM sys.dm_exec_sessions s
120+
LEFT OUTER JOIN sys.dm_exec_requests AS r
121+
ON r.[session_id] = s.[session_id]
122+
LEFT OUTER JOIN sys.dm_exec_query_memory_grants AS mg
123+
ON mg.[session_id] = s.[session_id]
124+
OUTER APPLY sys.dm_exec_sql_text (r.[sql_handle]) AS t
125+
OUTER APPLY sys.dm_exec_input_buffer(s.[session_id], NULL) AS ib
126+
OUTER APPLY sys.dm_exec_query_plan (r.[plan_handle]) AS qp
127+
WHERE mg.granted_memory_kb > 0
128+
ORDER BY mg.granted_memory_kb desc, mg.requested_memory_kb desc;
129+
GO
130+
```
131+
132+
## See also
133+
134+
- [sys.dm_exec_query_resource_semaphores &#40;Transact-SQL&#41;](../../relational-databases/system-dynamic-management-views/sys-dm-exec-query-resource-semaphores-transact-sql.md)
135+
- [sys.dm_os_wait_stats &#40;Transact-SQL&#41;](../../relational-databases/system-dynamic-management-views/sys-dm-os-wait-stats-transact-sql.md)
136+
- [Execution Related Dynamic Management Views and Functions &#40;Transact-SQL&#41;](../../relational-databases/system-dynamic-management-views/execution-related-dynamic-management-views-and-functions-transact-sql.md)
137+
- [Thread and Task Architecture Guide](../../relational-databases/thread-and-task-architecture-guide.md)
138+
- [MSSQLSERVER_701](../errors-events/mssqlserver-701-database-engine-error.md)
139+
- [Troubleshoot out of memory errors with Azure SQL Database](/azure/azure-sql/database/troubleshoot-memory-errors-issues)

0 commit comments

Comments
 (0)