Documente Academic
Documente Profesional
Documente Cultură
OPTION (RECOMPILE)
1.Workaround
: OPTION (Recompile)
- Let's assume that this SP is called %99 percent for "Bothell" and only %1
percent for "Seattle"
- But if the first time it is called with "Seattle" paramater, after that all
"Bothell" queries run with undesired query plan
- If you have a workload like this, you can use OPTION (OPTIMIZE FOR
(@VARIABLE=VALUE)) workaround
- This option causes the compilation process to ignore the value for the specified
variable and use the specified value instead.
- Unlike the first workaround (Option(Recompile)), this option avoid the overhead
of recompiling the query every time.
*/
--Alter SP
Alter proc my_AddressSP (@city nvarchar(30))
as
select * from Person.Address
where city=@city
option (optimize for (@city='Bothell'))
from Person.Address
where city=@city
option (optimize for (@city UNKNOWN))
--check the estimated number of rows. It's 34.1113 for every city
exec my_AddressSP 'Seattle'
exec my_AddressSP 'Bothell'
from Person.Address
where city=@city_x
--drop sp
drop proc my_AddressSP
Parameter sniffing
Parameter sniffing can be set to off with help of trace flag.
We need to enable trace flag 7646
With DBCC trace on (7646)
And then we can verify with
Validate Trace Flag "7646" is ON -- DBCC TRACESTATUS (7646)
To show all trace flags, which told me that it wasn't set (after reboot, I guess). To set the trace flag
permanently, I did this:
;-T{tracenumber}