Home
%3CLINGO-SUB%20id%3D%22lingo-sub-385004%22%20slang%3D%22en-US%22%3EDevelopers%20Choice%3A%20Query%20progress%20-%20anytime%2C%20anywhere%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-385004%22%20slang%3D%22en-US%22%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3EFirst%20published%20on%20MSDN%20on%20Dec%2002%2C%202016%20%3C%2FSTRONG%3E%20%3CBR%20%2F%3EEdit%20(4%2F23%2F2018)%3A%20due%20to%20a%20possible%20random%20AV%20running%20your%20favorite%20monitoring%20stored%20procedure%2C%20please%20make%20sure%20you%20install%20%3CA%20href%3D%22http%3A%2F%2Fsupport.microsoft.com%2Fhelp%2F4078596%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%20KB%204078596%20%3C%2FA%3E%20in%20your%20SQL%20Server%202016%20and%20SQL%20Server%202017.%20%3CBR%20%2F%3E%3CBR%20%2F%3EEdit%20(9%2F24%2F2018)%3A%20this%20feature%20is%20now%20%3CSTRONG%3E%20enabled%20by%20default%20in%20SQL%20Server%202019%20%3C%2FSTRONG%3E%20.%20Trace%20flag%20not%20needed%20anymore!%20Also%2C%20new%20query%20hint%20%3CSTRONG%3E%20query_plan_profile%20%3C%2FSTRONG%3E%20to%20enable%20lightweight%20profiling%20at%20the%20query%20level%2C%20for%20any%20session%2C%20in%20SQL%20Server%202019%2C%202017%20CU11%20and%202016%20SP2%20CU3.%20%3CBR%20%2F%3E%3CBR%20%2F%3EOne%20of%20the%20most%20important%20actions%20when%20a%20performance%20issue%20hits%2C%20is%20to%20get%20precise%20understanding%20on%20the%20workload%20that%E2%80%99s%20executing%20and%20how%20resource%20usage%20is%20being%20driven.%20The%20actual%20execution%20plan%20is%20an%20invaluable%20tool%20for%20this%20purpose.%20%3CBR%20%2F%3EQuery%20completion%20is%20a%20prerequisite%20for%20the%20availability%20of%20an%20actual%20query%20plan%2C%20but%20with%20LQS%20(%20%3CA%20href%3D%22https%3A%2F%2Fmsdn.microsoft.com%2Fen-us%2Flibrary%2Fdn831878.aspx%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%20Live%20Query%20Statistics%20%3C%2FA%3E%20)%2C%20you%20can%20already%20get%20information%20about%20in-flight%20query%20executions%20(see%20%3CA%20title%3D%22https%3A%2F%2Fblogs.msdn.microsoft.com%2Fsql_server_team%2Foperator-progress-changes-in-lqs%2F%22%20href%3D%22https%3A%2F%2Fblogs.msdn.microsoft.com%2Fsql_server_team%2Foperator-progress-changes-in-lqs%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%20this%20blog%20post%20%3C%2FA%3E%20)%2C%20which%20is%20especially%20useful%20for%20long%20running%20queries%2C%20and%20queries%20that%20run%20indefinitely%20and%20never%20finish.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CFONT%20size%3D%225%22%3EUp%20to%20SQL%20Server%202016%20RTM%3C%2FFONT%3E%20%3CBR%20%2F%3ETo%20look%20at%20in-flight%20query%20executions%2C%20the%20%3CA%20href%3D%22http%3A%2F%2Fsupport.microsoft.com%2Fkb%2F3170113%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%20query%20execution%20statistics%20profile%20infrastructure%20%3C%2FA%3E%20must%20be%20enabled%20on%20demand%20(later%20in%20this%20post%20referred%20to%20as%20Standard).%20%3CBR%20%2F%3E%3CBR%20%2F%3EThe%20query%20execution%20statistics%20profile%20infrastructure%20can%20be%20enabled%20%3CSTRONG%3E%20for%20a%20target%20session%20%3C%2FSTRONG%3E%20%3A%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FP%3E%0A%3CUL%3E%0A%3CUL%3E%0A%3CLI%3EClicking%20on%20%3CEM%3E%20Include%20Live%20Query%20Statistics%20%3C%2FEM%3E%20in%20SSMS%2C%20starting%20with%20SQL%20Server%202014.%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3C%2FUL%3E%0A%3CUL%3E%0A%3CUL%3E%0A%3CLI%3ESET%20STATISTICS%20XML%20ON%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3C%2FUL%3E%0A%3CUL%3E%0A%3CUL%3E%0A%3CLI%3ESET%20STATISTICS%20PROFILE%20ON%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3C%2FUL%3E%0A%3CP%3EOr%20%3CSTRONG%3E%20globally%20%3C%2FSTRONG%3E%20%2C%20to%20view%20LQS%20%3CSTRONG%3E%20for%20all%20sessions%20%3C%2FSTRONG%3E%20(such%20as%20from%20Activity%20Monitor)%2C%20by%20enabling%20the%20%3CEM%3E%20query_post_execution_showplan%20%3C%2FEM%3E%20(starting%20with%20SQL%20Server%202014).%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20334px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F98202iBBD34E00C8CDD05C%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20%2F%3E%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20338px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F98203i649107233BD031ED%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20%2F%3E%3C%2FSPAN%3E%20%3CBR%20%2F%3E%3CBR%20%2F%3EBut%20enabling%20this%20event%20can%20take%20a%20big%20performance%20toll%20in%20your%20production%20server%2C%20and%20so%20probably%20you%20are%20not%20running%20with%20it%20continuously%2C%20even%20if%20it%20gives%20you%20the%20ability%20to%20tap%20into%20any%20running%20executions%2C%20and%20query%20plans%20with%20execution%20statistics.%20%3CBR%20%2F%3E%3CBR%20%2F%3EWith%20SQL%20Server%202016%20and%20SQL%20Server%202014%20SP2%2C%20we%20managed%20to%20considerably%20bring%20down%20the%20performance%20toll%20of%20running%20this%20xEvent%20(see%20table%20below%2C%20middle%20row)%2C%20and%20also%20introduced%20the%20%3CEM%3E%20query_thread_profile%20%3C%2FEM%3E%20extended%20event.%20This%20also%20enables%20LQS%20%3CSTRONG%3E%20for%20all%20sessions%20%3C%2FSTRONG%3E%20%2C%20using%20Activity%20Monitor%20or%20directly%20querying%20%3CA%20href%3D%22https%3A%2F%2Fmsdn.microsoft.com%2Fen-us%2Flibrary%2Fdn223301.aspx%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%20%3CEM%3E%20sys.dm_exec_query_profiles%20%3C%2FEM%3E%20%3C%2FA%3E%20.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CFONT%20size%3D%225%22%3EEnters%20SQL%20Server%202016%20SP1%20%3C%2FFONT%3E%3CBR%20%2F%3ENow%20with%20SQL%20Server%202016%20SP1%2C%20we%20are%20introducing%20a%20real%20%3CSTRONG%3E%20lightweight%20%3C%2FSTRONG%3E%20query%20execution%20statistics%20profiling%20infrastructure%2C%20to%20%3CSTRONG%3E%20dramatically%20reduce%20performance%20overhead%20of%20collecting%20per-operator%20query%20execution%20statistics%20%3C%2FSTRONG%3E%20%2C%20such%20as%20actual%20number%20of%20rows.%20This%20feature%20can%20be%20enabled%20either%20using%20global%20startup%20%3CSTRONG%3E%20%3CA%20href%3D%22http%3A%2F%2Faka.ms%2Ftraceflags%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%20trace%20flag%207412%20%3C%2FA%3E%20%3C%2FSTRONG%3E%20%2C%20or%20is%20automatically%20turned%20on%20when%20%3CEM%3E%20query_thread_profile%20%3C%2FEM%3E%20xEvent%20is%20enabled.%20Again%2C%20this%20also%20enables%20LQS%20%3CSTRONG%3E%20for%20all%20sessions%20%3C%2FSTRONG%3E%20%2C%20using%20Activity%20Monitor%20or%20directly%20querying%20%3CEM%3E%20sys.dm_exec_query_profiles%20%3C%2FEM%3E%20.%3C%2FP%3E%0A%3CP%3E%3CBR%20%2F%3E%3CFONT%20size%3D%224%22%3EHow%20much%20lightweight%20you%20ask%3F%20%3C%2FFONT%3E%3CBR%20%2F%3EWe%20ran%20a%20TPC-C%20like%20workload%20in%20both%20the%20legacy%20and%20lightweight%20profiling%20infrastructures%2C%20and%20this%20is%20what%20we%20found%3A%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20580px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F98204iC86DA89F3412E888%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20%2F%3E%3C%2FSPAN%3E%20%3CBR%20%2F%3E%3CBR%20%2F%3ESo%20you%20can%20see%20above%20that%20enabling%20the%20new%20lightweight%20profiling%20infrastructure%20(with%20the%20%3CA%20href%3D%22http%3A%2F%2Faka.ms%2Ftraceflags%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%20trace%20flag%207412%20%3C%2FA%3E%20)%20has%20a%20max%20%3CSTRONG%3E%20overhead%20of%202%20percent%20%3C%2FSTRONG%3E%20%2C%20as%20opposed%20to%2075%20percent%20before%20SQL%20Server%202014%20SP2%2F2016%20RTM.%20This%20means%20that%20now%2C%20%3CSTRONG%3E%20for%20any%20server%20that%20is%20not%20already%20resource%20bound%2C%20you%20can%20run%20the%20lightweight%20profiling%20infrastructure%20continuously%2C%20and%20tap%20into%20any%20running%20execution%20at%20any%20time%20using%20for%20example%20Activity%20Monitor%20or%20directly%20querying%20%3CEM%3E%20sys.dm_exec_query_profiles%20%3C%2FEM%3E%20%2C%26nbsp%3Band%20get%20the%20query%20plan%20with%20execution%20statistics%20%3C%2FSTRONG%3E%20.%20%3CBR%20%2F%3E%3CBR%20%2F%3EThe%20possibilities%20this%20unlocks%20for%20performance%20troubleshooting%20in%20production%20environments%20are%20tremendous%2C%20and%20if%20your%20servers%20are%20not%20already%20resource%20bound%2C%20we%20encourage%20you%20to%20enable%20this%20trace%20flag%20for%20the%20potential%20it%20unlocks.%20As%20usual%2C%20we%20recommend%20you%20test%20this%20in%20a%20pre-production%20environment%20to%20estimate%20your%20specific%20impact%20beforehand.%20%3CBR%20%2F%3E%3CBR%20%2F%3EWhen%20the%20lightweight%20profiling%20is%20enabled%2C%20the%20information%20in%20%3CEM%3E%20sys.dm_exec_query_profiles%20%3C%2FEM%3E%20(and%20so%20LQS)%20is%20available%20via%20the%20new%20profiling%20infrastructure%20instead%20of%20the%20legacy%20profiling%20infrastructure.%3C%2FP%3E%0A%3CP%3E%3CBR%20%2F%3E%3CFONT%20size%3D%225%22%3EExamples%20%3C%2FFONT%3E%3CBR%20%2F%3EHere's%20a%20few%20usage%20examples%3A%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FP%3E%0A%3CUL%3E%0A%3CUL%3E%0A%3CLI%3EClicking%20on%20%3CEM%3E%20Include%20Live%20Query%20Statistics%20%3C%2FEM%3E%20in%20SSMS%20uses%20the%20legacy%20profiling%20infrastructure%20starting%20with%20SQL%20Server%202014%20RTM%2C%20but%20with%20SQL%20Server%202014%20SP2%20and%20SQL%20Server%202016%2C%20doing%20the%20same%20uses%20an%20earlier%20version%20of%20the%20lightweight%20profiling%20infrastructure.%20In%20SQL%20Server%202016%20SP1%2C%20and%20only%20by%20setting%20TF%20%3CSTRONG%3E%207412%20%3C%2FSTRONG%3E%20or%20using%20%3CSTRONG%3E%20query_thread_profile%20%3C%2FSTRONG%3E%20xEvent%2C%20you%20can%20then%20click%20on%20%3CEM%3E%20Include%20Live%20Query%20Statistics%20%3C%2FEM%3E%20and%20use%20the%20new%20lightweight%20profiling%20infrastructure.%20Starting%20with%20SQL%20Server%202016%20SP2CU3%20and%20SQL%20Server%202017%20CU11%2C%20the%20%3CA%20href%3D%22https%3A%2F%2Fsupport.microsoft.com%2Fhelp%2F4458593%2Fupdate-adds-lightweight-query-profiling-hint-in-sql-server-2016-and-20%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%20new%26nbsp%3Bquery%20hint%20%3C%2FA%3E%20%3CSTRONG%3E%20query_plan_profile%20%3C%2FSTRONG%3E%20can%20be%20used%20to%20enable%20lightweight%20profiling%20at%20the%20query%20level%2C%20for%20any%20session.%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3C%2FUL%3E%0A%3CP%3E%3CBR%20%2F%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F98205i8C569A33F7DDDE4D%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20%2F%3E%3C%2FSPAN%3E%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FP%3E%0A%3CUL%3E%0A%3CUL%3E%0A%3CLI%3ESET%20STATISTICS%20XML%20ON%2C%26nbsp%3BSET%20STATISTICS%20PROFILE%20ON%20and%20%3CEM%3E%20query_post_execution_showplan%20%3C%2FEM%3E%20xEvent%20always%20use%20the%20legacy%20profiling%20infrastructure.%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3C%2FUL%3E%0A%3CP%3EThere%E2%80%99s%20also%20the%20new%20DMF%20%3CA%20href%3D%22https%3A%2F%2Fmsdn.microsoft.com%2Fen-us%2Flibrary%2Fmt791503.aspx%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%20sys.dm_exec_query_statistics_xml%20%3C%2FA%3E%20%2C%20which%20returns%20the%20query%20execution%20plan%20for%20in-flight%20requests.%20For%20example%2C%20you%20can%20now%20use%20this%20DMF%20to%20get%20the%20plan%20for%20a%20query%20while%20it%20is%20running%2C%20and%20works%20under%20both%20legacy%20and%20lightweight%20profiling%20infrastructure.%20%3CBR%20%2F%3E%3CBR%20%2F%3EUse%20this%20DMV%20to%20retrieve%20the%20showplan%20XML%20with%20transient%20statistics%2C%20as%20seen%20below%3A%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20713px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F98206i284A2432C7FFBFC9%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20%2F%3E%3C%2FSPAN%3E%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20712px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F98207iEF3DDAAE5083A01D%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20%2F%3E%3C%2FSPAN%3E%20%3CBR%20%2F%3E%3CBR%20%2F%3EAnd%20you%20can%20join%20with%20other%20DMVs%20like%20sys.dm_exec_requests.%20The%20%3CA%20title%3D%22Running-Blocked-Processes%22%20href%3D%22https%3A%2F%2Fgithub.com%2FMicrosoft%2Ftigertoolbox%2Ftree%2Fmaster%2FRunning-Blocked-Processes%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%20Running-Blocked-Processes%20%3C%2FA%3E%20script%20in%20the%20%3CA%20href%3D%22http%3A%2F%2Faka.ms%2Ftigertoolbox%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%20TigerToolbox%20%3C%2FA%3E%20Github%20is%20already%20using%20this%20to%20access%20the%20%E2%80%9Clive_query_plan_snapshot%E2%80%9D%2C%20among%20other%20useful%20data.%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F98208i4DF4FB150C0DC7F5%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CFONT%20size%3D%225%22%3EIn%20summary%20%3C%2FFONT%3E%3CBR%20%2F%3EIf%20your%20SQL%20Server%20is%20%3CSTRONG%3E%20not%20already%20CPU%20bound%20and%20a%201.5%25%20to%202%25%20overhead%20is%20negligible%20for%20your%20workloads%2C%20we%20recommend%20you%20enable%20TF%207412%20as%20a%20startup%20trace%20flag%20%3C%2FSTRONG%3E%20%2C%20and%20unleash%20the%20power%20of%20live%20query%20troubleshooting%20in%20production%20environments.%20%3CBR%20%2F%3E%3CBR%20%2F%3EPedro%20Lopes%20(%20%3CA%20href%3D%22https%3A%2F%2Ftwitter.com%2Fsqlpto%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%20%40sqlpto%20%3C%2FA%3E%20)%20%E2%80%93%20Senior%20Program%20Manager%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-385004%22%20slang%3D%22en-US%22%3E%3CP%3EFirst%20published%20on%20MSDN%20on%20Dec%2002%2C%202016%20Edit%20(4%2F23%2F2018)%3A%20due%20to%20a%20possible%20random%20AV%20running%20your%20favorite%20monitoring%20stored%20procedure%2C%20please%20make%20sure%20you%20install%20KB%204078596%20in%20your%20SQL%20Server%202016%20and%20SQL%20Server%202017.%3C%2FP%3E%3C%2FLINGO-TEASER%3E%3CLINGO-LABS%20id%3D%22lingo-labs-385004%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3ESQLServerTiger%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Microsoft

 

