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
Copy file name to clipboardExpand all lines: support/sql/database-engine/performance/troubleshoot-never-ending-query.md
+7-7Lines changed: 7 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -18,18 +18,18 @@ This article provides troubleshooting guidance for issues in which a Microsoft S
18
18
This article focuses on queries that seem to run or compile without end. That is, their CPU usage continues to increase. This article doesn't apply to queries that are blocked or waiting on a resource that's never released. In those cases, the CPU usage remains constant or changes only slightly.
19
19
20
20
> [!IMPORTANT]
21
-
> If a query is left to continue running, it might eventually finish. This process could take just a few seconds or several days.
21
+
> If a query is left to continue running, it might eventually finish. This process could take just a few seconds or several days. One exception is an endless run where a [WHILE](sql/t-sql/language-elements/while-transact-sql) loop doesn't exit.
22
22
> The term "never-ending" is used here to describe the perception of a query that doesn't finish.
23
23
24
24
## Cause
25
25
26
26
Common causes of long-running (never-ending) queries include:
27
27
28
-
-**Nested Loop (NL) joins on very large tables:** Because of the nature of NL joins, a query that joins tables that have lots of rows might run for a long time. In some cases, including `TOP`, `FAST`, and other row goals, the query must use NL joins. Therefore, even if a hash match or a Merge join might be faster, the optimizer can't use either process because of the row goal.
28
+
-**Nested Loop (NL) joins on very large tables:** Because of the nature of NL joins, a query that joins tables that have lots of rows might run for a long time. One example where NL joins is the only choice for query optimizer is the use of `TOP`, `FAST`, or `EXISTS`. Even if a Hash join or a Merge join might be faster, the optimizer can't use either operator because of the row goal. Another scenario where NL join is the only choice is where an inequality join predicate is used in a query. For example, `SELECT .. FROM tab1 AS a JOIN tab 2 AS b ON a.id > b.id`. Merge and Hash joins are not a possible query optimizer choice here. For more information, see [Joins](/sql/relational-databases/performance/joins)
29
29
-**Out-of-date statistics:** Queries that pick a plan based on outdated statistics might be suboptimal and take a long time to run.
30
30
-**Endless loops:** T-SQL queries that use WHILE loops might be incorrectly written. The resulting code never leaves the loop and runs endlessly. These queries are truly never-ending. They run until they're killed manually.
31
31
-**Complex queries that have many joins and large tables:** Queries that involve many joined tables would typically have complex query plans that might take a long time to run. This scenario is common in analytical queries that don't filter out rows and that involve a large number of tables.
32
-
-**Missing indexes:** Queries can be accelerated if appropriate indexes are used on tables. Indexes enable the selection of a subset of the data to provide faster access.
32
+
-**Missing indexes:** Queries can run significantly faster if appropriate indexes are used on tables. Indexes enable the selection of a subset of the data to provide faster access.
33
33
34
34
## Solution
35
35
@@ -42,7 +42,7 @@ Look for a never-ending query that's running on the system. You have to determin
42
42
Run the following diagnostic query on your SQL Server instance where the never-ending query is active:
43
43
44
44
```sql
45
-
DECLARE @cntr int=0
45
+
DECLARE @cntr INT=0
46
46
47
47
WHILE (@cntr <3)
48
48
BEGIN
@@ -116,7 +116,7 @@ If the slow query meets these criteria, focus on reducing its runtime. Typically
116
116
117
117
##### Long wait time
118
118
119
-
This article isn't applicable for long wait scenarios. In a wait scenario, you might receive an output that resembles the following example in which the CPU usage doesn't change or changes slightly because the session is waiting on a resource:
119
+
This article isn't applicable to long wait scenarios. In a wait scenario, you might receive an output that resembles the following example in which the CPU usage doesn't change or changes slightly because the session is waiting on a resource:
@@ -128,7 +128,7 @@ To troubleshoot queries that are long because of waits, see [Troubleshoot slow-r
128
128
129
129
##### Long compilation time
130
130
131
-
On rare occasions, you might observe that the CPU usage increases continuously over time but isn't driven by the query run. Instead, an excessively long compilation (the parsing and compiling of a query) might be the cause. In these cases, check the `transaction_name` output column for a value of `sqlsource_transform`. This transaction name indicates a compilation.
131
+
On rare occasions, you might observe that the CPU usage increases continuously over time but isn't driven by the query execution. Instead, an excessively long compilation (the parsing and compiling of a query) might be the cause. In these cases, check the `transaction_name` output column for a value of `sqlsource_transform`. This transaction name indicates a compilation.
132
132
133
133
### Step 2: Collect diagnostic logs manually
134
134
@@ -339,7 +339,7 @@ To identify the slow steps in the query, follow these steps:
339
339
You can use [SQL LogScout](https://github.com/microsoft/SQL_LogScout/releases) to capture logs while a never-ending query is running. Use the [never ending query scenario](https://github.com/microsoft/SQL_LogScout?tab=readme-ov-file#15-never-ending-query) with the following command:
0 commit comments