现在的位置: 首页 > 综合 > 正文

SQL Server 设置执行计划的显示格式

2014年07月28日 ⁄ 综合 ⁄ 共 1253字 ⁄ 字号 评论关闭

 

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 on

select *
from @t x
left outer join @tt y
  on x.t1 = y.t1 and y.t2 like '%c%'

go

 

set showplan_text off

go

 

结果:

  |--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 on

go

-----再次运行上面的sql语句

set showplan_all off

go

 

结果:除了执行计划,还包括: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 on

go

抱歉!评论已关闭.