• 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
  • 相关阅读:
    jmeter元件的执行顺序
    jmeter json path espressions学习
    性能测试常见瓶颈分析及调优方法
    jmeter 聚合报告参数解释
    Jmeter 使用正则表达式提取响应结果中的值
    前端所需要了解的东西
    webpack4导入jQuery的新方案
    react服务端渲染SSR实战
    使用await写异步优化代码
    Provider
  • 原文地址:https://www.cnblogs.com/momogua/p/8304397.html
Copyright © 2020-2023  润新知