Sys DM Exec Text Query Plan
Sys DM Exec Text Query Plan
Sys DM Exec Text Query Plan
dm_exec_text_query_planacceptsstatement_start_offsetand
statement_end_offsettogettheplanoftheexactstatementrunning.TheotherbenefitfromthisDMVistouse
thisoutputinUSEPLANqueryhintorusingPLANGUIDE.
Todemonstratemoreonsys.dm_exec_text_query_planIwillmodifytheprocedureIusedinlasttwoblogs.Iwill
beaddinganotherstatementtotheprocedure.
1
2
3
4
5
6
7
8
USEAdventureWorks2012
GO
CREATEPROCEDUREusp_getPerson_prc
AS
BEGIN
SELECT*FROMperson.person
SELECT*FROMperson.Address
END
Nowexecutetheabovestoredprocedure.Whiletheabovestoredprocedureisrunningandifyouarefast
enoughyoucanrunthebelowquerytwiceduringexecution.Youwillfindthebelowoutputfortwodifferent
statementsduringtheprocedureexecution.
1
2
3
4
5
6
7
8
9
10
11
12
SELECTer.session_id,
SUBSTRING(est.text,statement_start_offset/2+
(((CASEWHENer.statement_end_offset=
THENdatalength(est.text)
ELSEer.statement_end_offsetEND)
AScurrent_stmnt,
textASbatch,
CAST(etqp.query_planASXML)ASstmnt_plan
FROMsys.dm_exec_requestser
CROSSAPPLYsys.dm_exec_sql_text(er.sql_handle)est
CROSSAPPLYsys.dm_exec_text_query_plan(er.plan_handle
WHEREsession_id=54Changethesession_idasneeded
Firststatement:
Secondstatement:
Theexecutionplansforindividualstatementsinabatchcanbecollectedlikethisusing
sys.dm_exec_text_query_plan.Sostartusingittoday.