SET Statements for SQLServer

SET
SHOWPLAN_ALL { ON | OFF }

  • It will not
    execute the TSQL statements.

  • It cannot
    be specified inside a stored procedure,must be the only statements in a
    batch.

  • Returns
    information as a set of rows that form a hierarchical tree representing the
    steps taken by the SQL Server query processor as it executes each statement.
    Each statement reflected in the output contains a single row with the text of
    the statement, followed by several rows with the details of the execution
    steps.

SET
SHOWPLAN_ALL ON;
GO
SELECT
BusinessEntityID, JobTitle
FROM
HumanResources.Employee
WHERE
JobTitle LIKE ‘Production%‘;
GO
SET
SHOWPLAN_ALL OFF;
GO

All
the information in the resultset are estimated, no any actual statistics
information.

SET
SHOWPLAN_XML { ON | OFF }

  • It will not
    execute the TSQL statements.

  • It will
    return detailed information about how the statements are going to be executed
    in XML.

SET
SHOWPLAN_XML ON;
GO
SELECT
BusinessEntityID, JobTitle
FROM
HumanResources.Employee
WHERE
JobTitle LIKE ‘Production%‘;
GO
SET
SHOWPLAN_XML OFF;
GO

SET
SHOWPLAN_TEXT { ON | OFF }

  • It will not
    execute the TSQL statements.

  • It cannot be
    specified inside a stored procedure,must be the only statements in a
    batch.

  • Returns
    information as a set of rows that form a hierarchical tree representing the
    steps taken by the SQL Server query processor as it executes each statement.
    Each statement reflected in the output
    contains a single row with the text of the statement, followed by
    several rows with the details of the execution steps. The table shows the
    column that the output contains.

  • SET
    SHOWPLAN_TEXT is intended to return readable output for Microsoft Win32
    command prompt applications such as
    the osql utility. SET SHOWPLAN_ALL
    returns more detailed output intended to be used with programs designed to
    handle its output.

SET
SHOWPLAN_TEXT ON;
GO
SELECT
BusinessEntityID, JobTitle
FROM
HumanResources.Employee
WHERE
JobTitle LIKE ‘Production%‘;
GO
SET
SHOWPLAN_TEXT OFF;
GO

SET
STATISTICS PROFILE { ON | OFF }

  • It
    will execute the TSQL statements.

  • Returns
    information as a set of rows that form a hierarchical tree representing the
    steps taken by the SQL Server query processor as it executes each statement.
    Each statement reflected in the output contains a single row with the text of
    the statement, followed by several rows with the details of the execution
    steps.

  • While
    compare with SHOWPLAN_ALL, it has two additional column
    Rows,Executes.

SET
statistics profile ON;
GO
SELECT
BusinessEntityID, JobTitle
FROM
HumanResources.Employee
WHERE
JobTitle LIKE ‘Production%‘;
GO
SET
statistics profile OFF;
GO


SET
STATISTICS XML { ON | OFF }

  • It will
    execute the TSQL statements.

  • It will
    generate detailed information about how the statements were executed in
    XML.

  • SET
    STATISTICS XML need not be the only statement in a batch.

  • SET
    STATISTICS XML returns output as nvarchar(max) for applications, such as
    the sqlcmd utility, where the XML output is
    subsequently used by other tools to display and process the query plan
    information. The xml comply with below xsd file \Microsoft SQL Server\100\Tools\Binn\schemas\sqlserver\2004\07\showplan\showplanxml.xsd

  • SET
    STATISTICS PROFILE and SET STATISTICS XML are counterparts of each other. The
    former produces textual output; the latter produces XML output. In
    future versions of SQL Server, new query execution plan information will only
    be displayed through the SET STATISTICS XML statement, not the SET STATISTICS
    PROFILE statement.

  • If Include Actual Execution Plan is selected in SQL
    Server Management Studio, this SET option does not produce XML Showplan
    output. Clear the Include Actual Execution
    Plan button before using this SET
    option.

