Skip to content

Commit 90e2f9c

Browse files
authored
Correct and refine OPTIMIZE FOR
Also adjusting Example "B".
1 parent 0c5f0e3 commit 90e2f9c

1 file changed

Lines changed: 7 additions & 7 deletions

File tree

docs/t-sql/queries/hints-transact-sql-query.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ Is a literal constant value to be assigned _\@variable\_name_ for use with the O
233233
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).
234234

235235
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.
237237

238238
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.
239239

@@ -401,17 +401,17 @@ GO
401401
```
402402

403403
### 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.
405405

406406
```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
411411
SELECT * FROM Person.Address
412412
WHERE City = @city_name AND PostalCode = @postal_code
413413
OPTION ( OPTIMIZE FOR (@city_name = 'Seattle', @postal_code UNKNOWN) );
414-
GO
414+
GO
415415
```
416416

417417
### C. Using MAXRECURSION

0 commit comments

Comments
 (0)