zoukankan      html  css  js  c++  java
  • SQL优化:设置执行计划的显示格式

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


    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
  • 相关阅读:
    ado异常代码含义对照表及SQL Access,oracle 数据类型对照表
    关于同花顺日数据格式
    把自己以前的QQ plan贴一下
    为应用程序制作帮助文件
    [临时]单源最短路径(Dijkstra算法)
    [IDA] 分析for循环的汇编代码
    对 strlen 汇编代码的解释
    [VC6] 在对话框上实现LOGO图片的渐变性切换效果
    [C++]拼图游戏
    使用 ADO 向数据库中存储一张图片
  • 原文地址:https://www.cnblogs.com/momogua/p/8304397.html
Copyright © 2011-2022 走看看