Locks caused by async stats update












0















SQL Server has an option for statistics to autoupdate asynchronously (AUTO_UPDATE_STATISTICS_ASYNC).



But, even if this option is ON, from time to time I see locks like this on pic below: say, spid 549 locks spid 42 and spid 42 locks 8 other sessions. As I can see, system spid tries to change old statistics to a newly computed one, so it waits for SCH-M lock to acquire.



enter image description here



Suddenly, it turns out, async stats update is not so async: any long-running SELECT (which, of course, holds SCH-S lock) can freeze system spid, which, in turn, can gather queue of many other sessions. Even worse, whole queue wait time depends on of locker’s SELECT duration. It means, if I have a long OLAP query, all the stalled sessions could be timed out.



So, my question is: am I missing something or it works just by design? Can I somehow avoid these sudden locks, caused by autoupdated statistics?










share|improve this question














bumped to the homepage by Community 3 mins ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.




















    0















    SQL Server has an option for statistics to autoupdate asynchronously (AUTO_UPDATE_STATISTICS_ASYNC).



    But, even if this option is ON, from time to time I see locks like this on pic below: say, spid 549 locks spid 42 and spid 42 locks 8 other sessions. As I can see, system spid tries to change old statistics to a newly computed one, so it waits for SCH-M lock to acquire.



    enter image description here



    Suddenly, it turns out, async stats update is not so async: any long-running SELECT (which, of course, holds SCH-S lock) can freeze system spid, which, in turn, can gather queue of many other sessions. Even worse, whole queue wait time depends on of locker’s SELECT duration. It means, if I have a long OLAP query, all the stalled sessions could be timed out.



    So, my question is: am I missing something or it works just by design? Can I somehow avoid these sudden locks, caused by autoupdated statistics?










    share|improve this question














    bumped to the homepage by Community 3 mins ago


    This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.


















      0












      0








      0








      SQL Server has an option for statistics to autoupdate asynchronously (AUTO_UPDATE_STATISTICS_ASYNC).



      But, even if this option is ON, from time to time I see locks like this on pic below: say, spid 549 locks spid 42 and spid 42 locks 8 other sessions. As I can see, system spid tries to change old statistics to a newly computed one, so it waits for SCH-M lock to acquire.



      enter image description here



      Suddenly, it turns out, async stats update is not so async: any long-running SELECT (which, of course, holds SCH-S lock) can freeze system spid, which, in turn, can gather queue of many other sessions. Even worse, whole queue wait time depends on of locker’s SELECT duration. It means, if I have a long OLAP query, all the stalled sessions could be timed out.



      So, my question is: am I missing something or it works just by design? Can I somehow avoid these sudden locks, caused by autoupdated statistics?










      share|improve this question














      SQL Server has an option for statistics to autoupdate asynchronously (AUTO_UPDATE_STATISTICS_ASYNC).



      But, even if this option is ON, from time to time I see locks like this on pic below: say, spid 549 locks spid 42 and spid 42 locks 8 other sessions. As I can see, system spid tries to change old statistics to a newly computed one, so it waits for SCH-M lock to acquire.



      enter image description here



      Suddenly, it turns out, async stats update is not so async: any long-running SELECT (which, of course, holds SCH-S lock) can freeze system spid, which, in turn, can gather queue of many other sessions. Even worse, whole queue wait time depends on of locker’s SELECT duration. It means, if I have a long OLAP query, all the stalled sessions could be timed out.



      So, my question is: am I missing something or it works just by design? Can I somehow avoid these sudden locks, caused by autoupdated statistics?







      sql-server statistics maintenance






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jul 4 '17 at 7:58









      0wl0wl

      234




      234





      bumped to the homepage by Community 3 mins ago


      This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.







      bumped to the homepage by Community 3 mins ago


      This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
























          1 Answer
          1






          active

          oldest

          votes


















          0














          It has nothing to do with SCH-S. Select queries which acquire S and IS locks are compatible with SCH-S, which is called Schema Stability and all it does, it prevents table during query execution from being modified which means SCH-S and SCH-M are not compatible. SCH-S is compatible with all other locks.



          Your problem however is that you have auto update stats enabled, which are triggered whenever there is certain amount of data changed within a table (20% pre SQL2016 or with 2371 traceflag counts % depending on number of rows) and it does when you execute the query.



          So what might have happened is that your table was pretty busy and query optimizer decided it should update statistics, so you had to wait.



          You should disable auto update statistics and update it manually






          share|improve this answer


























          • Will updating stats manually not cause blocking?

            – Erik Darling
            Jul 4 '17 at 9:33











          • "Will updating stats manually not cause blocking? " -- it will

            – 0wl
            Jul 4 '17 at 9:37













          • It will Erik, but at least he can be prepared for it, and update whenever he believes that these queries are not executed

            – S4V1N
            Jul 4 '17 at 9:42











          • "Your problem however is that you have auto update stats enabled" -- Autoupdate statistics is exactly what I am trying to implement. And my goal is to make it create as less locks as possible. I thougth, async update would help me, but, you see, there are some complications. I agree, It has nothing with Schema-Stability. Problem occurs when system spid acquires Schema-Modification lock (to change, like I said, old statistics to a new one). And, as I see, this lock can cause a lot of other users stall on Schema-Stability lock, which is incompatible with SCH-M.

            – 0wl
            Jul 4 '17 at 9:42













          • You should monitor how many rows are inserted/updated/deleted on a daily basis on that table/tables, and how often and when is that query executed and try to come up with a plan when to update statistics

            – S4V1N
            Jul 4 '17 at 10:02











          Your Answer








          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "182"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f177932%2flocks-caused-by-async-stats-update%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          It has nothing to do with SCH-S. Select queries which acquire S and IS locks are compatible with SCH-S, which is called Schema Stability and all it does, it prevents table during query execution from being modified which means SCH-S and SCH-M are not compatible. SCH-S is compatible with all other locks.



          Your problem however is that you have auto update stats enabled, which are triggered whenever there is certain amount of data changed within a table (20% pre SQL2016 or with 2371 traceflag counts % depending on number of rows) and it does when you execute the query.



          So what might have happened is that your table was pretty busy and query optimizer decided it should update statistics, so you had to wait.



          You should disable auto update statistics and update it manually






          share|improve this answer


























          • Will updating stats manually not cause blocking?

            – Erik Darling
            Jul 4 '17 at 9:33











          • "Will updating stats manually not cause blocking? " -- it will

            – 0wl
            Jul 4 '17 at 9:37













          • It will Erik, but at least he can be prepared for it, and update whenever he believes that these queries are not executed

            – S4V1N
            Jul 4 '17 at 9:42











          • "Your problem however is that you have auto update stats enabled" -- Autoupdate statistics is exactly what I am trying to implement. And my goal is to make it create as less locks as possible. I thougth, async update would help me, but, you see, there are some complications. I agree, It has nothing with Schema-Stability. Problem occurs when system spid acquires Schema-Modification lock (to change, like I said, old statistics to a new one). And, as I see, this lock can cause a lot of other users stall on Schema-Stability lock, which is incompatible with SCH-M.

            – 0wl
            Jul 4 '17 at 9:42













          • You should monitor how many rows are inserted/updated/deleted on a daily basis on that table/tables, and how often and when is that query executed and try to come up with a plan when to update statistics

            – S4V1N
            Jul 4 '17 at 10:02
















          0














          It has nothing to do with SCH-S. Select queries which acquire S and IS locks are compatible with SCH-S, which is called Schema Stability and all it does, it prevents table during query execution from being modified which means SCH-S and SCH-M are not compatible. SCH-S is compatible with all other locks.



          Your problem however is that you have auto update stats enabled, which are triggered whenever there is certain amount of data changed within a table (20% pre SQL2016 or with 2371 traceflag counts % depending on number of rows) and it does when you execute the query.



          So what might have happened is that your table was pretty busy and query optimizer decided it should update statistics, so you had to wait.



          You should disable auto update statistics and update it manually






          share|improve this answer


























          • Will updating stats manually not cause blocking?

            – Erik Darling
            Jul 4 '17 at 9:33











          • "Will updating stats manually not cause blocking? " -- it will

            – 0wl
            Jul 4 '17 at 9:37













          • It will Erik, but at least he can be prepared for it, and update whenever he believes that these queries are not executed

            – S4V1N
            Jul 4 '17 at 9:42











          • "Your problem however is that you have auto update stats enabled" -- Autoupdate statistics is exactly what I am trying to implement. And my goal is to make it create as less locks as possible. I thougth, async update would help me, but, you see, there are some complications. I agree, It has nothing with Schema-Stability. Problem occurs when system spid acquires Schema-Modification lock (to change, like I said, old statistics to a new one). And, as I see, this lock can cause a lot of other users stall on Schema-Stability lock, which is incompatible with SCH-M.

            – 0wl
            Jul 4 '17 at 9:42













          • You should monitor how many rows are inserted/updated/deleted on a daily basis on that table/tables, and how often and when is that query executed and try to come up with a plan when to update statistics

            – S4V1N
            Jul 4 '17 at 10:02














          0












          0








          0







          It has nothing to do with SCH-S. Select queries which acquire S and IS locks are compatible with SCH-S, which is called Schema Stability and all it does, it prevents table during query execution from being modified which means SCH-S and SCH-M are not compatible. SCH-S is compatible with all other locks.



          Your problem however is that you have auto update stats enabled, which are triggered whenever there is certain amount of data changed within a table (20% pre SQL2016 or with 2371 traceflag counts % depending on number of rows) and it does when you execute the query.



          So what might have happened is that your table was pretty busy and query optimizer decided it should update statistics, so you had to wait.



          You should disable auto update statistics and update it manually






          share|improve this answer















          It has nothing to do with SCH-S. Select queries which acquire S and IS locks are compatible with SCH-S, which is called Schema Stability and all it does, it prevents table during query execution from being modified which means SCH-S and SCH-M are not compatible. SCH-S is compatible with all other locks.



          Your problem however is that you have auto update stats enabled, which are triggered whenever there is certain amount of data changed within a table (20% pre SQL2016 or with 2371 traceflag counts % depending on number of rows) and it does when you execute the query.



          So what might have happened is that your table was pretty busy and query optimizer decided it should update statistics, so you had to wait.



          You should disable auto update statistics and update it manually







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Jul 4 '17 at 13:47

























          answered Jul 4 '17 at 9:13









          S4V1NS4V1N

          1




          1













          • Will updating stats manually not cause blocking?

            – Erik Darling
            Jul 4 '17 at 9:33











          • "Will updating stats manually not cause blocking? " -- it will

            – 0wl
            Jul 4 '17 at 9:37













          • It will Erik, but at least he can be prepared for it, and update whenever he believes that these queries are not executed

            – S4V1N
            Jul 4 '17 at 9:42











          • "Your problem however is that you have auto update stats enabled" -- Autoupdate statistics is exactly what I am trying to implement. And my goal is to make it create as less locks as possible. I thougth, async update would help me, but, you see, there are some complications. I agree, It has nothing with Schema-Stability. Problem occurs when system spid acquires Schema-Modification lock (to change, like I said, old statistics to a new one). And, as I see, this lock can cause a lot of other users stall on Schema-Stability lock, which is incompatible with SCH-M.

            – 0wl
            Jul 4 '17 at 9:42













          • You should monitor how many rows are inserted/updated/deleted on a daily basis on that table/tables, and how often and when is that query executed and try to come up with a plan when to update statistics

            – S4V1N
            Jul 4 '17 at 10:02



















          • Will updating stats manually not cause blocking?

            – Erik Darling
            Jul 4 '17 at 9:33











          • "Will updating stats manually not cause blocking? " -- it will

            – 0wl
            Jul 4 '17 at 9:37













          • It will Erik, but at least he can be prepared for it, and update whenever he believes that these queries are not executed

            – S4V1N
            Jul 4 '17 at 9:42











          • "Your problem however is that you have auto update stats enabled" -- Autoupdate statistics is exactly what I am trying to implement. And my goal is to make it create as less locks as possible. I thougth, async update would help me, but, you see, there are some complications. I agree, It has nothing with Schema-Stability. Problem occurs when system spid acquires Schema-Modification lock (to change, like I said, old statistics to a new one). And, as I see, this lock can cause a lot of other users stall on Schema-Stability lock, which is incompatible with SCH-M.

            – 0wl
            Jul 4 '17 at 9:42













          • You should monitor how many rows are inserted/updated/deleted on a daily basis on that table/tables, and how often and when is that query executed and try to come up with a plan when to update statistics

            – S4V1N
            Jul 4 '17 at 10:02

















          Will updating stats manually not cause blocking?

          – Erik Darling
          Jul 4 '17 at 9:33





          Will updating stats manually not cause blocking?

          – Erik Darling
          Jul 4 '17 at 9:33













          "Will updating stats manually not cause blocking? " -- it will

          – 0wl
          Jul 4 '17 at 9:37







          "Will updating stats manually not cause blocking? " -- it will

          – 0wl
          Jul 4 '17 at 9:37















          It will Erik, but at least he can be prepared for it, and update whenever he believes that these queries are not executed

          – S4V1N
          Jul 4 '17 at 9:42





          It will Erik, but at least he can be prepared for it, and update whenever he believes that these queries are not executed

          – S4V1N
          Jul 4 '17 at 9:42













          "Your problem however is that you have auto update stats enabled" -- Autoupdate statistics is exactly what I am trying to implement. And my goal is to make it create as less locks as possible. I thougth, async update would help me, but, you see, there are some complications. I agree, It has nothing with Schema-Stability. Problem occurs when system spid acquires Schema-Modification lock (to change, like I said, old statistics to a new one). And, as I see, this lock can cause a lot of other users stall on Schema-Stability lock, which is incompatible with SCH-M.

          – 0wl
          Jul 4 '17 at 9:42







          "Your problem however is that you have auto update stats enabled" -- Autoupdate statistics is exactly what I am trying to implement. And my goal is to make it create as less locks as possible. I thougth, async update would help me, but, you see, there are some complications. I agree, It has nothing with Schema-Stability. Problem occurs when system spid acquires Schema-Modification lock (to change, like I said, old statistics to a new one). And, as I see, this lock can cause a lot of other users stall on Schema-Stability lock, which is incompatible with SCH-M.

          – 0wl
          Jul 4 '17 at 9:42















          You should monitor how many rows are inserted/updated/deleted on a daily basis on that table/tables, and how often and when is that query executed and try to come up with a plan when to update statistics

          – S4V1N
          Jul 4 '17 at 10:02





          You should monitor how many rows are inserted/updated/deleted on a daily basis on that table/tables, and how often and when is that query executed and try to come up with a plan when to update statistics

          – S4V1N
          Jul 4 '17 at 10:02


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Database Administrators Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f177932%2flocks-caused-by-async-stats-update%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          SQL Server 17 - Attemping to backup to remote NAS but Access is denied

          Always On Availability groups resolving state after failover - Remote harden of transaction...

          Restoring from pg_dump with foreign key constraints