Home
%3CLINGO-SUB%20id%3D%22lingo-sub-400934%22%20slang%3D%22en-US%22%3ERe%3A%20What%20if%20the%20Actual%20Execution%20Plan%20was%20always%20available%20for%20any%20query%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-400934%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F295040%22%20target%3D%22_blank%22%3E%40LeoHu%3C%2FA%3E%20it%20collects%20stats%20based%20on%20lightweight%20profiling%20(link%20to%20docs%20on%20this%20feature%20are%20in%20the%20post).%20That%20said%2C%20it%20is%20an%20opt-in%20feature%20under%20preview%20precisely%20because%20there's%20only%20so%20much%20we%20can%20measure%20in%20our%20labs%2C%20and%20we%20count%20on%20you%20to%20help%20us%20determine%20if%20the%20expected%20low%20cost%20is%20true%20for%20different%20scenarios.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F314973%22%20target%3D%22_blank%22%3E%40jwms-1%3C%2FA%3E%20You%20enable%20the%20feature%20via%20trace%20flag%20(in%20the%20post)%20and%20then%20just%20access%20it%20with%20the%20new%20DMF.%20SSMS%20is%20then%20able%20to%20render%20the%20XML%20into%20a%20graphical%20plan.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-400918%22%20slang%3D%22en-US%22%3ERe%3A%20What%20if%20the%20Actual%20Execution%20Plan%20was%20always%20available%20for%20any%20query%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-400918%22%20slang%3D%22en-US%22%3E%3CP%3ESo%20this%20is%20a%20preference%20under%20Tools%2FOptions%3F%26nbsp%3B%20That%20would%20be%20cool%20if%20it%20was%20a%20permanent%20option%20for%20SSMS%2C%20though%20I%20too%20wonder%20about%20the%20performance%20impact%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-395506%22%20slang%3D%22en-US%22%3ERe%3A%20What%20if%20the%20Actual%20Execution%20Plan%20was%20always%20available%20for%20any%20query%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-395506%22%20slang%3D%22en-US%22%3E%3CP%3EGreat%20feature!%3C%2FP%3E%3CP%3EWhat's%20the%20performance%20impact%20for%20enabling%20it%3F%20Does%20it%20collect%20performance%20stats%20synchronously%20at%20run%20time%3F%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-394427%22%20slang%3D%22en-US%22%3ERe%3A%20What%20if%20the%20Actual%20Execution%20Plan%20was%20always%20available%20for%20any%20query%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-394427%22%20slang%3D%22en-US%22%3E%3CP%3EAmazing!%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-393387%22%20slang%3D%22en-US%22%3EWhat%20if%20the%20Actual%20Execution%20Plan%20was%20always%20available%20for%20any%20query%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-393387%22%20slang%3D%22en-US%22%3E%3CP%3EWell%2C%20w%3CFONT%20style%3D%22background-color%3A%20%23ffffff%3B%22%3Ehat%20if%20the%20Actual%20Execution%20Plan%20was%20always%20available%20for%20any%20query%3F%3C%2FFONT%3E%20The%20good%20news%20is%3A%20it%20can%20be!%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIn%20SQL%20Server%202019%20CTP%202.4%2C%20there%E2%80%99s%20a%20new%20DMF%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fsql%2Frelational-databases%2Fsystem-dynamic-management-views%2Fsys-dm-exec-query-plan-stats-transact-sql%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3Esys.dm_exec_query_plan_stats%3C%2FA%3E%20that%20allows%20you%20to%20always%20access%20the%20last%20actual%20execution%20plan%20for%20any%20query.%20But%20why%20do%20you%20want%20that%3F%20This%20is%20what%20this%20post%20is%20about.%3C%2FP%3E%0A%3CP%3EQuery%20execution%20plans%2C%20otherwise%20known%20as%20actual%20execution%20plans%20or%20just%20Showplan%2C%20are%20a%20treasure%20trove%20of%20information%20about%20how%20SQL%20Server%20executed%20the%20required%20operations%20to%20get%20the%20query%20output%2C%20which%20includes%20runtime%20performance%20statistics.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CFONT%20size%3D%225%22%3E%3CSTRONG%3EScenario%3C%2FSTRONG%3E%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3EThink%20of%20a%20scenario%20where%20a%20query%20has%20just%20turned%20bad%20(as%20in%20its%20performance%20tanked)%2C%20and%20you%20got%20a%20call%20informing%20the%20application%20has%20poor%20performance%20as%20a%20result.%20If%20it%E2%80%99s%20a%20long%20running%20query%20then%20you%20can%20use%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fsql%2Frelational-databases%2Fperformance%2Flive-query-statistics%23to-view-live-query-statistics-for-any-query%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3ELive%20Query%20Statistics%3C%2FA%3E%2C%20but%20if%20it%E2%80%99s%20something%20that%E2%80%99s%20been%20executing%20repeatedly%20(and%20poorly)%20in%20the%20last%20few%20minutes%2C%20then%20accessing%20the%20%3CSTRONG%3E*actual%20execution%20plan*%3C%2FSTRONG%3E%20allows%20you%20to%20troubleshoot%20that%20query%20immediately%20without%20starting%20any%20tracing.%20Note%3A%20I%E2%80%99m%20talking%20xEvents%2C%20no%20one%20should%20be%20using%20Profiler%20anymore%20%3Asmiling_face_with_smiling_eyes%3A%3C%2Fimg%3E.%3C%2FP%3E%0A%3CP%3EOf%20course%2C%20query%20plans%20are%20available%20through%20the%20Query%20Store%20(QS).%20However%2C%20QS%20aggregates%20performance%20data%20in%20configurable%20time%20periods%2C%20which%20is%20excellent%20to%20do%20performance%20troubleshooting%20over%20time%20(think%20time%20series)%2C%20and%20analysis%20of%20workload%20trends%20(you%20can%20see%20more%20in%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fsql%2Frelational-databases%2Fperformance%2Fmonitoring-performance-by-using-the-query-store%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3EMonitoring%20performance%20by%20using%20the%20Query%20Store%3C%2FA%3E).%3C%2FP%3E%0A%3CP%3EFor%20our%20current%20scenario%20we%20need%20to%20get%20the%20specific%20occurrence%20of%20the%20query%E2%80%99s%20execution%20plan%20to%20analyze%20and%20troubleshoot.%20Besides%20the%20xEvents%20we%20%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2FSQL-Server%2FUsing-xEvents-to-capture-an-Actual-Execution-Plan%2Fba-p%2F392136%22%20target%3D%22_blank%22%20rel%3D%22noopener%22%3Ediscussed%20before%20in%20this%20post%3C%2FA%3E%20which%20would%20require%20me%20to%20start%20a%20trace%2C%20I%20now%20have%20this%20new%20DMF%20to%20get%20the%20plan.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CFONT%20size%3D%225%22%3E%3CSTRONG%3EHow%20does%20it%20work%3F%3C%2FSTRONG%3E%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3EFirst%2C%20this%20DMF%20will%20only%20output%20information%20if%20trace%20flag%202451%20is%20enabled%20or%20the%20n%3CFONT%20style%3D%22background-color%3A%20%23ffffff%3B%22%3Eew%20LAST_QUERY_PLAN_STATS%20database%20scoped%20configuration%20(just%20for%20CTP%202.5)%3C%2FFONT%3E.%20If%20it%20is%2C%20then%20SQL%20Server%20leverages%20the%20lightweight%20query%20execution%20statistics%20profile%20infrastructure%20%E2%80%93%20or%20lightweight%20profiling%20for%20short%20(read%20more%20about%20it%20here).%3C%2FP%3E%0A%3CP%3EYou%20can%20enable%20trace%20flag%202451%20globally%20even%20without%20restarting%20SQL%20Server%20using%20this%20command%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EDBCC%20TRACEON(2451%2C%20-1)%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EWith%20that%2C%20let%E2%80%99s%20see%20an%20example.%20My%20workload%20is%20running%20now%2C%20and%20I%20can%20use%20the%20new%20DMF%20together%20with%20other%20familiar%20DMVs%2FDMFs%20to%20quickly%20search%20for%20the%20last%20actual%20execution%20plan%20on%20queries%20that%20are%20currently%20executing.%20The%20point%20of%20entry%20here%20are%20the%20in-flight%20requests%20from%20%3CSPAN%20style%3D%22text-align%3A%20left%3B%20color%3A%20%23333333%3B%20text-transform%3A%20none%3B%20line-height%3A%201.7142%3B%20text-indent%3A%200px%3B%20letter-spacing%3A%20normal%3B%20font-family%3A%20inherit%3B%20font-size%3A%2016px%3B%20font-style%3A%20normal%3B%20font-variant%3A%20normal%3B%20font-weight%3A%20300%3B%20text-decoration%3A%20none%3B%20word-spacing%3A%200px%3B%20float%3A%20none%3B%20display%3A%20inline%20!important%3B%20white-space%3A%20normal%3B%20cursor%3A%20text%3B%20orphans%3A%202%3B%20background-color%3A%20%23ffffff%3B%20-webkit-text-stroke-width%3A%200px%3B%22%3Esys.dm_exec_requests%3C%2FSPAN%3E%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ESELECT%20er.session_id%2C%20er.start_time%2C%20er.status%2C%20er.command%2C%20st.text%2C%20%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20qp.query_plan%20AS%20cached_plan%2C%20qps.query_plan%20AS%20last_actual_exec_plan%3CBR%20%2F%3EFROM%20sys.dm_exec_requests%20AS%20er%3CBR%20%2F%3EOUTER%20APPLY%20sys.dm_exec_query_plan(er.plan_handle)%20qp%20%3CBR%20%2F%3EOUTER%20APPLY%20sys.dm_exec_sql_text(er.sql_handle)%20st%3CBR%20%2F%3EOUTER%20APPLY%20sys.dm_exec_query_plan_stats(er.plan_handle)%20qps%3CBR%20%2F%3EWHERE%20session_id%20%26gt%3B%2050%20AND%20status%20IN%20('running'%2C%20'suspended')%3B%3CBR%20%2F%3EGO%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EWhich%20yields%20the%20following%20result%3A%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-center%22%20style%3D%22width%3A%20980px%3B%22%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20980px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F101452iE51368964BF09BD4%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%22image001.jpg%22%20title%3D%22image001.jpg%22%20%2F%3E%3CSPAN%20class%3D%22lia-inline-image-caption%22%20onclick%3D%22event.preventDefault()%3B%22%3Esys.dm_exec_query_plan_stats%20from%20in-flight%20requests%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FSPAN%3EFrom%20where%20I%20can%20click%20any%20of%20the%20links%20in%20the%20%3CSTRONG%3E%3CEM%3Elast_actual_exec_plan%3C%2FEM%3E%3C%2FSTRONG%3E%20column%2C%20and%20see%20the%20actual%20plan%3A%3C%2FP%3E%0A%3CP%3Elast%20actual%20executed%20planOr%20for%20queries%20that%20are%20not%20currently%20executing%2C%20but%26nbsp%3Bhave%20executed%20before%2C%20where%20the%20point%20of%20entry%20may%20be%20the%20cached%20plans%20from%20sys.dm_exec_cached_plans%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ESELECT%20DB_NAME(qp.dbid)%2C%20cacheobjtype%2C%20objtype%2C%20%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20qp.query_plan%20AS%20cached_plan%2C%20qps.query_plan%20AS%20last_actual_exec_plan%3CBR%20%2F%3EFROM%20sys.dm_exec_cached_plans%20AS%20cp%3CBR%20%2F%3EOUTER%20APPLY%20sys.dm_exec_query_plan(cp.plan_handle)%20qp%20%3CBR%20%2F%3EOUTER%20APPLY%20sys.dm_exec_query_plan_stats(cp.plan_handle)%20qps%3B%3CBR%20%2F%3EGO%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EWhich%20yields%20the%20following%20result%3A%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-center%22%20style%3D%22width%3A%20679px%3B%22%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20679px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F101454iE3E34D3546FA9560%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%22image003.jpg%22%20title%3D%22image003.jpg%22%20%2F%3E%3CSPAN%20class%3D%22lia-inline-image-caption%22%20onclick%3D%22event.preventDefault()%3B%22%3Esys.dm_exec_query_plan_stats%20from%20cached%20plans%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FSPAN%3EAnd%20similarly%2C%20I%20can%20click%20any%20of%20the%20links%20in%20the%20%3CSTRONG%3E%3CEM%3Elast_actual_exec_plan%3C%2FEM%3E%3C%2FSTRONG%3E%20column%2C%20and%20see%20the%20actual%20plan.%3C%2FP%3E%0A%3CP%3EFrom%20there%2C%20I%20can%20start%20troubleshooting%20my%20query%20based%20on%20actual%20runtime%20statistics.%20Looking%20at%20the%20actual%20execution%20plan%20for%20something%20that%20has%20already%20executed%2C%20I%20have%20actionable%20insight%3A%20I%20can%20create%20a%20narrow%20index%20on%20Sales.SalesOrderDetail%2C%20or%20even%20think%20about%20creating%20an%20indexed%20view%20on%20my%20query%20that%20includes%20the%20complex%20expression%20highlighted%20below.%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-center%22%20style%3D%22width%3A%20725px%3B%22%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20725px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F101455i81A61ED987E83D83%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%22image004.png%22%20title%3D%22image004.png%22%20%2F%3E%3CSPAN%20class%3D%22lia-inline-image-caption%22%20onclick%3D%22event.preventDefault()%3B%22%3EComplex%20expression%20in%20query%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FSPAN%3EIn%20the%20%3CA%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%20noopener%20noreferrer%22%3ETiger%20Toolbox%3C%2FA%3E%20you%20can%20see%20a%20much%20more%20comprehensive%20script%20to%20report%20useful%20information%20on%20in-flight%20requests%20that%20uses%20this%20and%20other%20newer%20DMVs%2FDMFs%2C%20as%20well%20as%20stored%20procedures%2Ftriggers%2Ffunctions%2Fquery%20stats.%20Also%20available%20in%20Jupyter%20Notebook%20format%20for%20use%20with%20Azure%20Data%20Studio.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CFONT%20size%3D%225%22%3E%3CSTRONG%3ENotes%3C%2FSTRONG%3E%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3EIn%20CTP%202.4%20not%20all%20actual%20execution%20plans%20will%20be%20available%2C%20you%20can%20see%20more%20details%20on%20that%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fsql%2Frelational-databases%2Fsystem-dynamic-management-views%2Fsys-dm-exec-query-plan-stats-transact-sql%23remarks%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehere%3C%2FA%3E.%3C%2FP%3E%0A%3CP%3EFor%20CTP%202.5%2C%20mostly%20%3CSTRONG%3Eall%3C%2FSTRONG%3E%20queries%20will%20be%20available%20with%20the%20equivalent%20of%20the%20actual%20execution%20plan.%20At%20least%20those%20where%20the%20plan%20was%20cached%20in%20the%20first%20place%2C%20or%20those%20where%20the%20plan%20has%20%3CSTRONG%3Enot%3C%2FSTRONG%3E%20been%20evicted%20from%20cache.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EPedro%20Lopes%20(%20%3CA%20href%3D%22https%3A%2F%2Ftwitter.com%2FSQLPedro%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%40SQLPedro%20%3C%2FA%3E)%20%E2%80%93%20Senior%20Program%20Manager%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-393387%22%20slang%3D%22en-US%22%3E%3CP%3E%3CSPAN%20style%3D%22text-align%3A%20left%3B%20color%3A%20%23333333%3B%20text-transform%3A%20none%3B%20line-height%3A%201.7142%3B%20text-indent%3A%200px%3B%20letter-spacing%3A%20normal%3B%20font-family%3A%20inherit%3B%20font-size%3A%2016px%3B%20font-style%3A%20normal%3B%20font-variant%3A%20normal%3B%20font-weight%3A%20300%3B%20text-decoration%3A%20none%3B%20word-spacing%3A%200px%3B%20float%3A%20none%3B%20display%3A%20inline%20!important%3B%20white-space%3A%20normal%3B%20cursor%3A%20text%3B%20orphans%3A%202%3B%20background-color%3A%20%23ffffff%3B%20-webkit-text-stroke-width%3A%200px%3B%22%3EIn%20SQL%20Server%202019%20CTP%202.4%2C%20there%E2%80%99s%20a%20new%20DMF%20%3C%2FSPAN%3E%3CA%20style%3D%22text-align%3A%20left%3B%20color%3A%20%23146cac%3B%20text-transform%3A%20none%3B%20text-indent%3A%200px%3B%20letter-spacing%3A%20normal%3B%20font-size%3A%2016px%3B%20font-style%3A%20normal%3B%20font-variant%3A%20normal%3B%20font-weight%3A%20300%3B%20text-decoration%3A%20underline%3B%20word-spacing%3A%200px%3B%20white-space%3A%20normal%3B%20box-sizing%3A%20border-box%3B%20orphans%3A%202%3B%20background-color%3A%20transparent%3B%20-webkit-text-stroke-width%3A%200px%3B%22%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fsql%2Frelational-databases%2Fsystem-dynamic-management-views%2Fsys-dm-exec-query-plan-stats-transact-sql%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%20target%3D%22_blank%22%3Esys.dm_exec_query_plan_stats%3C%2FA%3E%3CSPAN%20style%3D%22text-align%3A%20left%3B%20color%3A%20%23333333%3B%20text-transform%3A%20none%3B%20line-height%3A%201.7142%3B%20text-indent%3A%200px%3B%20letter-spacing%3A%20normal%3B%20font-family%3A%20inherit%3B%20font-size%3A%2016px%3B%20font-style%3A%20normal%3B%20font-variant%3A%20normal%3B%20font-weight%3A%20300%3B%20text-decoration%3A%20none%3B%20word-spacing%3A%200px%3B%20float%3A%20none%3B%20display%3A%20inline%20!important%3B%20white-space%3A%20normal%3B%20cursor%3A%20text%3B%20orphans%3A%202%3B%20background-color%3A%20%23ffffff%3B%20-webkit-text-stroke-width%3A%200px%3B%22%3E%20that%20allows%20you%20to%20always%20access%20the%20%3CSTRONG%3Elast%20actual%20execution%20plan%20for%20any%20query%3C%2FSTRONG%3E.%20But%20why%20do%20you%20want%20that%3F%20This%20is%20what%20this%20post%20is%20about.%3C%2FSPAN%3E%3C%2FP%3E%3C%2FLINGO-TEASER%3E%3CLINGO-LABS%20id%3D%22lingo-labs-393387%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3ESQLServerTiger%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Microsoft

