SQL优化:设置执行计划的显示格式

来源:互联网 发布:淘宝网结算页面进不去 编辑:程序博客网 时间:2024/06/03 19:32

要优化,就得看懂执行计划,要看懂执行计划,首先要能显示多种格式的执行计划,而不仅仅是看图形的执行计划,因为文本的执行计划更细,能看出更多的问题。


1、设置显示简单格式的执行计划,不执行sql语句
 

set showplan_text on   ----------注意:SET SHOWPLAN 语句必须是批处理中仅有的语句。go declare @t table( t1 int)declare @tt table(t1 int ,t2 varchar(10))insert into @t values(1)insert into @t values(2)insert into @t values(3)insert into @t values(4)insert into @tt values(1,'ab')insert into @tt values(1,'acd')insert into @tt values(2,'ab')insert into @tt values(3,'c')insert into @tt values(2,'a')--set statistics profile onselect *from @t xleft outer join @tt y  on x.t1 = y.t1 and y.t2 like '%c%'go set showplan_text offgo

结果:
  |--Nested Loops(Left Outer Join, WHERE:(@t.[t1] as [x].[t1]=@tt.[t1] as [y].[t1]))
       |--Table Scan(OBJECT:(@t AS [x]))
       |--Table Scan(OBJECT:(@tt AS [y]), WHERE:(@tt.[t2] as [y].[t2] like '%c%'))


 
2、设置显示全面的执行计划,但不执行sql语句(不执行这个执行计划)

set showplan_all ongo-----再次运行上面的sql语句set showplan_all offgo 

结果:除了执行计划,还包括:PhysicalOp、LogicalOp、EsitimateRows、EstimateIO、EstimateCPU、
AvgRowSize、TotalSubstreeCost、Parallel、EstimateExecutions等字段,都是显示了(根据统计信息、表的结构)预估的值。
 
3、设置显示全面的执行计划,外加显示计划执行后的实际信息

set statistics profile on-------再次运行上面的sql语句set statistics profile off   ----这个会关闭设置,如果不关闭,会导致之后的执行计划还是按照先前的设置显示出来。go

结果:

除执行计划、PhysicalOp、LogicalOp、EsitimateRows、EstimateIO、EstimateCPU、
AvgRowSize、TotalSubstreeCost、Parallel、EstimateExecutions等字段,还有Rows(实际返回行数)、Executions(操作实际执行的次数)字段

 
4、设置显示XML格式的执行计划。

 

set showplan_xml ongo

0 0
原创粉丝点击