First published on MSDN on Dec 02, 2016
Edit (4/23/2018): due to a possible random AV running your favorite monitoring stored procedure, please make sure you install KB 4078596 in your SQL Server 2016 and SQL Server 2017.

Edit (9/24/2018): this feature is now enabled by default in SQL Server 2019 . Trace flag not needed anymore! Also, new query hint query_plan_profile to enable lightweight profiling at the query level, for any session, in SQL Server 2019, 2017 CU11 and 2016 SP2 CU3.

One of the most important actions when a performance issue hits, is to get precise understanding on the workload that’s executing and how resource usage is being driven. The actual execution plan is an invaluable tool for this purpose.
Query completion is a prerequisite for the availability of an actual query plan, but with LQS ( Live Query Statistics ), you can already get information about in-flight query executions (see this blog post ), which is especially useful for long running queries, and queries that run indefinitely and never finish.

 

Up to SQL Server 2016 RTM
To look at in-flight query executions, the query execution statistics profile infrastructure must be enabled on demand (later in this post referred to as Standard).

The query execution statistics profile infrastructure can be enabled for a target session :

    • Clicking on Include Live Query Statistics in SSMS, starting with SQL Server 2014.
    • SET STATISTICS XML ON
    • SET STATISTICS PROFILE ON

Or globally , to view LQS for all sessions (such as from Activity Monitor), by enabling the query_post_execution_showplan (starting with SQL Server 2014).