SET
STATISTICS XML ON;
GO
SELECT
BusinessEntityID, JobTitle
FROM
HumanResources.Employee
WHERE
JobTitle LIKE ‘Production%‘;
GO
SET
STATISTICS XML OFF;
GO

SET
STATISTICS TIME { ON | OFF }

  • When SET
    STATISTICS TIME is ON, the time statistics for a statement are displayed. When
    OFF, the time statistics are not displayed.

  • The setting of SET
    STATISTICS TIME is set at execute or run time and not at parse
    time.

  • Microsoft SQL
    Server is unable to provide accurate statistics in fiber mode, which is
    activated when you enable the lightweight
    pooling
     configuration option.

  • The cpu column
    in the sysprocesses table is only updated when a
    query executes with SET STATISTICS TIME ON. When SET STATISTICS TIME is
    OFF, 0 is returned.

  • ON and OFF
    settings also affect the CPU column in the Process Info View for Current
    Activity in SQL Server Management Studio.

SET
STATISTICS TIME ON;
GO
SELECT
BusinessEntityID, JobTitle
FROM
HumanResources.Employee
WHERE
JobTitle LIKE ‘Production%‘;
GO
SET
STATISTICS TIME OFF;
GO

SET
STATISTICS IO { ON | OFF }

  • When
    STATISTICS IO is ON, statistical information is displayed. When OFF, the
    information is not displayed.

  • After this
    option is set ON, all subsequent Transact-SQL statements return the
    statistical information until the option is set to OFF.

  • When
    Transact-SQL statements retrieve LOB columns, some LOB retrieval operations
    might require traversing the LOB tree multiple times. This may cause SET
    STATISTICS IO to report higher than expected logical reads.





























  • Output
    item

    Meaning

    Table

    Name
    of the table.

    Scan
    count

    Number
    of seeks/scans started after reaching the leaf level in any direction to
    retrieve all the values to construct the final dataset for the
    output.

    • Scan
      count is 0 if the index used is a unique index or clustered index on a
      primary key and you are seeking for only one value. For
      example WHERE Primary_Key_Column =
      <value>.

    • Scant
      count is 1 when you are searching for one value using a non-unique
      clustered index which is defined on a non-primary key column. This is
      done to check for duplicate values for the key value that you are
      searching for. For example WHERE
      Clustered_Index_Key_Column = <value>.

    • Scan
      count is N when N is the number of different seek/scan started towards
      the left or right side at the leaf level after locating a key value
      using the index key.

    logical
    reads

    Number
    of pages read from the data cache.

    physical
    reads

    Number
    of pages read from disk.

    read-ahead
    reads

    Number
    of pages placed into the cache for the query.

    lob
    logical reads

    Number
    of text, ntext, image, or
    large value type (varchar(max), nvarchar(max), varbinary(max)) pages read from the data
    cache.

    lob
    physical reads

    Number
    of text, ntext, image or large value type pages read from
    disk.

    lob
    read-ahead reads

    Number
    of text, ntext, image or large value type pages placed into
    the cache for the query.

SET
STATISTICS IO ON;
GO
SELECT
BusinessEntityID, JobTitle
FROM
HumanResources.Employee
WHERE
JobTitle LIKE ‘Production%‘;
GO
SET
STATISTICS IO OFF;
GO

SET
FORCEPLAN { ON | OFF }

  • When
    FORCEPLAN is set to ON, the SQL Server query optimizer processes a join in the
    same order as the tables appear in the FROM clause of a query.
    In addition, setting FORCEPLAN to ON forces the use of a nested loop join
    unless other types of joins are required to construct a plan for the query, or
    they are requested with join hints or query hints.

  • SET
    FORCEPLAN essentially overrides the logic used by the query optimizer to
    process a Transact-SQL SELECT statement. The data returned by the SELECT
    statement is the same regardless of this setting. The only difference is the
    way in which SQL Server processes the tables to satisfy the query.Query
    optimizer hints can also be used in queries to affect how SQL Server processes
    the SELECT statement.
