1

As far as I know, the OPTIMIZE FOR hint helps to avoid parameter sniffing.

For such a stored procedure:

SELECT * FROM Table WHERE param=@param1

We have two parameters S1 with high selectivity and S2 with low selectivity.

If we are using such a hint:

OPTION(OPTIMIZE FOR @param1=S1)

and then send S2 to the stored procedure, we have still parameter sniffing.

Now I have a conceptual question:

How does the OPTIMZE FOR hint help us avoid parameter sniffing?

Paul White
95.3k30 gold badges439 silver badges689 bronze badges
asked Apr 28, 2015 at 7:17

1 Answer 1

4

OPTIMIZE FOR is used for making a good plan for specific query. A classical example is a report to skewed data that is run very often with same parameters. In such a scenario, it could be useful to optimize the query for the most common parameter. This is a trade-off, as other queries with different parameter are going to get worse a plan.

If you are suffering from parameter sniffing, you could use OPTIMIZE FOR UNKNOWN, OPTION RECOMPILE or local variables. None of these is a silver bullet, so bencmark the queries carefully. Make sure the issue really is parameter sniffing and not, say, out-of-date statistics.

A Microsoft blog discusses the issue with sample code, as a question right here on dba.so.

Glorfindel
2,2095 gold badges19 silver badges26 bronze badges
answered Apr 28, 2015 at 7:46
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.