But enabling this event can take a big performance toll in your production server, and so probably you are not running with it continuously, even if it gives you the ability to tap into any running executions, and query plans with execution statistics.

With SQL Server 2016 and SQL Server 2014 SP2, we managed to considerably bring down the performance toll of running this xEvent (see table below, middle row), and also introduced the query_thread_profile extended event. This also enables LQS for all sessions , using Activity Monitor or directly querying sys.dm_exec_query_profiles .

 

Enters SQL Server 2016 SP1
Now with SQL Server 2016 SP1, we are introducing a real lightweight query execution statistics profiling infrastructure, to dramatically reduce performance overhead of collecting per-operator query execution statistics , such as actual number of rows. This feature can be enabled either using global startup trace flag 7412 , or is automatically turned on when query_thread_profile xEvent is enabled. Again, this also enables LQS for all sessions , using Activity Monitor or directly querying sys.dm_exec_query_profiles .


How much lightweight you ask?
We ran a TPC-C like workload in both the legacy and lightweight profiling infrastructures, and this is what we found:



So you can see above that enabling the new lightweight profiling infrastructure (with the trace flag 7412 ) has a max overhead of 2 percent , as opposed to 75 percent before SQL Server 2014 SP2/2016 RTM. This means that now, for any server that is not already resource bound, you can run the lightweight profiling infrastructure continuously, and tap into any running execution at any time using for example Activity Monitor or directly querying sys.dm_exec_query_profiles , and get the query plan with execution statistics .

