You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Use the **sp_statement_completed** extended event to trace execution of a query. Create an extended event session with this event, optionally with a filter on object_id for a particular natively compiled stored procedure. The extended event is raised after the execution of each query. The CPU time and duration reported by the extended event indicate how much CPU the query used and the execution time. A natively compiled stored procedure that uses a lot of CPU time may have performance problems.
23
23
24
-
**line_number**, along with the **object_id** in the extended event can be used to investigate the query. The following query can be used to retrieve the procedure definition. The line number can be used to identify the query within the definition:
24
+
The**line_number**, along with the **object_id** in the extended event can be used to investigate the query. The following query can be used to retrieve the procedure definition. The line number can be used to identify the query within the definition:
25
25
26
26
```sql
27
27
SELECT [definition]
28
-
fromsys.sql_modules
29
-
where object_id=object_id;
28
+
FROMsys.sql_modules
29
+
WHERE object_id=object_id;
30
30
```
31
-
32
-
31
+
33
32
## Using Data Management Views and Query Store
34
-
[!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)] and [!INCLUDE[ssSDS](../../includes/sssds-md.md)] support collecting execution statistics for natively compiled stored procedures, both on the procedure level and the query level. Collecting execution statistics is not enabled by default due to performance impact.
33
+
[!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)] and [!INCLUDE[ssSDSfull](../../includes/sssdsfull-md.md)] support collecting execution statistics for natively compiled stored procedures, both on the procedure level and the query level. Collecting execution statistics is not enabled by default due to performance impact.
35
34
36
35
Execution statistics are reflected in the system views [sys.dm_exec_procedure_stats](../../relational-databases/system-dynamic-management-views/sys-dm-exec-procedure-stats-transact-sql.md) and [sys.dm_exec_query_stats](../../relational-databases/system-dynamic-management-views/sys-dm-exec-query-stats-transact-sql.md), as well as in [Query Store](../../relational-databases/performance/monitoring-performance-by-using-the-query-store.md).
37
36
38
37
## Procedure-Level Execution Statistics
39
38
40
39
**[!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)]**: Enable or disable statistics collection on natively compiled stored procedures at the procedure-level using [sys.sp_xtp_control_proc_exec_stats (Transact-SQL)](../../relational-databases/system-stored-procedures/sys-sp-xtp-control-proc-exec-stats-transact-sql.md). The following statement enables collection of procedure-level execution statistics for all natively compiled T-SQL modules on the current instance:
40
+
41
41
```sql
42
42
EXEC sys.sp_xtp_control_proc_exec_stats1
43
43
```
44
44
45
-
**[!INCLUDE[ssSDSFull](../../includes/sssdsfull-md.md)]**: Enable or disable statistics collection on natively compiled stored procedures at the procedure level using the [database-scoped configuration](../../t-sql/statements/alter-database-scoped-configuration-transact-sql.md) option `XTP_PROCEDURE_EXECUTION_STATISTICS`. The following statement enables collection of procedure-level execution statistics for all natively compiled T-SQL modules in the current database:
45
+
**[!INCLUDE[ssSDSFull](../../includes/sssdsfull-md.md)]** and **[!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)]**: Enable or disable statistics collection on natively compiled stored procedures at the procedure level using the [database-scoped configuration](../../t-sql/statements/alter-database-scoped-configuration-transact-sql.md) option `XTP_PROCEDURE_EXECUTION_STATISTICS`. The following statement enables collection of procedure-level execution statistics for all natively compiled T-SQL modules in the current database:
46
+
46
47
```sql
47
-
ALTERDATABASE
48
-
SCOPED CONFIGURATION
49
-
SET XTP_PROCEDURE_EXECUTION_STATISTICS =ON;
48
+
ALTERDATABASE SCOPED CONFIGURATION SET XTP_PROCEDURE_EXECUTION_STATISTICS =ON;
50
49
```
51
50
52
51
## Query-Level Execution Statistics
53
52
54
53
**[!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)]**: Enable or disable statistics collection on natively compiled stored procedures at the query-level using [sys.sp_xtp_control_query_exec_stats (Transact-SQL)](../../relational-databases/system-stored-procedures/sys-sp-xtp-control-query-exec-stats-transact-sql.md). The following statement enables collection of query-level execution statistics for all natively compiled T-SQL modules on the current instance:
54
+
55
55
```sql
56
56
EXEC sys.sp_xtp_control_query_exec_stats1
57
57
```
58
58
59
-
**[!INCLUDE[ssSDSFull](../../includes/sssdsfull-md.md)]**: Enable or disable statistics collection on natively compiled stored procedures at the statement level using the [database-scoped configuration](../../t-sql/statements/alter-database-scoped-configuration-transact-sql.md) option `XTP_QUERY_EXECUTION_STATISTICS`. The following statement enables collection of query-level execution statistics for all natively compiled T-SQL modules in the current database:
59
+
**[!INCLUDE[ssSDSFull](../../includes/sssdsfull-md.md)]** and **[!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)]**: Enable or disable statistics collection on natively compiled stored procedures at the statement level using the [database-scoped configuration](../../t-sql/statements/alter-database-scoped-configuration-transact-sql.md) option `XTP_QUERY_EXECUTION_STATISTICS`. The following statement enables collection of query-level execution statistics for all natively compiled T-SQL modules in the current database:
60
+
60
61
```sql
61
-
ALTERDATABASE
62
-
SCOPED CONFIGURATION
63
-
SET XTP_QUERY_EXECUTION_STATISTICS =ON;
62
+
ALTERDATABASE SCOPED CONFIGURATION SET XTP_QUERY_EXECUTION_STATISTICS =ON;
64
63
```
65
64
66
65
## Sample Queries
@@ -71,60 +70,37 @@ ALTER DATABASE
71
70
The following query returns the procedure names and execution statistics for natively compiled stored procedures in the current database, after statistics collection:
AND object_id IN (SELECT object_id FROMsys.sql_modulesWHERE uses_native_compilation =1)
82
+
ORDER BY total_worker_time desc;
96
83
```
97
84
98
85
The following query returns the query text as well as execution statistics for all queries in natively compiled stored procedures in the current database for which statistics have been collected, ordered by total worker time, in descending order:
0 commit comments