SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Bad Query Plan


Bad Query Plan

Author
Message
TheSQLGuru
TheSQLGuru
SSChampion
SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)

Group: General Forum Members
Points: 12565 Visits: 8559
Robert klimes (8/7/2014)
Roger Sabin (8/7/2014)
I tried what you suggest and it does force the "bad" plan when I use 40 and a "good" plan when I use 17. Thanks.


So now you verified what is happening the "fix" is up to you.

As Kevin suggested, you can use optimize for (which I have used on a few occasions) which will always create a plan for a specific parameter or if you use unknown then it will build a plan based on all stats for all values. in either case you will not always have an optimal plan.

Another option is to add with recompile to the proc which will generate the best plan for each parameter at the expense of having to recompile each time it runs. Depending on your workload and resources this may be acceptable or not.

yet another option would be to refactor the proc so it always generates the same plan.


I was definitely NOT espousing the use of OPTIMIZE FOR as a SOLUTION for this issue - just to expose it. I DESPISE that "feature", because it GUARANTEES you will get a BAD PLAN for at least some of your executions, potentially many of them!

Best,
Kevin G. Boles
SQL Server Consultant
SQL MVP 2007-2012
TheSQLGuru on googles mail service
Robert klimes
Robert klimes
SSCrazy
SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)

Group: General Forum Members
Points: 2691 Visits: 3422
TheSQLGuru (8/7/2014)


I was definitely NOT espousing the use of OPTIMIZE FOR as a SOLUTION for this issue - just to expose it. I DESPISE that "feature", because it GUARANTEES you will get a BAD PLAN for at least some of your executions, potentially many of them!


I apologize. I misunderstood that you were suggesting to try OPTIMIZE FOR to identify the plans for different parameters instead of correcting the issue. While I agree this isn't the best option to solve bad plans caused by parameter sniffing, it may be good enough or it may be the best option. Only testing the different options would identify that.

Re-factoring the procedure to get the best plan is ideal but sometimes not possible.

Bob
-----------------------------------------------------------------------------
How to post to get the best help
ScottPletcher
ScottPletcher
SSCertifiable
SSCertifiable (7.9K reputation)SSCertifiable (7.9K reputation)SSCertifiable (7.9K reputation)SSCertifiable (7.9K reputation)SSCertifiable (7.9K reputation)SSCertifiable (7.9K reputation)SSCertifiable (7.9K reputation)SSCertifiable (7.9K reputation)

Group: General Forum Members
Points: 7947 Visits: 7160
My first choice would be to use RECOMPILE and just force SQL to rebuild a plan every time.

But, if a HASH join is the "good" plan, forcing a HASH join is much safer overall than forcing a LOOP join. You might try that for a range of values and verify that it works OK across all of them. This, too, may not be the "best" solution, but it should be a workable solution.

SQL DBA,SQL Server MVP(07, 08, 09)[size=2]Prosecutor James Blackburn, in closing argument in the Fatal Vision murders trial: If in the future, you should cry a tear, cry one for them [the murder victims]. If in the future, you should say a prayer, say one for them. And if in the future, you should light a candle, light one for them.[/size]
TheSQLGuru
TheSQLGuru
SSChampion
SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)

Group: General Forum Members
Points: 12565 Visits: 8559
ScottPletcher (8/8/2014)
My first choice would be to use RECOMPILE and just force SQL to rebuild a plan every time.

But, if a HASH join is the "good" plan, forcing a HASH join is much safer overall than forcing a LOOP join. You might try that for a range of values and verify that it works OK across all of them. This, too, may not be the "best" solution, but it should be a workable solution.


I am curious why you say HASH force would be safer. I would say just the opposite...

Best,
Kevin G. Boles
SQL Server Consultant
SQL MVP 2007-2012
TheSQLGuru on googles mail service
ScottPletcher
ScottPletcher
SSCertifiable
SSCertifiable (7.9K reputation)SSCertifiable (7.9K reputation)SSCertifiable (7.9K reputation)SSCertifiable (7.9K reputation)SSCertifiable (7.9K reputation)SSCertifiable (7.9K reputation)SSCertifiable (7.9K reputation)SSCertifiable (7.9K reputation)

