SQL Flow Field Guide

QUERY HINT OPTIMIZE FOR ( @variable_name { UNKNOWN | = <literal_constant> } [ , ...n ] )

Catalog support for SQL Server 2022 (16.x) and 2025 (17.x)

Instructs the Query Optimizer to use a particular value for a local variable when the query is compiled and optimized.

What changes

Input
none
Output or change
none

Syntax at a glance

OPTION ( OPTIMIZE FOR ( @variable_name { UNKNOWN | = <literal_constant> } [ , ...n ] ) )

Work example

A measured query issue may justify testing QUERY HINT OPTIMIZE FOR ( @variable_name { UNKNOWN | = <literal_constant> } [ , ...n ] ) against the optimizer's default choice.

Capture the current plan, rows, reads, and elapsed time. Change one hint in a test copy and compare the same parameter values.

Check your work QUERY HINT OPTIMIZE FOR ( @variable_name { UNKNOWN | = <literal_constant> } [ , ...n ] ). The intended plan behavior for QUERY HINT OPTIMIZE FOR ( @variable_name { UNKNOWN | = <literal_constant> } [ , ...n ] ) is visible and the result rows are unchanged across the comparison.

Open Microsoft Learn for full syntax, permissions, and details for your version.