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
@@ -147,7 +148,17 @@ The basic steps that [!INCLUDE [ssNoVersion](../includes/ssnoversion-md.md)] use
147
148
148
149
### Constant folding and expression evaluation
149
150
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
+
```
151
162
152
163
#### Foldable expressions
153
164
@@ -1062,15 +1073,25 @@ Parameter values are sniffed during compilation or recompilation for the followi
1062
1073
- Queries submitted via `sp_executesql`
1063
1074
- Prepared queries
1064
1075
1065
-
For more information on troubleshooting bad parameter sniffing issues, see:
1076
+
For more information on troubleshooting parameter sniffing issues, see:
1066
1077
-[Investigate and resolve parameter-sensitive issues](/troubleshoot/sql/performance/troubleshoot-high-cpu-usage-issues#step-5-investigate-and-resolve-parameter-sensitive-issues)
1067
1078
-[Parameters and Execution Plan Reuse](#parameters-and-execution-plan-reuse)
1068
1079
-[Parameter Sensitive Plan optimization](./performance/parameter-sensitive-plan-optimization.md)
1069
1080
-[Troubleshoot queries with parameter sensitive query execution plan issues in Azure SQL Database](/azure/azure-sql/database/identify-query-performance-issues#parameter-sensitivity)
1070
1081
-[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)
1071
1082
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.
0 commit comments