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: docs/t-sql/queries/hints-transact-sql-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
@@ -233,7 +233,7 @@ Is a literal constant value to be assigned _\@variable\_name_ for use with the O
233
233
OPTIMIZE FOR can counteract the optimizer's default parameter detection behavior. Also use OPTIMIZE FOR when you create plan guides. For more information, see [Recompile a Stored Procedure](../../relational-databases/stored-procedures/recompile-a-stored-procedure.md).
234
234
235
235
OPTIMIZE FOR UNKNOWN
236
-
Instructs the Query Optimizer to use statistical data instead of the initial values for all local variables when the query is compiled and optimized. This optimization includes parameters created with forced parameterization.
236
+
Instructs the Query Optimizer to use the average selectivity of the predicate across all column values instead of using the runtime parameter value when the query is compiled and optimized.
237
237
238
238
If you use OPTIMIZE FOR @variable_name = _literal\_constant_ and OPTIMIZE FOR UNKNOWN in the same query hint, the Query Optimizer will use the _literal\_constant_ specified for a specific value. The Query Optimizer will use UNKNOWN for the rest of the variable values. The values are used only during query optimization, and not during query execution.
239
239
@@ -401,17 +401,17 @@ GO
401
401
```
402
402
403
403
### B. Using OPTIMIZE FOR
404
-
The following example instructs the Query Optimizer to use the value `'Seattle'` for local variable `@city_name` and to use statistical data to determine the value for the local variable`@postal_code` when optimizing the query. The example uses the [!INCLUDE[ssSampleDBobject](../../includes/sssampledbobject-md.md)] database.
404
+
The following example instructs the Query Optimizer to use the value `'Seattle'` for `@city_name` and to use the average selectivity of the predicate across all column values for`@postal_code` when optimizing the query. The example uses the [!INCLUDE[ssSampleDBobject](../../includes/sssampledbobject-md.md)] database.
405
405
406
406
```sql
407
-
DECLARE @city_name nvarchar(30);
408
-
DECLARE @postal_code nvarchar(15);
409
-
SET @city_name ='Ascheim';
410
-
SET @postal_code =86171;
407
+
CREATE PROCEDURE dbo.RetrievePersonAddress
408
+
@city_name nvarchar(30),
409
+
@postal_code nvarchar(15)
410
+
AS
411
411
SELECT*FROMPerson.Address
412
412
WHERE City = @city_name AND PostalCode = @postal_code
413
413
OPTION ( OPTIMIZE FOR (@city_name ='Seattle', @postal_code UNKNOWN) );
0 commit comments