I have a query that I suspect is guilty of parameter-sniffing. I'd like to apply an interceptor like the one found here, but I don't want any other queries affected.
The code used there is: DbInterception.Add(new OptionRecompileHintDbCommandInterceptor());
What is the scope of this command and how do you limit the scope of an interceptor to just a single query?
My first thought was to do something like this:
var interceptor = new OptionRecompileHintDbCommandInterceptor();
try
{
DbInterception.Add(interceptor);
// Do query
}
finally
{
DbInterception.Remove(interceptor);
}
But is this same to do or could it affect queries on other threads?
Update
Another thought occurred to me. If I can't control when an interceptor is applied, is there a way to put the logic in a conditional if
statement in the interceptor and then supply some kind of context to the interceptor so it knows when to apply the logic?