Well, what if the Actual Execution Plan was always available for any query? The good news is: it can be!

 

In SQL Server 2019 CTP 2.4, there’s a new DMF sys.dm_exec_query_plan_stats that allows you to always access the last actual execution plan for any query. But why do you want that? This is what this post is about.

Query execution plans, otherwise known as actual execution plans or just Showplan, are a treasure trove of information about how SQL Server executed the required operations to get the query output, which includes runtime performance statistics.

 

Scenario

Think of a scenario where a query has just turned bad (as in its performance tanked), and you got a call informing the application has poor performance as a result. If it’s a long running query then you can use Live Query Statistics, but if it’s something that’s been executing repeatedly (and poorly) in the last few minutes, then accessing the *actual execution plan* allows you to troubleshoot that query immediately without starting any tracing. Note: I’m talking xEvents, no one should be using Profiler anymore :smiling_face_with_smiling_eyes:.

Of course, query plans are available through the Query Store (QS). However, QS aggregates performance data in configurable time periods, which is excellent to do performance troubleshooting over time (think time series), and analysis of workload trends (you can see more in Monitoring performance by using the Query Store).

For our current scenario we need to get the specific occurrence of the query’s execution plan to analyze and troubleshoot. Besides the xEvents we discussed before in this post which would require me to start a trace, I now have this new DMF to get the plan.

 

