Skip to content

Commit bc55554

Browse files
Merge pull request #34336 from PiJoCoder/QPArchLocalVars_Pijocoder_060625
QP Architecutre: Add a note on local variables and improve parameter sniffing section
2 parents ea68873 + 1f00d6f commit bc55554

File tree

1 file changed

+27
-6
lines changed

1 file changed

+27
-6
lines changed

docs/relational-databases/query-processing-architecture-guide.md

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,16 @@ description: "How SQL Server processes queries and optimizes query reuse through
44
author: MikeRayMSFT
55
ms.author: mikeray
66
ms.reviewer: maghan, randolphwest, derekw
7-
ms.date: 01/14/2025
7+
ms.date: 06/06/2025
88
ms.service: sql
99
ms.topic: concept-article
1010
helpviewer_keywords:
1111
- "guide, query processing architecture"
1212
- "query processing architecture guide"
1313
- "row mode execution"
1414
- "batch mode execution"
15-
monikerRange: "=azuresqldb-current||>=sql-server-2016||>=sql-server-linux-2017||=azuresqldb-mi-current"
15+
ai-usage: ai-assisted
16+
monikerRange: "=azuresqldb-current || >=sql-server-2016 || >=sql-server-linux-2017 || =azuresqldb-mi-current"
1617
---
1718

1819
# Query processing architecture guide
@@ -147,7 +148,17 @@ The basic steps that [!INCLUDE [ssNoVersion](../includes/ssnoversion-md.md)] use
147148

148149
### Constant folding and expression evaluation
149150

150-
[!INCLUDE [ssNoVersion](../includes/ssnoversion-md.md)] evaluates some constant expressions early to improve query performance. This is referred to as constant folding. A constant is a [!INCLUDE [tsql](../includes/tsql-md.md)] literal, such as `3`, `'ABC'`, `'2005-12-31'`, `1.0e3`, or `0x12345678`.
151+
[!INCLUDE [ssNoVersion](../includes/ssnoversion-md.md)] evaluates some constant expressions early to improve query performance. This optimization technique used by the query optimizer aims to simplify expressions at compile time rather than at runtime. It involves evaluating constant expressions during query compilation so that the resulting execution plan is more efficient. This is referred to as constant folding. A constant is a [!INCLUDE [tsql](../includes/tsql-md.md)] literal, such as `3`, `'ABC'`, `'2005-12-31'`, `1.0e3`, or `0x12345678`. For example, take this query:
152+
153+
```sql
154+
SELECT * FROM Orders WHERE OrderDate < DATEADD(day, 30 * 12, '2020-01-01');
155+
```
156+
157+
Here, 30 * 12 is a constant expression. SQL Server can evaluate this during compilation and rewrite the query internally as:
158+
159+
```sql
160+
SELECT * FROM Orders WHERE OrderDate < DATEADD(day, 360, '2020-01-01');
161+
```
151162

152163
#### Foldable expressions
153164

@@ -1062,15 +1073,25 @@ Parameter values are sniffed during compilation or recompilation for the followi
10621073
- Queries submitted via `sp_executesql`
10631074
- Prepared queries
10641075

1065-
For more information on troubleshooting bad parameter sniffing issues, see:
1076+
For more information on troubleshooting parameter sniffing issues, see:
10661077
- [Investigate and resolve parameter-sensitive issues](/troubleshoot/sql/performance/troubleshoot-high-cpu-usage-issues#step-5-investigate-and-resolve-parameter-sensitive-issues)
10671078
- [Parameters and Execution Plan Reuse](#parameters-and-execution-plan-reuse)
10681079
- [Parameter Sensitive Plan optimization](./performance/parameter-sensitive-plan-optimization.md)
10691080
- [Troubleshoot queries with parameter sensitive query execution plan issues in Azure SQL Database](/azure/azure-sql/database/identify-query-performance-issues#parameter-sensitivity)
10701081
- [Troubleshoot queries with parameter sensitive query execution plan issues in Azure SQL Managed Instance](/azure/azure-sql/managed-instance/identify-query-performance-issues#parameter-sensitivity)
10711082

1072-
> [!NOTE]
1073-
> For queries using the `RECOMPILE` hint, both parameter values and current values of local variables are sniffed. The values sniffed (of parameters and local variables) are those that exist at the place in the batch just before the statement with the `RECOMPILE` hint. In particular, for parameters, the values that came along with the batch invocation call aren't sniffed.
1083+
When a query in SQL Server uses the `OPTION (RECOMPILE)` hint, the query optimizer turns parameter and local variables into compile-time constants that can be constant folded and reduced to literals. This means that during compilation, the optimizer knows and can use the current runtime values of parameters and local variables as they exist immediately prior to that statement. The OPTION (RECOMPILE) allows the optimizer to generate a more optimal query plan tailored to the specific values and to take advantage of the best underlying indexes at run time. For parameters, this process refers not to the values originally passed to the batch or stored procedure, but to their values at the time of recompilation. These values might have been modified within the procedure before reaching the statement that includes `RECOMPILE`. This behavior can improve performance for queries with highly variable or skewed input data.
1084+
1085+
### Local variables
1086+
1087+
When a query uses local variables, SQL Server can't sniff their values at compile time, so it estimates cardinality using available statistics or heuristics. If statistics exist, it typically uses the **All Density** value (also known as **average density**) from the statistical histogram to estimate how many rows match the predicate. However, if no statistics are available for the column, SQL Server falls back on heuristic estimates, such as assuming 10% selectivity for equality predicates, and 30% for inequalities and ranges, which might lead to less accurate execution plans. Here's an example of a query that uses a local variable.
1088+
1089+
```sql
1090+
DECLARE @ProductId INT = 100;
1091+
SELECT * FROM Products WHERE ProductId = @ProductId;
1092+
```
1093+
1094+
In this case, SQL Server doesn't use the value 100 to optimize the query. It uses a general estimate.
10741095

10751096
## Parallel query processing
10761097

0 commit comments

Comments
 (0)