My application search becomes slow some times in a day.And whenever i update statistics for a particular table,it gets fixed. So i am thinking of creating a job to run update statistics every morning by 11 am for that table alone.Currently the statistics is set to update by itself with SQL server default sampling. Is it a bad idea to schedule a job to run Update statistics with full scan for that one particular table.This issue happens in production environment.
-
Does the table you are updating the statistics on get updated or reloaded frequently?– rvsc48Commented Oct 2, 2017 at 20:39
-
>>>whenever i update statistics for a particular table,it gets fixed.<<< If it fixes the issue of course you should do it regularly as you suggested– sepupicCommented Oct 2, 2017 at 20:42
-
1Which version of SQL Server are you on? It may have something to do with ascending keys– Erik Reasonable Rates DarlingCommented Oct 2, 2017 at 20:59
-
I am on SQL Server 2008R2.– sparktechCommented Oct 3, 2017 at 17:00
-
I don't want the application to go slow and then update and fix the issue.Updating statistics manually whenever issue happens is not a good practise ryt.– sparktechCommented Oct 3, 2017 at 17:01
2 Answers
While you could schedule a job to run UPDATE STATISTICS
on that particular table, you should also look into Parameter Sniffing. What's likely happening here is that a bad plan was wiped from the plan cache when you updated statistics on that table. Instead of doing a costly UPDATE STATISTICS
, remove the bad plan from cache. Once wiped from the plan cache, the query will compile and get a new plan. Whichever parameter values were used at the time of compilation is what the execution plan will be optimized, that is as long as you are not overriding that behavior with the OPTION OPTIMIZE FOR
query hint.
If the search function is a stored procedure, you can use the good ol' sp_recompile
. Otherwise, use DBCC FREEPROCACHE
and pass that command the SQL handle or the plan handle. You can use sp_BlitzCache with ExpertMode = 1 to get the command (it's all the way to the right in the result set).
To help diagnose which plan is causing the issue, I like to use sp_WhoIsActive or sp_BlitzCache. For sp_WhoIsActive, I like to log current activity to a table and then I can go back in time to compare execution plans and see who was the culprit.
For more information on Parameter Sniffing, see Erland's Slow in the Application, Fast in SSMS? post.
MS recommends not updating statistics too frequently as this can cause queries which use the table you are updating stats for to recompile. If that recompile time exceeds your gain from improving the query plan, there is a perf tradeoff. However, until this occurs, compiling the queries with the most up to date stats can beneficial (improves the query plan) especially on a very volatile table, which is why I asked was the table loaded or re-loaded frequently.
-
1
-
Hi Eric, 2nd paragraph at top - learn.microsoft.com/en-us/sql/t-sql/statements/…– rvsc48Commented Oct 3, 2017 at 12:56
-