3

I have this query that runs fine for a couple of days (from the application) and then slows down to where it can't even finish.

If I rebuild the plan, it flies.

There is a signifigant amount of data being modified in the tables that it is using.

Also if I run it from SSMS and turn ARITHABORT on, it works fine. If I turn ARITHABORT off, it hangs and hangs.

Side note - Stats are repopulated every Monday. Today (Thursday) the plan is bad. The table in question has 1.4 billion records with 9 million changed since Monday.

Is there a way I can increase performance so I don't have to rebuild this plan?

Bad Plan Bad Plan

Good Plan Good Plan

3
  • 1
    Have you looked at the compiled parameters for each plan? My guess is they're probably different. If so, you could use OPTIMIZE FOR (good parameter)
    – Queue Mann
    Commented Jun 18, 2015 at 19:00
  • I have. They are the same. Commented Jun 18, 2015 at 19:07
  • 1
    Cant you rebuild stats more often? I excpect its your stats that are causing this. Commented Jun 19, 2015 at 9:01

1 Answer 1

1

pictures

There's not a lot to go off of in the pictures of query plans you posted, which share many similarities in shape and join order. In fact, they're identical aside from estimates and costs. The bad plan has all one row estimates, and the good plan does a bit better.

What I think you're running into is either the ascending key problem, because

Stats are repopulated every Monday. Today (Thursday) the plan is bad

Or temp table caching is leading you astray. You'll wanna check out these articles for more detail there;

inference

What I see happen quite a bit with the ascending key problem and the legacy cardinality estimator is the one row guess from off-histogram values leads to poor plan choices. That's understandable, since one row guesses are rarely accurate.

A similar trick can also happen when temp tables are cached with histogram values for one execution that don't match up with subsequent executions. It's closer to parameter sniffing than the ascending key problem, but it leads to the same thing for you.

The position this puts you in is one where the attributes of the nested loops joins in your plans will show different values for being optimized and using some form of prefetching.

I've written about the performance problems this can lead to here:

You'll likely want to try updating stats on user tables more frequently, and as detailed in SQL Server Temporary Object Caching linked above:

  • Manually UPDATE STATISTICS on the temporary table within the module; and
  • Add an OPTION (RECOMPILE) hint to statements that reference the temporary table

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.