%3CLINGO-SUB%20id%3D%22lingo-sub-1441687%22%20slang%3D%22en-US%22%3EImproving%20concurrency%20of%20asynchronous%20statistics%20update%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1441687%22%20slang%3D%22en-US%22%3E%3CH2%20id%3D%22toc-hId--1324822259%22%20id%3D%22toc-hId--1324822259%22%3EExecutive%20summary%3C%2FH2%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIn%20Azure%20SQL%20Database%20and%20Azure%20SQL%20Managed%20Instance%2C%20the%20background%20process%20that%20updates%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fsql%2Frelational-databases%2Fstatistics%2Fstatistics%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3Estatistics%3C%2FA%3E%20asynchronously%20can%20now%20wait%20for%20the%20schema%20modification%20lock%20on%20a%20low%20priority%20queue.%20This%20improves%20concurrency%20for%20workloads%20with%20frequent%20query%20plan%20(re)compilations.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ENew%20behavior%20is%20enabled%20with%20the%20ASYNC_STATS_UPDATE_WAIT_AT_LOW_PRIORITY%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fsql%2Ft-sql%2Fstatements%2Falter-database-scoped-configuration-transact-sql%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3Edatabase-scoped%20configuration%3C%2FA%3E.%20This%20feature%20is%20currently%20in%20public%20preview.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId-1162690574%22%20id%3D%22toc-hId-1162690574%22%3EAutomatic%20statistics%20update%20%E2%80%93%20synchronous%20vs.%20asynchronous%3C%2FH2%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EBy%20default%2C%20the%20SQL%20Server%20database%20engine%20updates%20statistics%20automatically%20as%20needed%20to%20reflect%20changes%20in%20the%20underlying%20data%2C%20and%20help%20the%20query%20optimizer%20generate%20more%20optimal%20plans.%20When%20a%20query%20plan%20is%20compiled%2C%20if%20existing%20statistics%20are%20considered%20out-of-date%20(stale)%2C%20new%20statistics%20are%20collected%20and%20written%20to%20the%20database%20metadata.%20By%20default%2C%20this%20happens%20synchronously%20with%20query%20execution%2C%20therefore%20the%20time%20to%20collect%20and%20write%20new%20statistics%20is%20added%20to%20the%20execution%20time%20of%20the%20query%20being%20compiled.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EWhile%20up-to-date%20statistics%20often%20improve%20query%20plan%20quality%2C%20the%20extra%20time%20added%20to%20some%20query%20executions%20due%20to%20statistics%20update%20may%20be%20undesirable%2C%20particularly%20in%20transactional%20workloads%20with%20short%20queries%2C%20where%20updating%20statistics%20may%20take%20longer%20than%20query%20execution%20itself.%20For%20this%20reason%2C%20the%20SQL%20Server%20database%20engine%20also%20supports%20an%20option%20to%20update%20statistics%20asynchronously.%20When%20the%20database%20option%20AUTO_UPDATE_STATISTICS_ASYNC%20is%20set%20to%20ON%2C%20query%20optimizer%20proceeds%20to%20compile%20and%20execute%20the%20query%20even%20if%20statistics%20are%20considered%20stale%3B%20however%2C%20stale%20statistics%20are%20then%20updated%20on%20a%20background%20thread%20asynchronously%2C%20so%20that%20future%20query%20executions%20starting%20after%20this%20asynchronous%20process%20has%20completed%20can%20benefit%20from%20up-to-date%20statistics.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId--644763889%22%20id%3D%22toc-hId--644763889%22%3EPotential%20concurrency%20impact%20of%20asynchronous%20statistics%20update%3C%2FH2%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EWhile%20asynchronous%20statistics%20update%20eliminates%20occasional%20spikes%20in%20query%20duration%20due%20to%20statistics%20update%2C%20it%20can%20sometimes%20cause%20a%20different%2C%20concurrency-related%2C%20problem%2C%20particularly%20for%20workloads%20where%20query%20plans%20are%20frequently%20compiled%20or%20recompiled.%20The%20mechanism%20in%20play%20here%20is%20non-trivial%2C%20and%20deserves%20a%20closer%20look.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EWhen%20a%20query%20plan%20is%20compiled%2C%20the%20process%20places%20schema%20stability%20(Sch-S)%20locks%20on%20all%20statistics%20metadata%20objects%20used%20by%20the%20query%20optimizer%2C%20to%20ensure%20that%20they%20are%20not%20modified%20during%20compilation.%20If%2C%20during%20compilation%2C%20the%20same%20or%20a%20different%20process%20finds%20that%20one%20of%20these%20statistics%20is%20stale%2C%20it%20will%20trigger%20an%20asynchronous%20statistics%20update%2C%20using%20a%20separate%20(background)%20process.%20That%20process%20will%20scan%20data%20to%20build%20new%20statistics%2C%20and%20will%20then%20attempt%20to%20acquire%20a%20schema%20modification%20(Sch-M)%20lock%20on%20the%20statistics%20metadata%20object%20to%20update%20it%20in%20the%20database.%20But%20this%20background%20process%20can%20get%20blocked%20by%20a%20query%20that%20is%20already%20holding%20the%20Sch-S%20lock%20on%20the%20same%20statistics%20metadata%20object%2C%20because%20its%20plan%20is%20being%20compiled%20at%20that%20time.%20At%20this%20point%2C%20new%20queries%20being%20compiled%20may%20also%20need%20to%20use%20the%20same%20statistics%20and%20attempt%20to%20place%20Sch-S%20locks%20on%20the%20same%20object%2C%20but%20they%20would%20be%20blocked%20behind%20the%20background%20process%20waiting%20for%20the%20Sch-M%20lock.%20A%20blocking%20chain%20would%20form%20as%20the%20result%2C%20limiting%20application%20concurrency%2C%20as%20shown%20in%20%3CSTRONG%3EFigure%201%3C%2FSTRONG%3E.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22wlp-disabled.png%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F196603i20B510560447B4D7%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22wlp-disabled.png%22%20alt%3D%22wlp-disabled.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3EFigure%201%3C%2FSTRONG%3E.%20An%20illustration%20of%20a%20blocking%20chain%20forming%20when%20asynchronous%20statistics%20update%20is%20waiting%20for%20a%20Sch-M%20lock%20at%20normal%20priority.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThe%20impact%20of%20this%20blocking%20chain%20could%20be%20severe%2C%20especially%20with%20frequent%20(re)compilations%20and%20statistics%20updates.%20For%20some%20workloads%20we%20have%20seen%2C%20blocking%20would%20not%20resolve%20for%20several%20minutes.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EAn%20example%20in%20the%20Appendix%20demonstrates%20this%20scenario.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId-1842748944%22%20id%3D%22toc-hId-1842748944%22%3EAsynchronous%20statistics%20update%20with%20low%20priority%20wait%3C%2FH2%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThis%20problem%20can%20now%20be%20fixed%20in%20Azure%20SQL%20Database%20and%20Azure%20SQL%20Managed%20Instance%20by%20enabling%20the%20ASYNC_STATS_UPDATE_WAIT_AT_LOW_PRIORITY%20database-scoped%20configuration.%20This%20modifies%20the%20behavior%20of%20the%20background%20update%20statistics%20process%20to%20wait%20for%20the%20Sch-M%20lock%20%3CEM%3Eat%20low%20priority%3C%2FEM%3E.%20Low%20priority%20here%20means%20that%20this%20request%20will%20not%20acquire%20the%20Sch-M%20lock%20on%20the%20statistics%20metadata%20object%20until%20all%20other%20processes%20holding%20incompatible%20locks%20on%20the%20same%20object%20release%20their%20locks.%20Thus%2C%20the%20background%20process%20will%20not%20update%20statistics%20until%20all%20running%20queries%20finish%20compilation%20and%20release%20their%20Sch-S%20locks%20(see%20%3CSTRONG%3EFigure%202%3C%2FSTRONG%3E).%20The%20low%20priority%20mechanism%20used%20here%20is%20the%20same%20as%20what%20is%20used%20in%20several%20other%20DDL%20commands%20in%20SQL%20Server%2C%20such%20as%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fsql%2Ft-sql%2Fstatements%2Falter-index-transact-sql%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3Eonline%20index%20rebuild%3C%2FA%3E%20and%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fsql%2Ft-sql%2Fstatements%2Falter-table-transact-sql%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3Epartition%20switching%3C%2FA%3E.%20The%20example%20in%20the%20Appendix%20demonstrates%20this%20mechanism%20in%20action.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22wlp-enabled.png%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F196604i6331EE9D81342E77%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22wlp-enabled.png%22%20alt%3D%22wlp-enabled.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3EFigure%202%3C%2FSTRONG%3E.%20An%20illustration%20of%20the%20absence%20of%20query%20blocking%20when%20asynchronous%20statistics%20update%20is%20waiting%20for%20Sch-M%20lock%20at%20low%20priority.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EBut%20what%20if%20multiple%20concurrent%20query%20compilations%20continue%20for%20a%20long%20time%3F%20This%20could%20mean%20that%20at%20any%20point%2C%20there%20is%20at%20least%20one%20Sch-S%20lock%20on%20the%20statistics%20metadata%20object.%20For%20some%20very%20intensive%20workloads%20we%20have%20seen%2C%20this%20does%20happen.%20In%20this%20case%2C%20the%20background%20statistics%20update%20could%20wait%20for%20a%20long%20time%20as%20well.%20Since%20there%20is%20a%20limited%20number%20of%20background%20threads%2C%20this%20could%20negatively%20impact%20other%20parts%20of%20the%20database%20engine%20and%20hold%20resources%20unnecessarily.%20To%20avoid%20this%2C%20the%20background%20process%20will%20time%20out%20after%20several%20minutes%20of%20waiting%20at%20low%20priority.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThis%20necessary%20timeout%20means%20that%20in%20rare%20cases%2C%20stale%20statistics%20would%20not%20be%20updated.%20Because%20this%20can%20lead%20to%20suboptimal%20query%20plans%2C%20the%20default%20value%20of%20the%20ASYNC_STATS_UPDATE_WAIT_AT_LOW_PRIORITY%20database-scoped%20configuration%20is%20OFF.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EAsynchronous%20statistics%20update%20operations%20that%20wait%20at%20low%20priority%20can%20be%20monitored%20using%20the%20%3CEM%3Eautostats_update_async_low_priority_status%3C%2FEM%3E%20extended%20event.%20This%20event%20fires%20when%20the%20operation%20starts%2C%20completes%20successfully%2C%20times%20out%2C%20or%20fails.%20The%20%3CEM%3Estats_update_correlation_id%3C%2FEM%3E%20field%20can%20be%20used%20to%20correlate%20multiple%20events%20for%20the%20same%20statistics%20update%20operation.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EOn%20secondary%20replicas%2C%20changes%20to%20statistics%20that%20happen%20on%20the%20primary%20are%20automatically%20persisted%20as%20part%20of%20applying%20transaction%20log%20records.%20The%20ASYNC_STATS_UPDATE_WAIT_AT_LOW_PRIORITY%20configuration%20has%20no%20effect%20on%20that%20process.%20However%2C%20this%20configuration%2C%20if%20enabled%20on%20a%20secondary%20replica%2C%20applies%20to%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fsql%2Fdatabase-engine%2Favailability-groups%2Fwindows%2Factive-secondaries-readable-secondary-replicas-always-on-availability-groups%23Read-OnlyStats%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3Etemporary%20statistics%3C%2FA%3E%20in%20the%20same%20fashion%20as%20it%20does%20to%20permanent%20statistics%20on%20the%20primary.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId-35294481%22%20id%3D%22toc-hId-35294481%22%3EConclusion%3C%2FH2%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThe%20new%20option%20to%20wait%20for%20lock%20at%20low%20priority%20in%20asynchronous%20statistics%20update%20solves%20a%20specific%20type%20of%20workload%20concurrency%20problem%20that%20has%20been%20affecting%20several%20Azure%20SQL%20and%20SQL%20Server%20customers.%20This%20solution%20is%20now%20available%20in%20Azure%20SQL%20Database%20and%20Azure%20SQL%20Managed%20Instance%2C%20and%20is%20planned%20to%20be%20included%20in%20SQL%20Server%20vNext.%20For%20customers%20seeing%20blocking%20chains%20where%20a%20background%20process%20waits%20for%20a%20Sch-M%20lock%20on%20a%20statistics%20metadata%20object%2C%20we%20recommend%20enabling%20the%20ASYNC_STATS_UPDATE_WAIT_AT_LOW_PRIORITY%20database-scoped%20configuration%20to%20improve%20workload%20concurrency.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EWe%20welcome%20your%20questions%20and%20feedback%20on%20this%20improvement%20via%20comments%20to%20this%20blog%20post%2C%20or%20via%20email%20to%20IntelligentQP%20%5Bat%5D%20microsoft%20%5Bdot%5D%20com.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId--1772159982%22%20id%3D%22toc-hId--1772159982%22%3EAppendix%3C%2FH2%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThe%20script%20below%20shows%20an%20example%20of%20a%20blocking%20chain%20that%20could%20occur%20with%20asynchronous%20statistics%20update%20enabled%2C%20and%20then%20shows%20how%20the%20new%20option%20to%20wait%20for%20a%20Sch-M%20lock%20at%20low%20priority%20avoids%20it.%20Execute%20the%20script%20one%20step%20at%20a%20time.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ENote%20that%20on%20instances%20with%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fsql%2Frelational-databases%2Fsql-server-transaction-locking-and-row-versioning-guide%23lock_partitioning%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3Elock%20partitioning%3C%2FA%3E%20enabled%2C%20the%20likelihood%20of%20encountering%20a%20blocking%20chain%20becomes%20lower%20as%20the%20number%20of%20schedulers%20and%20lock%20partitions%20increases.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EWith%20lock%20partitioning%20enabled%2C%20the%20Sch-S%20lock%20held%20by%20the%20long-running%20query%20in%20Step%202%20will%20be%20in%20a%20particular%20lock%20partition%2C%20say%20partition%204%2C%20because%20shared%20access%20requires%20a%20Sch-S%20lock%20in%20only%20one%20lock%20partition.%20When%20the%20background%20process%20updating%20statistics%20attempts%20to%20acquire%20a%20Sch-M%20lock%2C%20it%20will%20do%20that%20on%20all%20lock%20partitions%20one%20at%20a%20time%2C%20starting%20from%20lock%20partition%200%20to%20avoid%20deadlocks.%20But%20it%20will%20get%20blocked%20once%20it%20gets%20to%20lock%20partition%204%20where%20the%20long-running%20query%20is%20holding%20the%20Sch-S%20lock%2C%20and%20will%20not%20proceed%20to%20lock%20partitions%20with%20higher%20numbers.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EWhen%20we%20execute%20the%20query%20in%20Step%206%20that%20attempts%20to%20acquire%20a%20Sch-S%20lock%20on%20statistics%20metadata%20to%20compile%20its%20plan%2C%20it%20may%20be%20able%20to%20do%20so%20on%20a%20lock%20partition%20with%20a%20higher%20number%20than%20the%20ones%20where%20background%20process%20is%20holding%20or%20waiting%20for%20a%20Sch-M%20lock%2C%20i.e.%20it%20can%20acquire%20the%20Sch-S%20lock%20on%20partition%205%20and%20above.%20Thus%2C%20since%20this%20is%20the%20only%20lock%20the%20query%20needs%20for%20shared%20access%2C%20the%20query%20would%20not%20be%20blocked.%20As%20the%20total%20number%20of%20lock%20partitions%20increases%2C%20the%20likelihood%20of%20finding%20a%20lock%20partition%20without%20a%20Sch-M%20lock%20increases%20too%2C%20making%20the%20blocking%20chain%20less%20likely%20to%20occur.%3C%2FP%3E%0A%3CP%3ETo%20reproduce%20the%20problem%20reliably%2C%20use%20an%20Azure%20SQL%20database%20with%201%20scheduler%2C%20e.g.%20use%20GP_Gen4_1%20service%20objective.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EFirst%2C%20to%20set%20up%20the%20scenario%2C%20enable%20asynchronous%20statistics%20update%2C%20and%20make%20sure%20that%20the%20configuration%20to%20wait%20at%20low%20priority%20for%20asynchronous%20statistics%20update%20is%20disabled.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-sql%22%3E%3CCODE%3EALTER%20DATABASE%20CURRENT%20SET%20AUTO_UPDATE_STATISTICS_ASYNC%20ON%3B%0AALTER%20DATABASE%20SCOPED%20CONFIGURATION%20SET%20ASYNC_STATS_UPDATE_WAIT_AT_LOW_PRIORITY%20%3D%20OFF%3B%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH3%20id%3D%22toc-hId--1081598508%22%20id%3D%22toc-hId--1081598508%22%3EStep%201%3C%2FH3%3E%0A%3CP%3ECreate%20test%20table%20and%20statistics.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-sql%22%3E%3CCODE%3EDROP%20TABLE%20IF%20EXISTS%20dbo.stats_wlp%3B%0A%0ACREATE%20TABLE%20dbo.stats_wlp%0A(%0Ac1%20varchar(200)%20NOT%20NULL%20CONSTRAINT%20df_stats_wlp_c1%20DEFAULT%20('Hello%20World.')%0A)%3B%0A%0ACREATE%20STATISTICS%20stats_wlp_stat1%20ON%20dbo.stats_wlp%20(c1)%3B%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EPopulate%20test%20table%20with%2030000%20rows.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-sql%22%3E%3CCODE%3EINSERT%20INTO%20stats_wlp%20(c1)%0ASELECT%20TOP%20(30000)%20'Hello%20World.'%20AS%20c1%20%0AFROM%20sys.all_columns%20AS%20ac1%0ACROSS%20JOIN%20sys.all_columns%20AS%20ac2%3B%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH3%20id%3D%22toc-hId-1405914325%22%20id%3D%22toc-hId-1405914325%22%3EStep%202%3C%2FH3%3E%0A%3CP%3EStart%20a%20long-running%20query.%20This%20query%20places%20a%20Sch-S%20lock%20on%20the%20statistics%20metadata%20object%20to%20read%20statistics%20properties.%20This%20simulates%20a%20query%20taking%20this%20lock%20to%20compile%20its%20plan.%20Note%20the%20%3CEM%3Eobject_id%3C%2FEM%3E%20and%20%3CEM%3Estats_id%3C%2FEM%3E%20values%20for%20the%20statistics%20metadata%20object%20in%20the%20result%20set.%20They%20will%20be%20appearing%20in%20the%20following%20steps.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-sql%22%3E%3CCODE%3ESELECT%20t1.c1%2C%20s.object_id%2C%20s.stats_id%0AFROM%20dbo.stats_wlp%20t1%0ACROSS%20JOIN%20dbo.stats_wlp%20t2%0ACROSS%20JOIN%20dbo.stats_wlp%20t3%0ACROSS%20JOIN%20dbo.stats_wlp%20t4%0ACROSS%20JOIN%20sys.stats%20AS%20s%0ACROSS%20APPLY%20sys.dm_db_stats_properties(s.object_id%2C%20s.stats_id)%20AS%20sp%0AWHERE%20s.name%20%3D%20'stats_wlp_stat1'%3B%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH3%20id%3D%22toc-hId--401540138%22%20id%3D%22toc-hId--401540138%22%3EStep%203%3C%2FH3%3E%0A%3CP%3EUsing%20a%20separate%20session%20(query%20window)%2C%20observe%20a%20granted%20Sch-S%20lock%20on%20the%20statistics%20metadata%20resource%20with%20the%20same%20%3CEM%3Eobject_id%3C%2FEM%3E%20and%20%3CEM%3Estats_id%3C%2FEM%3E%2C%20held%20by%20the%20long-running%20query.%20Here%20and%20below%2C%20screenshots%20show%20a%20subset%20of%20columns%20for%20brevity.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-sql%22%3E%3CCODE%3ESELECT%20l.resource_type%2C%20l.resource_subtype%2C%20l.resource_description%2C%20l.request_mode%2C%20l.request_type%2C%20l.request_status%2C%20l.request_session_id%2C%20l.resource_lock_partition%2C%20r.blocking_session_id%2C%20r.command%2C%20r.status%2C%20r.wait_type%0AFROM%20sys.dm_tran_locks%20AS%20l%0AINNER%20JOIN%20sys.dm_exec_requests%20AS%20r%0AON%20l.request_session_id%20%3D%20r.session_id%0AWHERE%20resource_type%20%3D%20'METADATA'%0A%20%20%20%20%20%20AND%0A%20%20%20%20%20%20resource_subtype%20%3D%20'STATS'%3B%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22step3-1.png%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F196607i8E0704F6537D9122%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22step3-1.png%22%20alt%3D%22step3-1.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH3%20id%3D%22toc-hId-2085972695%22%20id%3D%22toc-hId-2085972695%22%3EStep%204%3C%2FH3%3E%0A%3CP%3EUsing%20another%20separate%20session%20(query%20window)%2C%20execute%20a%20query%20that%20will%20compile%20a%20new%20plan%20and%20trigger%20an%20asynchronous%20statistics%20update%20due%20to%20significant%20changes%20in%20data%20distribution%20(recall%20that%20we%20inserted%2030000%20rows).%20The%20query%20completes%20quickly%20because%20it%20does%20not%20wait%20for%20the%20statistics%20update%20to%20complete.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-sql%22%3E%3CCODE%3ESELECT%20COUNT(1)%20AS%20cnt%0AFROM%20dbo.stats_wlp%0AWHERE%20c1%20LIKE%20'%25Hello%25'%20%0AOPTION%20(RECOMPILE)%3B%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH3%20id%3D%22toc-hId--416645247%22%20id%3D%22toc-hId--416645247%22%3EStep%205%3C%2FH3%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EUse%20the%20query%20from%20Step%203%20to%20look%20at%20locks%20again.%20See%20that%20now%20there%20is%20a%20background%20process%20attempting%20to%20acquire%20a%20Sch-M%20lock%20on%20the%20same%20statistics%20metadata%20object%2C%20with%20%3CEM%3Erequest_status%3C%2FEM%3E%20set%20to%20CONVERT.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-sql%22%3E%3CCODE%3ESELECT%20l.resource_type%2C%20l.resource_subtype%2C%20l.resource_description%2C%20l.request_mode%2C%20l.request_type%2C%20l.request_status%2C%20l.request_session_id%2C%20l.resource_lock_partition%2C%20r.blocking_session_id%2C%20r.command%2C%20r.status%2C%20r.wait_type%0AFROM%20sys.dm_tran_locks%20AS%20l%0AINNER%20JOIN%20sys.dm_exec_requests%20AS%20r%0AON%20l.request_session_id%20%3D%20r.session_id%0AWHERE%20resource_type%20%3D%20'METADATA'%0A%20%20%20%20%20%20AND%0A%20%20%20%20%20%20resource_subtype%20%3D%20'STATS'%3B%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22step5-1.png%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F196608iFD596A272C554411%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22step5-1.png%22%20alt%3D%22step5-1.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH3%20id%3D%22toc-hId-2070867586%22%20id%3D%22toc-hId-2070867586%22%3EStep%206%3C%2FH3%3E%0A%3CP%3ERun%20the%20query%20from%20step%204%20again.%20This%20time%2C%20it%20is%20blocked%2C%20because%20it%20needs%20to%20acquire%20the%20Sch-S%20lock%20on%20the%20statistics%20metadata%20object%20to%20compile%20its%20plan%2C%20but%20it%20gets%20queued%20behind%20the%20background%20process%20waiting%20for%20the%20Sch-M%20lock.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-sql%22%3E%3CCODE%3ESELECT%20COUNT(1)%20AS%20cnt%0AFROM%20dbo.stats_wlp%0AWHERE%20c1%20LIKE%20'%25Hello%25'%20%0AOPTION%20(RECOMPILE)%3B%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22step6-1.png%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F196609iCAAB995AB40D9655%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22step6-1.png%22%20alt%3D%22step6-1.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ECancel%20the%20long-running%20query%20started%20in%20Step%202%20to%20resolve%20the%20blocking%20chain.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ENow%20let%E2%80%99s%20enable%20the%20configuration%20to%20wait%20at%20low%20priority%20for%20asynchronous%20statistics%20update%2C%20to%20see%20how%20it%20changes%20this%20scenario.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-sql%22%3E%3CCODE%3EALTER%20DATABASE%20SCOPED%20CONFIGURATION%20SET%20ASYNC_STATS_UPDATE_WAIT_AT_LOW_PRIORITY%20%3D%20ON%3B%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ERepeat%20steps%201-5.%20In%20step%205%2C%20see%20that%20for%20the%20background%20process%20waiting%20for%20Sch-M%20lock%20to%20update%20statistics%20asynchronously%2C%20%3CEM%3Erequest_status%3C%2FEM%3E%20is%20now%20LOW_PRIORITY_CONVERT%2C%20and%20%3CEM%3Ewait_type%3C%2FEM%3E%20is%20LCK_M_SCH_M_LOW_PRIORITY.%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22step5-2.png%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F196610i6A6CF7DEB8989870%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22step5-2.png%22%20alt%3D%22step5-2.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIn%20step%206%2C%20you%20can%20see%20that%20the%20query%20is%20no%20longer%20blocked%20and%20completes%20quickly%2C%20because%20it%20is%20able%20to%20acquire%20the%20Sch-S%20lock%20on%20the%20statistics%20metadata%20object%20in%20front%20of%20the%20background%20process%20waiting%20on%20a%20Sch-M%20lock%20at%20low%20priority.%20We%20can%20continue%20executing%20the%20same%20query%20multiple%20times%20without%20any%20blocking.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ECancel%20the%20long-running%20query.%20The%20query%20showing%20statistics%20metadata%20locks%20now%20returns%20an%20empty%20set%2C%20because%20as%20soon%20as%20we%20canceled%20the%20long-running%20query%2C%20the%20background%20process%20was%20able%20to%20acquire%20the%20Sch-M%20lock%2C%20update%20statistics%20successfully%2C%20and%20release%20the%20lock.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-1441687%22%20slang%3D%22en-US%22%3E%3CP%3EIn%20Azure%20SQL%20Database%20and%20Azure%20SQL%20Managed%20Instance%2C%20the%20background%20process%20that%20updates%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fsql%2Frelational-databases%2Fstatistics%2Fstatistics%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%20target%3D%22_blank%22%3Estatistics%3C%2FA%3E%20asynchronously%20can%20now%20wait%20for%20the%20schema%20modification%20lock%20on%20a%20low%20priority%20queue.%20This%20improves%20concurrency%20for%20workloads%20with%20frequent%20query%20plan%20(re)compilations.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ENew%20behavior%20is%20enabled%20with%20the%20ASYNC_STATS_UPDATE_WAIT_AT_LOW_PRIORITY%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fsql%2Ft-sql%2Fstatements%2Falter-database-scoped-configuration-transact-sql%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%20target%3D%22_blank%22%3Edatabase-scoped%20configuration%3C%2FA%3E.%20This%20feature%20is%20currently%20in%20public%20preview.%3C%2FP%3E%3C%2FLINGO-TEASER%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1441687%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EAzure%20SQL%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Microsoft