USE AdventureWorks2012;
GO
-- Make sure FORCEPLAN is set to OFF.
SET SHOWPLAN_TEXT OFF;
GO
SET FORCEPLAN OFF;
GO
SET SHOWPLAN_TEXT ON;
GO
-- Example where the query plan is not forced.
SELECT p.LastName, p.FirstName, v.Name
FROM Person.Person AS p
INNER JOIN HumanResources.Employee AS e
ON e.BusinessEntityID = p.BusinessEntityID
INNER JOIN Purchasing.PurchaseOrderHeader AS poh
ON e.BusinessEntityID = poh.EmployeeID
INNER JOIN Purchasing.Vendor AS v
ON poh.VendorID = v.BusinessEntityID;
GO
-- SET FORCEPLAN to ON.
SET SHOWPLAN_TEXT OFF;
GO
SET FORCEPLAN ON;
GO
SET SHOWPLAN_TEXT ON;
GO
-- Reexecute inner join to see the effect of SET FORCEPLAN ON.
SELECT p.LastName, p.FirstName, v.Name
FROM Person.Person AS p
INNER JOIN HumanResources.Employee AS e
ON e.BusinessEntityID = p.BusinessEntityID
INNER JOIN Purchasing.PurchaseOrderHeader AS poh
ON e.BusinessEntityID = poh.EmployeeID
INNER JOIN Purchasing.Vendor AS v
ON poh.VendorID = v.BusinessEntityID;
GO
SET SHOWPLAN_TEXT OFF;
GO
SET FORCEPLAN OFF;
GO

DBCC
DROPCLEANBUFFERS 清除数据缓存
DBCC FREEPROCCACHE  清除执行计划缓存

REFERENCES

SET
SHOWPLAN_XML
http://technet.microsoft.com/en-us/library/ms187757.aspx

SET
SHOWPLAN_TEXT

http://technet.microsoft.com/en-us/library/ms176058.aspx

SET
SHOWPLAN_ALL

http://technet.microsoft.com/en-us/library/ms187735.aspx

SET
STATISTICS PROFILE

http://technet.microsoft.com/en-us/library/ms188752.aspx

SET
STATISTICS XML
http://technet.microsoft.com/en-us/library/ms176107.aspx

SET
STATISTICS TIME

http://technet.microsoft.com/en-us/library/ms190287.aspx

SET
STATISTICS IO

http://technet.microsoft.com/en-us/library/ms184361.aspx

SET
FORCEPLAN

http://technet.microsoft.com/en-us/library/ms188344.aspx

SET
Statements (Transact-SQL)

http://msdn.microsoft.com/en-us/library/ms190356.aspx

SET Statements for SQLServer,码迷,mamicode.com

时间: 2024-10-10 05:56:38

SET Statements for SQLServer的相关文章

【转】IBatis.Net项目数据库SqlServer迁移至Oracle

转自:http://www.2cto.com/database/201312/265514.html 最近完成了一个(IBatis.Net+MVC)项目的数据库+代码迁移工作,可把我折腾得~~~ IBatis.Net是一个ORM框架,具体介绍可以问度娘.我之前没用ORM框架使用经验,所以这一路我不是走来的,而是爬出一个坑又掉入另外一个坑~~~ 项目原来用的是Sqlserver2008,现在要转到Oracle,所以我先完成数据迁移,然后是代码迁移. 数据库迁移 1.数据库安装与配置 略过. 2.表

Sql Server之旅——第十一站 简单说说sqlserver的执行计划

原文:Sql Server之旅--第十一站 简单说说sqlserver的执行计划 我们知道sql在底层的执行给我们上层人员开了一个窗口,那就是执行计划,有了执行计划之后,我们就清楚了那些烂sql是怎么执行的,这样 就可以方便的找到sql的缺陷和优化点. 一:执行计划生成过程 说到执行计划,首先要知道的是执行计划大概生成的过程,这样就可以做到就心中有数了,下面我画下简图: 1. 分析过程 这三个比较容易理解,首先我们要保证sql的语法不能错误,select和join的表是必须存在的,以及你是有执行

