亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

sql server 正在運行的sql語句

系統 1931 0

Introduction

sp_who2 is a well known utility that shows what spids are currently executing. However the information it shows is relatively limited. For example, it only shows the type of command executing as SELECT, DELETE etc, with no reference to the actual underlying SQL executing.

Knowing what SQL is executing can be vital in debugging why a query is taking a long time, or determining if it is being blocked. It can also be useful in showing the progress of a stored procedure i.e. what statement within the stored procedure is currently executing.

The utility described in this article will obviate these limitations of sp_who2.

The utility makes use of Dynamic Management Views (DMVs), so can be used by SQL Server 2005 or greater.

What SQL Statements Are Currently Executing Utility

The SQL used in this utility ‘dba_WhatSQLIsExecuting’ is given in Listing 1.

The Dynamic Management View (DMV) sys.db_exec_requests shows which requests are currently executing, the information shown includes the handle to the whole SQL text of the batch or stored procedure (sql_handle), together with offsets relating to the section of SQL within the batch that is currently executing (statement_start_offset and statement_end_offset).

To determine the current section of SQL currently executing, we need to call the Dynamic Management Function (DMF) sys.dm_exec_sql_text, passing in the handle of the SQL batch that is currently executing, and then apply the relevant offsets.

We can get more information about the query by combining the sys.db_exec_requests DMV with the sys.processes system view (joined on spid/session_id). This information includes who is executing the query, the machine they are running from, and the name of the database.

The utility selects relevant fields from the sys.db_exec_requests and sys.sysprocesses views. The selected fields are described in figure 1 (largely taken from SQL Server 2005 Books online).

Column name Data type Description
spid smallint SQL Server process ID.
ecid smallint Execution context ID used to uniquely identify the subthreads operating on behalf of a single process.
dbid smallint ID of the database currently being used by the process.
nt_username nchar(128) Windows user name for the process, if using Windows Authentication, or a trusted connection.
status nchar(30) Process ID status. For example, running and sleeping.
wait_type bigint Current wait time in milliseconds.
Individual Query varchar SQL Statement currently running.
Parent Query varchar Routine that contains the Individual Query.
program_name nchar(128) Name of the application program.
Hostname nchar(128) Name of the workstation.
nt_domain nchar(128) Microsoft Windows domain for the client, if using Windows Authentication, or a trusted connection.
Start_time datetime Time when the request is scheduled to run.

Figure 1 Columns in the ‘What SQL Statements Are Executing’ utility.

Running the utility on my SQL Server gives the results given in Figure 2.

Figure 2 Output from the ‘What SQL Statements Are Executing’ utility.

The results show the Parent Query that is running (typically a stored procedure), together with the Individual Query within the Parent Query that is currently executing. Additional useful information (e.g. database name, user name etc) is also shown.

Discussion

This utility allows you to observe the progress of a stored procedure or SQL batch, additionally it can be used to identify the cause of a long running query or blocking query.

Since the utility uses existing data held in DMVs it is relatively non-intrusive and should have little affect on performance.

If the identified queries are long running or causing blocking, it might be worthwhile running them inside the Database Tuning Advisor (DTA), this might identify the cause of the slow running (e.g. a missing index).

Further work

It is possible to extend this utility to report only on the database you are interested in, by providing a filter based on database name or database id.

It might be interesting to use the output to drive a trace and/or process-flow engine. This will report on process flow through a stored procedure, and could be useful in determining how much code has been hit/missed during testing, as well as getting a view on what code is executed for a given run/set of parameters.

Conclusion

The utility described in this article will allow you to identify what SQL statements are currently executing. This information can be useful in debugging the cause of both long running queries and blocking, and should prove valuable in the everyday work of the SQL Server DBA/developer.

Credits

Ian Stirkhas been working in IT as a developer, designer, and architect since 1987. He holds the following qualifications: M.Sc., MCSD.NET, MCDBA, and SCJP. He is a freelance consultant working with Microsoft technologies in London England. He can be contacted at Ian_Stirk@yahoo.com .

Code

      
CREATE PROC [ dbo ] . [ dba_WhatSQLIsExecuting ]
AS
/* --------------------------------------------------------------------
Purpose: Shows what individual SQL statements are currently executing.
----------------------------------------------------------------------
Parameters: None.
Revision History:
24/07/2008 Ian_Stirk@yahoo.com Initial version
Example Usage:
1. exec YourServerName.master.dbo.dba_WhatSQLIsExecuting
---------------------------------------------------------------------
*/
BEGIN
-- Do not lock anything, and do not get held up by any locks.
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED

-- What SQL Statements Are Currently Running?
SELECT [ Spid ] = session_Id
, ecid
,
[ Database ] = DB_NAME (sp.dbid)
,
[ User ] = nt_username
,
[ Status ] = er.status
,
[ Wait ] = wait_type
,
[ Individual Query ] = SUBSTRING (qt. text ,
er.statement_start_offset
/ 2 ,
(
CASE WHEN er.statement_end_offset = - 1
THEN LEN ( CONVERT ( NVARCHAR ( MAX ), qt. text )) * 2
ELSE er.statement_end_offset END -
er.statement_start_offset)
/ 2 )
,
[ Parent Query ] = qt. text
, Program
= program_name
, Hostname
, nt_domain
, start_time
FROM sys.dm_exec_requests er
INNER JOIN sys.sysprocesses sp ON er.session_id = sp.spid
CROSS APPLY sys.dm_exec_sql_text(er.sql_handle) as qt
WHERE session_Id > 50 -- Ignore system spids.
AND session_Id NOT IN ( @@SPID ) -- Ignore this current statement.
ORDER BY 1 , 2
END
from http://www.sqlservercentral.com/articles/DMV/64425/

sql server 正在運行的sql語句


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 看黄色一级毛片 | 黄色综合网站 | 五月天激情在线 | 亚洲香蕉网综合久久 | 久久精品99精品免费观看 | 亚洲精品在线免费看 | 国产与自拍 | 中文字幕一区中文亚洲 | 97视频精品全国在线观看 | 国产成人精品男人免费 | 国产精品美女一区二区三区 | 深夜在线免费 | 国产成人精品视频一区二区不卡 | 波多野结衣一区二区三区 | 国产成人高清亚洲一区久久 | 成人毛片18女人毛片免费96 | 99久久免费国产精品m9 | 国产另类视频 | 国产精品亚洲精品日韩已满 | 欧美乱妇在线观看 | 91视频国内 | 奇米影视888狠狠狠777九色 | 不卡影院网 | 精品免费国产一区二区三区 | 亚洲男人的天堂久久无 | 天天射天天色天天干 | 精品欧美一区二区三区在线观看 | 黄色片网站在线观看 | 国产激情一级毛片久久久 | 亚洲欧洲国产成人综合一本 | 国产日本欧美亚洲精品视 | 久久精品啪啪嗷嗷叫 | 成人禁在线观看午夜亚洲 | 亚洲欧美另类国产综合 | 99精品国产在这里白浆 | 亚洲九九视频 | 99精品久久久久久久婷婷 | 老司机永久免费视频 | 日韩欧美精品在线视频 | 日本中文字幕不卡免费视频 | 天天操天天干天天舔 |