Executive summary

 

In Azure SQL Database and Azure SQL Managed Instance, the background process that updates statistics asynchronously can now wait for the schema modification lock on a low priority queue. This improves concurrency for workloads with frequent query plan (re)compilations.

 

New behavior is enabled with the ASYNC_STATS_UPDATE_WAIT_AT_LOW_PRIORITY database-scoped configuration. This feature is currently in public preview.

 

Automatic statistics update – synchronous vs. asynchronous

 

By default, the SQL Server database engine updates statistics automatically as needed to reflect changes in the underlying data, and help the query optimizer generate more optimal plans. When a query plan is compiled, if existing statistics are considered out-of-date (stale), new statistics are collected and written to the database metadata. By default, this happens synchronously with query execution, therefore the time to collect and write new statistics is added to the execution time of the query being compiled.

 

While up-to-date statistics often improve query plan quality, the extra time added to some query executions due to statistics update may be undesirable, particularly in transactional workloads with short queries, where updating statistics may take longer than query execution itself. For this reason, the SQL Server database engine also supports an option to update statistics asynchronously. When the database option AUTO_UPDATE_STATISTICS_ASYNC is set to ON, query optimizer proceeds to compile and execute the query even if statistics are considered stale; however, stale statistics are then updated on a background thread asynchronously, so that future query executions starting after this asynchronous process has completed can benefit from up-to-date statistics.

 