How does it work?

First, this DMF will only output information if trace flag 2451 is enabled or the new LAST_QUERY_PLAN_STATS database scoped configuration (just for CTP 2.5). If it is, then SQL Server leverages the lightweight query execution statistics profile infrastructure – or lightweight profiling for short (read more about it here).

You can enable trace flag 2451 globally even without restarting SQL Server using this command:

 

DBCC TRACEON(2451, -1);

 

With that, let’s see an example. My workload is running now, and I can use the new DMF together with other familiar DMVs/DMFs to quickly search for the last actual execution plan on queries that are currently executing. The point of entry here are the in-flight requests from sys.dm_exec_requests:

 

SELECT er.session_id, er.start_time, er.status, er.command, st.text,
            qp.query_plan AS cached_plan, qps.query_plan AS last_actual_exec_plan
FROM sys.dm_exec_requests AS er
OUTER APPLY sys.dm_exec_query_plan(er.plan_handle) qp
OUTER APPLY sys.dm_exec_sql_text(er.sql_handle) st
OUTER APPLY sys.dm_exec_query_plan_stats(er.plan_handle) qps
WHERE session_id > 50 AND status IN ('running', 'suspended');
GO

 

Which yields the following result:

 

image001.jpgsys.dm_exec_query_plan_stats from in-flight requestsFrom where I can click any of the links in the last_actual_exec_plan column, and see the actual plan:

