【飞秋】SQL Server性能调教系列(4)--Profiler(上)
一:簡介
在處理性能問題是,DBA傾向于關(guān)注系統(tǒng)技術(shù)層面,如資源隊(duì)列,資源利用率,系統(tǒng)loading等。而用戶只把性能問題認(rèn)為是等待,他們從業(yè)務(wù)邏輯層面發(fā)出一個(gè)請求,等待返回結(jié)果,后臺(tái)數(shù)據(jù)庫就需要去響應(yīng)這個(gè)請求。從用戶角度來看,一般認(rèn)為等待三秒才返回就屬于性能問題(特殊的系統(tǒng)除外:比如需要大量的數(shù)據(jù)操作),他們并不關(guān)心系統(tǒng)的數(shù)據(jù)層,比如有多少個(gè)命令在等待處理,CPU利用率,RAM使用率等。在遇到這些問題之后,我們需要找到這個(gè)問題,請著手優(yōu)化,找到合理的解決方案。
?
注:硬件方面的問題請參照系列(2)
SQL Server性能調(diào)教系列(2)--Server Performance Monitor(Perfmon)
?
二:理論
要做優(yōu)化,首先要找出需要優(yōu)化的部分(如找到效率低的SQL或SP等),引用SQL技術(shù)內(nèi)幕中介紹的優(yōu)化步驟:
1.分析實(shí)例級(jí)的等待
2.聯(lián)系等待的隊(duì)列
3.確定方案
4.細(xì)化到數(shù)據(jù)庫/文件級(jí)
5.細(xì)化到進(jìn)程級(jí)
6.優(yōu)化索引/查詢
?
三:方法
本章主要介紹Profiler工具來跟蹤性能工作負(fù)荷。
1. Profiler簡介
通過SQL Server—>Tools—>SQL Server Profiler啟動(dòng)
?
General頁:跟蹤的記錄有兩種保存方式:保存到文件和保存到表。通常選擇保存到文件,因?yàn)楸4娴奖頃?huì)增加較多的額外系統(tǒng)開銷。
Events Selection頁:能夠選擇跟蹤的事件。更多的跟蹤事件請參考MSDN。
?
注:
不要使用GUI跟蹤,應(yīng)該使用T-SQL。因?yàn)槭褂肎UI會(huì)有兩個(gè)跟蹤,一個(gè)把跟蹤信息寫入目標(biāo)文件,一個(gè)把跟蹤信息寫入運(yùn)行的GUI,會(huì)增加系統(tǒng)的額外開銷。
不要把跟蹤直接寫入到表,這樣會(huì)嚴(yán)重影響性能,把文件寫到磁盤是最快的方案。
跟蹤會(huì)產(chǎn)生大量和額外的IO操作。不要把跟蹤文件放到包含數(shù)據(jù)庫文件(如數(shù)據(jù),日志和tempdb)的磁盤上。
選擇事件類的數(shù)據(jù)列,只跟蹤需要的信息。
使用跟蹤篩選需要的事件。
2.啟動(dòng)跟蹤
2.1? 可以先在GUI中設(shè)置需要跟蹤的事件類,然后在導(dǎo)出腳本(File—>Export-->Script Trace Definition),通常篩選Duration數(shù)列大于某些值(比如3000毫秒)的事件來跟蹤運(yùn)行得比較慢的進(jìn)程。
?
如:導(dǎo)出的腳本如下,這里把trace的腳本整理為一個(gè)存儲(chǔ)過程,以方便執(zhí)行.
CREATE PROC [dbo].[sp_perfworkload_trace_start]
? @dbid????? AS INT,
? @tracefile AS NVARCHAR(254),
? @traceid?? AS INT OUTPUT
AS
-- Create a Queue
DECLARE @rc????????? AS INT;
DECLARE @maxfilesize AS BIGINT;
SET @maxfilesize = 100;
EXEC @rc = sp_trace_create @traceid OUTPUT, 0, @tracefile, @maxfilesize, NULL
IF (@rc != 0) GOTO error;
-- Client side File and Table cannot be scripted
-- Set the events
DECLARE @on AS BIT;
SET @on = 1;
EXEC sp_trace_setevent @traceid, 10, 15, @on;
EXEC sp_trace_setevent @traceid, 10, 8, @on;
EXEC sp_trace_setevent @traceid, 10, 16, @on;
EXEC sp_trace_setevent @traceid, 10, 48, @on;
EXEC sp_trace_setevent @traceid, 10, 1, @on;
EXEC sp_trace_setevent @traceid, 10, 17, @on;
EXEC sp_trace_setevent @traceid, 10, 10, @on;
EXEC sp_trace_setevent @traceid, 10, 18, @on;
EXEC sp_trace_setevent @traceid, 10, 11, @on;
EXEC sp_trace_setevent @traceid, 10, 12, @on;
EXEC sp_trace_setevent @traceid, 10, 13, @on;
EXEC sp_trace_setevent @traceid, 10, 14, @on;
EXEC sp_trace_setevent @traceid, 45, 8, @on;
EXEC sp_trace_setevent @traceid, 45, 16, @on;
EXEC sp_trace_setevent @traceid, 45, 48, @on;
EXEC sp_trace_setevent @traceid, 45, 1, @on;
EXEC sp_trace_setevent @traceid, 45, 17, @on;
EXEC sp_trace_setevent @traceid, 45, 10, @on;
EXEC sp_trace_setevent @traceid, 45, 18, @on;
EXEC sp_trace_setevent @traceid, 45, 11, @on;
EXEC sp_trace_setevent @traceid, 45, 12, @on;
EXEC sp_trace_setevent @traceid, 45, 13, @on;
EXEC sp_trace_setevent @traceid, 45, 14, @on;
EXEC sp_trace_setevent @traceid, 45, 15, @on;
EXEC sp_trace_setevent @traceid, 41, 15, @on;
EXEC sp_trace_setevent @traceid, 41, 8, @on;
EXEC sp_trace_setevent @traceid, 41, 16, @on;
EXEC sp_trace_setevent @traceid, 41, 48, @on;
EXEC sp_trace_setevent @traceid, 41, 1, @on;
EXEC sp_trace_setevent @traceid, 41, 17, @on;
EXEC sp_trace_setevent @traceid, 41, 10, @on;
EXEC sp_trace_setevent @traceid, 41, 18, @on;
EXEC sp_trace_setevent @traceid, 41, 11, @on;
EXEC sp_trace_setevent @traceid, 41, 12, @on;
EXEC sp_trace_setevent @traceid, 41, 13, @on;
EXEC sp_trace_setevent @traceid, 41, 14, @on;
-- Set the Filters
DECLARE @intfilter AS INT;
DECLARE @bigintfilter AS BIGINT;
-- Application name filter
EXEC sp_trace_setfilter @traceid, 10, 0, 7, N'SQL Server Profiler%';
-- Database ID filter
EXEC sp_trace_setfilter @traceid, 3, 0, 0, @dbid;
-- Set the trace status to start
EXEC sp_trace_setstatus @traceid, 1;
-- Print trace id and file name for future references
PRINT 'Trce ID: ' + CAST(@traceid AS VARCHAR(10))
? + ', Trace File: ''' + @tracefile + '''';
GOTO finish;
error:
PRINT 'Error Code: ' + CAST(@rc AS VARCHAR(10));
finish:
2.2 用以下T-SQL代碼啟動(dòng)跟蹤:
declare @dbid int,@traceid int;
set @dbid=DB_ID()---可以為默認(rèn)的DB,或者DB_ID('Your_dbname')
EXEC sp_perfworkload_trace_start @dbid,'C:/test/performancetrace_20100802.trc',@traceid output
執(zhí)行之后會(huì)顯示出traceid,請記住這個(gè)traceid,會(huì)用它來停止和關(guān)閉追蹤。
Trce ID: 2, Trace File: 'C:/test/performancetrace_20100802.trc'
?
2.3 停止和關(guān)閉追蹤(sp_trace_setstatus,如果traceid是2,則停止和關(guān)閉的代碼如下):
EXEC sp_trace_setstatus 2,0
EXEC sp_trace_setstatus 2,2 2.4
如果忘記traceid,可以在查詢試圖sys.traces找到。
?
?
接下篇:
SQL Server性能調(diào)教系列(4)--Profiler(下)
關(guān)注技術(shù)文章飛秋:http://www.freeeim.com/,24小時(shí)專業(yè)轉(zhuǎn)載。
總結(jié)
以上是生活随笔為你收集整理的【飞秋】SQL Server性能调教系列(4)--Profiler(上)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: AutoLayouterLib第一版基本
- 下一篇: Application对象 简单的聊天室