Potential concurrency impact of asynchronous statistics update

 

While asynchronous statistics update eliminates occasional spikes in query duration due to statistics update, it can sometimes cause a different, concurrency-related, problem, particularly for workloads where query plans are frequently compiled or recompiled. The mechanism in play here is non-trivial, and deserves a closer look.

 

When a query plan is compiled, the process places schema stability (Sch-S) locks on all statistics metadata objects used by the query optimizer, to ensure that they are not modified during compilation. If, during compilation, the same or a different process finds that one of these statistics is stale, it will trigger an asynchronous statistics update, using a separate (background) process. That process will scan data to build new statistics, and will then attempt to acquire a schema modification (Sch-M) lock on the statistics metadata object to update it in the database. But this background process can get blocked by a query that is already holding the Sch-S lock on the same statistics metadata object, because its plan is being compiled at that time. At this point, new queries being compiled may also need to use the same statistics and attempt to place Sch-S locks on the same object, but they would be blocked behind the background process waiting for the Sch-M lock. A blocking chain would form as the result, limiting application concurrency, as shown in Figure 1.

 

wlp-disabled.png

Figure 1. An illustration of a blocking chain forming when asynchronous statistics update is waiting for a Sch-M lock at normal priority.

 

The impact of this blocking chain could be severe, especially with frequent (re)compilations and statistics updates. For some workloads we have seen, blocking would not resolve for several minutes.

 

