Query slows down after a couple of days, Why?
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?
2_3 is the old plan
3_3 is the new plan that works fine.
First, the problem:
The problem is that your stored procedure will cache an execution plan which is optimized for a certain data skew. Then, when your parameters change and the data set is much different, your stored procedure will execute with the cached plan and perform very poorly.
The reason that ARITHABORT settings modify your experience in SSMS is because the set options are included in how SQL Server identifies whether the same query is being executed and can look into the plan cache for a pre-compiled plan. By default SSMS has ARITHABORT ON, while .NET, ODBC and OLEDB default to OFF. This difference means that, by default, SSMS will use a different compiled plan than your applications will.
There is a nice table here, in "The Default Settings" section.
Now for solutions:
- You can set WITH RECOMPILE in your stored procedure so that it always has the best plan possible. This has a performance and server resource hit to consider.
- A better option is to prevent parameter sniffing with the use of local variables. There is a lot of good information here.
- My preference, if you are using SQL Server 2008 or higher, is to use the OPTIMIZE FOR UNKNOWN query option. There is good information on that here.