image002.pnglast actual executed planOr for queries that are not currently executing, but have executed before, where the point of entry may be the cached plans from sys.dm_exec_cached_plans:

 

SELECT DB_NAME(qp.dbid), cacheobjtype, objtype,
            qp.query_plan AS cached_plan, qps.query_plan AS last_actual_exec_plan
FROM sys.dm_exec_cached_plans AS cp
OUTER APPLY sys.dm_exec_query_plan(cp.plan_handle) qp
OUTER APPLY sys.dm_exec_query_plan_stats(cp.plan_handle) qps;
GO

 

Which yields the following result:

 

image003.jpgsys.dm_exec_query_plan_stats from cached plansAnd similarly, I can click any of the links in the last_actual_exec_plan column, and see the actual plan.

From there, I can start troubleshooting my query based on actual runtime statistics. Looking at the actual execution plan for something that has already executed, I have actionable insight: I can create a narrow index on Sales.SalesOrderDetail, or even think about creating an indexed view on my query that includes the complex expression highlighted below.

 

image004.pngComplex expression in queryIn the Tiger Toolbox you can see a much more comprehensive script to report useful information on in-flight requests that uses this and other newer DMVs/DMFs, as well as stored procedures/triggers/functions/query stats. Also available in Jupyter Notebook format for use with Azure Data Studio.

 

Notes

In CTP 2.4 not all actual execution plans will be available, you can see more details on that here.

For CTP 2.5, mostly all queries will be available with the equivalent of the actual execution plan. At least those where the plan was cached in the first place, or those where the plan has not been evicted from cache.

 

Pedro Lopes ( @SQLPedro ) – Senior Program Manager

4 Comments
Frequent Visitor

Amazing!

Occasional Visitor

Great feature!

What's the performance impact for enabling it? Does it collect performance stats synchronously at run time?

Occasional Visitor

So this is a preference under Tools/Options?  That would be cool if it was a permanent option for SSMS, though I too wonder about the performance impact

Microsoft

@LeoHu it collects stats based on lightweight profiling (link to docs on this feature are in the post). That said, it is an opt-in feature under preview precisely because there's only so much we can measure in our labs, and we count on you to help us determine if the expected low cost is true for different scenarios.

 

@jwms-1 You enable the feature via trace flag (in the post) and then just access it with the new DMF. SSMS is then able to render the XML into a graphical plan.