An example in the Appendix demonstrates this scenario.

 

Asynchronous statistics update with low priority wait

 

This problem can now be fixed in Azure SQL Database and Azure SQL Managed Instance by enabling the ASYNC_STATS_UPDATE_WAIT_AT_LOW_PRIORITY database-scoped configuration. This modifies the behavior of the background update statistics process to wait for the Sch-M lock at low priority. Low priority here means that this request will not acquire the Sch-M lock on the statistics metadata object until all other processes holding incompatible locks on the same object release their locks. Thus, the background process will not update statistics until all running queries finish compilation and release their Sch-S locks (see Figure 2). The low priority mechanism used here is the same as what is used in several other DDL commands in SQL Server, such as online index rebuild and partition switching. The example in the Appendix demonstrates this mechanism in action.

 

wlp-enabled.png

Figure 2. An illustration of the absence of query blocking when asynchronous statistics update is waiting for Sch-M lock at low priority.

 

But what if multiple concurrent query compilations continue for a long time? This could mean that at any point, there is at least one Sch-S lock on the statistics metadata object. For some very intensive workloads we have seen, this does happen. In this case, the background statistics update could wait for a long time as well. Since there is a limited number of background threads, this could negatively impact other parts of the database engine and hold resources unnecessarily. To avoid this, the background process will time out after several minutes of waiting at low priority.

 