Group: General Forum Members
Points: 7947 Visits: 7160
TheSQLGuru (8/8/2014)
ScottPletcher (8/8/2014)
My first choice would be to use RECOMPILE and just force SQL to rebuild a plan every time.

But, if a HASH join is the "good" plan, forcing a HASH join is much safer overall than forcing a LOOP join. You might try that for a range of values and verify that it works OK across all of them. This, too, may not be the "best" solution, but it should be a workable solution.


I am curious why you say HASH force would be safer. I would say just the opposite...


My thinking is:
LOOP is extremely -- even prohibitively -- expensive on a very large number of rows.
HASH might not be ideal for a smaller number of rows, but it shouldn't be awful either.

SQL DBA,SQL Server MVP(07, 08, 09)[size=2]Prosecutor James Blackburn, in closing argument in the Fatal Vision murders trial: If in the future, you should cry a tear, cry one for them [the murder victims]. If in the future, you should say a prayer, say one for them. And if in the future, you should light a candle, light one for them.[/size]
TheSQLGuru
TheSQLGuru
SSChampion
SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)

Group: General Forum Members
Points: 12565 Visits: 8559
ScottPletcher (8/8/2014)
TheSQLGuru (8/8/2014)
ScottPletcher (8/8/2014)
My first choice would be to use RECOMPILE and just force SQL to rebuild a plan every time.

But, if a HASH join is the "good" plan, forcing a HASH join is much safer overall than forcing a LOOP join. You might try that for a range of values and verify that it works OK across all of them. This, too, may not be the "best" solution, but it should be a workable solution.


I am curious why you say HASH force would be safer. I would say just the opposite...


My thinking is:
LOOP is extremely -- even prohibitively -- expensive on a very large number of rows.
HASH might not be ideal for a smaller number of rows, but it shouldn't be awful either.


Expensive in lots of logical IOs, yet. But those can be exceedingly quick due to cached iterative hits on same page for multiple rows. More importantly from my experience is the page locks that will (hopefully) be taken which can DRASTICALLY improve concurrency. Those blocking index/table scans are a killer from that perspective.

Best,
Kevin G. Boles
SQL Server Consultant
SQL MVP 2007-2012
TheSQLGuru on googles mail service
ScottPletcher
ScottPletcher
SSCertifiable
SSCertifiable (7.9K reputation)SSCertifiable (7.9K reputation)SSCertifiable (7.9K reputation)SSCertifiable (7.9K reputation)SSCertifiable (7.9K reputation)SSCertifiable (7.9K reputation)SSCertifiable (7.9K reputation)SSCertifiable (7.9K reputation)

Group: General Forum Members
Points: 7947 Visits: 7160
TheSQLGuru (8/8/2014)
ScottPletcher (8/8/2014)
TheSQLGuru (8/8/2014)
ScottPletcher (8/8/2014)
My first choice would be to use RECOMPILE and just force SQL to rebuild a plan every time.

But, if a HASH join is the "good" plan, forcing a HASH join is much safer overall than forcing a LOOP join. You might try that for a range of values and verify that it works OK across all of them. This, too, may not be the "best" solution, but it should be a workable solution.


I am curious why you say HASH force would be safer. I would say just the opposite...


My thinking is:
LOOP is extremely -- even prohibitively -- expensive on a very large number of rows.
HASH might not be ideal for a smaller number of rows, but it shouldn't be awful either.


Expensive in lots of logical IOs, yet. But those can be exceedingly quick due to cached iterative hits on same page for multiple rows. More importantly from my experience is the page locks that will (hopefully) be taken which can DRASTICALLY improve concurrency. Those blocking index/table scans are a killer from that perspective.


I've just not had the experience of loops being "exceeding quick" once the number of rows gets too large. Indeed, to me it seems that often the only reason SQL is using a loop is that it couldn't accurately pre-determine the cardinality of rows.

SQL DBA,SQL Server MVP(07, 08, 09)[size=2]Prosecutor James Blackburn, in closing argument in the Fatal Vision murders trial: If in the future, you should cry a tear, cry one for them [the murder victims]. If in the future, you should say a prayer, say one for them. And if in the future, you should light a candle, light one for them.[/size]
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search