|
If I submit a query that necessitates auto creation or updating of statistics during the compilation stage and the command timeout is reached before this is finished what happens? Do stats still get updated? If so does this happen via a background process or does the timeout request have to wait until that is complete?
(comments are locked)
|
|
I have now tested this. With the following test table And the following test code Auto Creation Profiler shows the following
First it creates the stats for column It can be seen that the stats creation occurs on the same spid as the query and so this also aborts the creation of stats on column Auto Update Synchronous The above refers to stats creation, to test auto update of the stats I ran the above query without a timeout so both sets of stats were successfully created then truncated and repopulated the table so that the stats would be out of date and re-ran the test. The trace for that is pretty similar showing only one update managed to complete.
Auto Update Asynch Finally just for completeness with
Looking back at my answer I may have been a little quick and didn't adequately explain the usage of setting auto stat updates to async which does use a background thread letting the executing query complete with out of date statistics.
Dec 07 '11 at 05:14 AM
Blackhawk-17
(comments are locked)
|
|
If updating statistics causes the query to exceed its command timeout, the query will fail. I do not know whether stats still get updated. Since the process is synchronous, it's possible that the stats update fails as well. To see what happens for sure, you can fire up profiler and monitor the Performance > Auto Stats event then run a query that causes a statistics update which, in turn, causes the query to time out. To avoid this timeout situation, since SQL Server 2005, you have been able set the auto update to asynchronous with AUTO_UPDATE_STATISTICS_ASYNC ON|OFF (OFF by default) - http://msdn.microsoft.com/en-us/library/bb522682.aspx. This database-wide setting causes your query to trigger an Auto Stats event when appropriate, but then it compiles and completes using the old statistics so it doesn't have to wait for new ones. The trade-off being you run with a potentially non-optimal plan. Always a risk, especially as the statistics approach, but don't quite reach, the auto update threshold - http://technet.microsoft.com/en-us/library/cc966419.aspx. Thanks. I was hoping that some one would know the answer without me having to resort to that. If nobody does then at some point I'll perform this experiment and post the results (the motivation for the question is because I seem to remember reading somewhere that this was an advantage of the asynch mode but I haven't read it anywhere else and can't see anything in BOL that bears that out)
Jun 02 '11 at 10:37 PM
Martin 1
(comments are locked)
|
|
Stats are updated prior to plan compilation and it is generally a very quick process. The stats will be updated irrespective of the timeout. While the update may be initiated due to a batch it occurs outside of it and is performed by a background process. Setting auto stat updates to async defers the updating so the current batch isn't slowed down by the update - negligible in many instances IMHO. Without this setting the batch enters a wait condition until the auto stats update completes. If the updates were dependent on the batch and the scenario you describe were to happen frequently then the whole stats architecture would be suspect. They really need to be all or nothing operations. This is not correct. I have tested in profiler and that shows stats maintenance happens on the same spid as the query execution.
Dec 07 '11 at 03:23 AM
Martin 1
(comments are locked)
|