This necessary timeout means that in rare cases, stale statistics would not be updated. Because this can lead to suboptimal query plans, the default value of the ASYNC_STATS_UPDATE_WAIT_AT_LOW_PRIORITY database-scoped configuration is OFF.

 

Asynchronous statistics update operations that wait at low priority can be monitored using the autostats_update_async_low_priority_status extended event. This event fires when the operation starts, completes successfully, times out, or fails. The stats_update_correlation_id field can be used to correlate multiple events for the same statistics update operation.

 

On secondary replicas, changes to statistics that happen on the primary are automatically persisted as part of applying transaction log records. The ASYNC_STATS_UPDATE_WAIT_AT_LOW_PRIORITY configuration has no effect on that process. However, this configuration, if enabled on a secondary replica, applies to temporary statistics in the same fashion as it does to permanent statistics on the primary.

 

Conclusion

 

The new option to wait for lock at low priority in asynchronous statistics update solves a specific type of workload concurrency problem that has been affecting several Azure SQL and SQL Server customers. This solution is now available in Azure SQL Database and Azure SQL Managed Instance, and is planned to be included in SQL Server vNext. For customers seeing blocking chains where a background process waits for a Sch-M lock on a statistics metadata object, we recommend enabling the ASYNC_STATS_UPDATE_WAIT_AT_LOW_PRIORITY database-scoped configuration to improve workload concurrency.

 

