2008年6月29日 星期日

SQLDiag工具程式的使用

SQLDiag工具程式的使用

 
前言:SQLDiag是SQL Server內建的診斷收集共用程式,
[SQL Server 2005]
預設位置:C:\Program Files\Microsoft SQL Server\90\Tools\Binn\SQLDiag.exe
你可以在任何路徑下執行SQLDiag因為此路徑已被加到path系統變數,執行sqldiag -?或sqldiag /?即可顯示參數說明
SQL 2005的sqldiag搭配參數請使用正斜線"/"
SQLdiag 
可以收集下列類型的診斷資訊:
Windows 效能記錄
Windows 事件記錄檔
SQL Server Profiler 追蹤
SQL Server 封鎖資訊
SQL Server 組態資訊
一般使用:
例如: 
sqldiag /O C:\temp\sqldiag
(/O 指定輸出檔案的目錄)
最下方會出現
2008/07/01 00:34:42.82 SQLDIAG 
Collection started. Press Ctrl+C to stop.表示正在收集中
在此時按下Ctrl+C即可停止收集 收集的資訊會儲存在指定的C:\temp\sqldiag\目錄下,
其中數個log_xx.trc檔是從C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\LOG\路徑下所複製的
進階收集: 修改並指定xml檔,來收集Performance Counter的資訊
[SQL Server 2000]
預設位置:C:\Program Files\Microsoft SQL Server\MSSQL\Binn\SQLDiag.exe
切換目錄到此位置後執行sqldiag -?或sqldiag /?即可顯示參數說明
SQL 2000的sqldiag搭配參數執行要使用"-"
例如: sqldiag -E -O c:\temp\sqldiag.log
(-O 指定輸出資訊到指定的檔案裡)
若要讓SQL 2000可以取得SQL Trace(*.trc)資料,可以利用下面[Sample Code]或KB281671文件範例SQL在master資料庫新增一個名為trace_blackbox的Stored Procedure
KB281671 INF: 預存程序來建立一個 SQL Server 2000 的 blackbox 追蹤
然後在排定時間執行trace_blackbox 1以啟動trace_blackbox,執行期間這個Instance所有連線的SQL Command都會記錄到C:\Program Files\Microsoft SQL Server\MSSQL\Data\blackbox開頭.trc,在必要的時間執行trace_blackbox 0以關閉trace_blackbox,而這些trc檔案都將會被sqldiag所複製並更名到指定的位置
[Caution] 
若執行SQLDiag當時trace_blackbox還開著,檔案將無法複製所以SQLDiag.trc會是空的
[Samlp Code]
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[trace_blackbox]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[trace_blackbox]
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO
CREATE    PROCEDURE trace_blackbox @on int = 2 AS
/* If no argument is passed to the @on parameter then get the current blackbox trace status.
  If @on is zero then stop and delete the blackbox trace.
  If @on is one then create and start the blackbox trace.
*/
declare @traceid int, @blackboxstatus int, @dir nvarchar(80)
set @traceid = 0
set @blackboxstatus = 0
set nocount on
SELECT @traceid = traceid FROM :: fn_trace_getinfo(0)
where property = 1 and value = 8
IF @on = 0 and @traceid > 0
begin
 select @blackboxstatus = cast(value as int) FROM :: fn_trace_getinfo(0)
  where traceid = @traceid and property = 5
 IF @blackboxstatus > 0 exec sp_trace_setstatus @traceid,0 --stop blackbox trace
 exec sp_trace_setstatus @traceid,2 --delete blackbox trace definition
end
IF @on = 1
  begin
   IF @traceid < traceid =" 0" blackboxstatus =" 0" traceid =" traceid" property =" 1" value =" 8" blackboxstatus =" cast(value" traceid =" @traceid" property =" 5"> 0 and @blackboxstatus > 0
  begin
   select @dir = cast(value as nvarchar(80)) FROM :: fn_trace_getinfo(0)
    where traceid = @traceid and property = 2
   select 'The blackbox trace is running and the trace file is in the following directory.'
   select @dir + '.trc'
  end
ELSE select 'The blackbox trace is not running.'
set nocount off
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON

沒有留言:

SQL Server Database Mirroring 資料庫鏡像

SQL Server Database Mirroring 資料庫鏡像