The possibilities this unlocks for performance troubleshooting in production environments are tremendous, and if your servers are not already resource bound, we encourage you to enable this trace flag for the potential it unlocks. As usual, we recommend you test this in a pre-production environment to estimate your specific impact beforehand.

When the lightweight profiling is enabled, the information in sys.dm_exec_query_profiles (and so LQS) is available via the new profiling infrastructure instead of the legacy profiling infrastructure.


Examples
Here's a few usage examples:

    • Clicking on Include Live Query Statistics in SSMS uses the legacy profiling infrastructure starting with SQL Server 2014 RTM, but with SQL Server 2014 SP2 and SQL Server 2016, doing the same uses an earlier version of the lightweight profiling infrastructure. In SQL Server 2016 SP1, and only by setting TF 7412 or using query_thread_profile xEvent, you can then click on Include Live Query Statistics and use the new lightweight profiling infrastructure. Starting with SQL Server 2016 SP2CU3 and SQL Server 2017 CU11, the new query hint query_plan_profile can be used to enable lightweight profiling at the query level, for any session.




    • SET STATISTICS XML ON, SET STATISTICS PROFILE ON and query_post_execution_showplan xEvent always use the legacy profiling infrastructure.

There’s also the new DMF sys.dm_exec_query_statistics_xml , which returns the query execution plan for in-flight requests. For example, you can now use this DMF to get the plan for a query while it is running, and works under both legacy and lightweight profiling infrastructure.

Use this DMV to retrieve the showplan XML with transient statistics, as seen below:





And you can join with other DMVs like sys.dm_exec_requests. The Running-Blocked-Processes script in the TigerToolbox Github is already using this to access the “live_query_plan_snapshot”, among other useful data.

 

In summary
If your SQL Server is not already CPU bound and a 1.5% to 2% overhead is negligible for your workloads, we recommend you enable TF 7412 as a startup trace flag , and unleash the power of live query troubleshooting in production environments.

Pedro Lopes ( @sqlpto ) – Senior Program Manager