We welcome your questions and feedback on this improvement via comments to this blog post, or via email to IntelligentQP [at] microsoft [dot] com.

 

Appendix

 

The script below shows an example of a blocking chain that could occur with asynchronous statistics update enabled, and then shows how the new option to wait for a Sch-M lock at low priority avoids it. Execute the script one step at a time.

 

Note that on instances with lock partitioning enabled, the likelihood of encountering a blocking chain becomes lower as the number of schedulers and lock partitions increases.

 

With lock partitioning enabled, the Sch-S lock held by the long-running query in Step 2 will be in a particular lock partition, say partition 4, because shared access requires a Sch-S lock in only one lock partition. When the background process updating statistics attempts to acquire a Sch-M lock, it will do that on all lock partitions one at a time, starting from lock partition 0 to avoid deadlocks. But it will get blocked once it gets to lock partition 4 where the long-running query is holding the Sch-S lock, and will not proceed to lock partitions with higher numbers.

 

When we execute the query in Step 6 that attempts to acquire a Sch-S lock on statistics metadata to compile its plan, it may be able to do so on a lock partition with a higher number than the ones where background process is holding or waiting for a Sch-M lock, i.e. it can acquire the Sch-S lock on partition 5 and above. Thus, since this is the only lock the query needs for shared access, the query would not be blocked. As the total number of lock partitions increases, the likelihood of finding a lock partition without a Sch-M lock increases too, making the blocking chain less likely to occur.