解剖SQLSERVER 第十五篇 SQLSERVER存储过程的源文本存放在哪里?(译)

解剖SQLSERVER 第十五篇  SQLSERVER存储过程的源文本存放在哪里?(译) http://improve.dk/where-does-sql-server-store-the-source-for-stored-procedures/ 目前我正在扩展OrcaMDF Studio的功能 不单只支持系统表,DMVs 和用户表 而且也要支持存储过程.那很容易,我们只需要查询sys.procedures --或者查询sys.sysschobjs, 因为当SQLSERVER没有在运行的时候我

sqlserver 触发器实例

实例1:update USE [数据库名称]GO/****** Object: Trigger [dbo].[触发器名称] Script Date: 05/08/2014 12:40:25 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOALTER TRIGGER [dbo].[T_触发器名称] ON [dbo].[表名称] FOR UPDATEAS DECLARE @Id VARCHAR(32), @remain_cost DECIMAL

sqlserver数据库备份方法

须事先准备一个工具curl,把它放在c盘.然后,在数据库所在服务器安装7z.最后把这2个存储过程执行,在sqlserver的代理中新建作业,即可实现备份操作. --备份指定数据库到本地和远程指定位置(7-zip压缩) Create PROCEDURE [dbo].[BackUpDB7z] @dbname sysname --数据库名 AS declare @backupfile nvarchar(200) --本地备份文件名 declare @backuplogfile nvarchar(200

sqlserver与mysql数据同步问题

由于短信项目的需求,需要将sqlserver当中的表实时同步到mysql当中,于是采用触发器完成该功能,对insert操作进行监控,当sqlserver进行插入操作时,同时将数据插入到mysql当中. 安装sqlserver2008以及mysql. 建立mysql链接服务器. 在本电脑上安装mysql驱动.Mysql-connector-odbc-5.1.6-win32.msi. 安装完毕后,在电脑控制面板-管理工具-odbc数据源-系统dsn-添加-选择mysql odbc driver-完成

sqlserver的执行计划

一:执行计划生成过程 说到执行计划,首先要知道的是执行计划大概生成的过程,这样就可以做到就心中有数了,下面我画下简图: 1. 分析过程 这三个比较容易理解,首先我们要保证sql的语法不能错误,select和join的表是必须存在的,以及你是有执行这个sql的权限,对不对... 这样我们就走完了执行计划生命周期的第一个流程. 2. 编译过程 保证了上面sql这三点的话,引擎就必须硬着头皮看你这么一大坨烂sql,该删的删,该改的改,该转换的转换,比如说你的“子查询”会转化为 “表连接”等等...其实

Caused by com microsoft sqlserver jdbc SQLServerException

1.错误描述 org.springframework.dao.TransientDataAccessResourceException: PreparedStatementCallback; SQL []; 索引 1 超出范围.; nested exception is com.microsoft.sqlserver.jdbc.SQLServerException: 索引 1 超出范围. at org.springframework.jdbc.support.SQLStateSQLExcepti

SQLServer数据库返回错误的国际化

前段时间测试提了一个BUG,我们繁体环境报错的提示是简体的,经查询之后这个错误是SQLServer抛出的,代码只是进行了异常转换,没有把异常信息换掉,在进行异常信息转换后,我又想,会不会有其他地方SQLServer的异常也没有转换,这个我没有办法去进行代码的全部扫描查询(即使进行了,也很难保证后面的同事不会再写),那我退而求其次,信息没转换我最起码能让用户看到的报错提示信息的语种是没有问题的,不能英语环境看到的是中文的报错.这个问题之前没有处理过也没有想过,于是只能自己摸索,经过查阅资料首先知道