To reproduce the problem reliably, use an Azure SQL database with 1 scheduler, e.g. use GP_Gen4_1 service objective.

 

First, to set up the scenario, enable asynchronous statistics update, and make sure that the configuration to wait at low priority for asynchronous statistics update is disabled.

 

ALTER DATABASE CURRENT SET AUTO_UPDATE_STATISTICS_ASYNC ON;
ALTER DATABASE SCOPED CONFIGURATION SET ASYNC_STATS_UPDATE_WAIT_AT_LOW_PRIORITY = OFF;

 

Step 1

Create test table and statistics.

 

DROP TABLE IF EXISTS dbo.stats_wlp;

CREATE TABLE dbo.stats_wlp
(
c1 varchar(200) NOT NULL CONSTRAINT df_stats_wlp_c1 DEFAULT ('Hello World.')
);

CREATE STATISTICS stats_wlp_stat1 ON dbo.stats_wlp (c1);

 

Populate test table with 30000 rows.

 

INSERT INTO stats_wlp (c1)
SELECT TOP (30000) 'Hello World.' AS c1 
FROM sys.all_columns AS ac1
CROSS JOIN sys.all_columns AS ac2;

 

Step 2

Start a long-running query. This query places a Sch-S lock on the statistics metadata object to read statistics properties. This simulates a query taking this lock to compile its plan. Note the object_id and stats_id values for the statistics metadata object in the result set. They will be appearing in the following steps.

 

SELECT t1.c1, s.object_id, s.stats_id
FROM dbo.stats_wlp t1
CROSS JOIN dbo.stats_wlp t2
CROSS JOIN dbo.stats_wlp t3
CROSS JOIN dbo.stats_wlp t4
CROSS JOIN sys.stats AS s
CROSS APPLY sys.dm_db_stats_properties(s.object_id, s.stats_id) AS sp
WHERE s.name = 'stats_wlp_stat1';

 

Step 3

Using a separate session (query window), observe a granted Sch-S lock on the statistics metadata resource with the same object_id and stats_id, held by the long-running query. Here and below, screenshots show a subset of columns for brevity.

 

SELECT l.resource_type, l.resource_subtype, l.resource_description, l.request_mode, l.request_type, l.request_status, l.request_session_id, l.resource_lock_partition, r.blocking_session_id, r.command, r.status, r.wait_type
FROM sys.dm_tran_locks AS l
INNER JOIN sys.dm_exec_requests AS r
ON l.request_session_id = r.session_id
WHERE resource_type = 'METADATA'
      AND
      resource_subtype = 'STATS';

 

step3-1.png

 

Step 4

Using another separate session (query window), execute a query that will compile a new plan and trigger an asynchronous statistics update due to significant changes in data distribution (recall that we inserted 30000 rows). The query completes quickly because it does not wait for the statistics update to complete.

 

SELECT COUNT(1) AS cnt
FROM dbo.stats_wlp
WHERE c1 LIKE '%Hello%' 
OPTION (RECOMPILE);

 

Step 5

 

Use the query from Step 3 to look at locks again. See that now there is a background process attempting to acquire a Sch-M lock on the same statistics metadata object, with request_status set to CONVERT.

 

SELECT l.resource_type, l.resource_subtype, l.resource_description, l.request_mode, l.request_type, l.request_status, l.request_session_id, l.resource_lock_partition, r.blocking_session_id, r.command, r.status, r.wait_type
FROM sys.dm_tran_locks AS l
INNER JOIN sys.dm_exec_requests AS r
ON l.request_session_id = r.session_id
WHERE resource_type = 'METADATA'
      AND
      resource_subtype = 'STATS';

 

step5-1.png

 

Step 6

Run the query from step 4 again. This time, it is blocked, because it needs to acquire the Sch-S lock on the statistics metadata object to compile its plan, but it gets queued behind the background process waiting for the Sch-M lock.

 

SELECT COUNT(1) AS cnt
FROM dbo.stats_wlp
WHERE c1 LIKE '%Hello%' 
OPTION (RECOMPILE);

 

step6-1.png

 

Cancel the long-running query started in Step 2 to resolve the blocking chain.

 

Now let’s enable the configuration to wait at low priority for asynchronous statistics update, to see how it changes this scenario.

 

ALTER DATABASE SCOPED CONFIGURATION SET ASYNC_STATS_UPDATE_WAIT_AT_LOW_PRIORITY = ON;

 

Repeat steps 1-5. In step 5, see that for the background process waiting for Sch-M lock to update statistics asynchronously, request_status is now LOW_PRIORITY_CONVERT, and wait_type is LCK_M_SCH_M_LOW_PRIORITY.

step5-2.png

 

In step 6, you can see that the query is no longer blocked and completes quickly, because it is able to acquire the Sch-S lock on the statistics metadata object in front of the background process waiting on a Sch-M lock at low priority. We can continue executing the same query multiple times without any blocking.

 

Cancel the long-running query. The query showing statistics metadata locks now returns an empty set, because as soon as we canceled the long-running query, the background process was able to acquire the Sch-M lock, update statistics successfully, and release the lock.