日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

【飞秋】SQL Server性能调教系列(4)--Profiler(上)

發(fā)布時間:2025/3/15 数据库 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【飞秋】SQL Server性能调教系列(4)--Profiler(上) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

一:簡介

在處理性能問題是,DBA傾向于關(guān)注系統(tǒng)技術(shù)層面,如資源隊列,資源利用率,系統(tǒng)loading等。而用戶只把性能問題認(rèn)為是等待,他們從業(yè)務(wù)邏輯層面發(fā)出一個請求,等待返回結(jié)果,后臺數(shù)據(jù)庫就需要去響應(yīng)這個請求。從用戶角度來看,一般認(rèn)為等待三秒才返回就屬于性能問題(特殊的系統(tǒng)除外:比如需要大量的數(shù)據(jù)操作),他們并不關(guān)心系統(tǒng)的數(shù)據(jù)層,比如有多少個命令在等待處理,CPU利用率,RAM使用率等。在遇到這些問題之后,我們需要找到這個問題,請著手優(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.分析實例級的等待

2.聯(lián)系等待的隊列

3.確定方案

4.細(xì)化到數(shù)據(jù)庫/文件級

5.細(xì)化到進(jìn)程級

6.優(yōu)化索引/查詢

?

三:方法

本章主要介紹Profiler工具來跟蹤性能工作負(fù)荷。

1. Profiler簡介

通過SQL Server—>Tools—>SQL Server Profiler啟動

?

General頁:跟蹤的記錄有兩種保存方式:保存到文件和保存到表。通常選擇保存到文件,因為保存到表會增加較多的額外系統(tǒng)開銷。

Events Selection頁:能夠選擇跟蹤的事件。更多的跟蹤事件請參考MSDN。

?

注:

不要使用GUI跟蹤,應(yīng)該使用T-SQL。因為使用GUI會有兩個跟蹤,一個把跟蹤信息寫入目標(biāo)文件,一個把跟蹤信息寫入運行的GUI,會增加系統(tǒng)的額外開銷。
不要把跟蹤直接寫入到表,這樣會嚴(yán)重影響性能,把文件寫到磁盤是最快的方案。
跟蹤會產(chǎn)生大量和額外的IO操作。不要把跟蹤文件放到包含數(shù)據(jù)庫文件(如數(shù)據(jù),日志和tempdb)的磁盤上。
選擇事件類的數(shù)據(jù)列,只跟蹤需要的信息。
使用跟蹤篩選需要的事件。
2.啟動跟蹤

2.1? 可以先在GUI中設(shè)置需要跟蹤的事件類,然后在導(dǎo)出腳本(File—>Export-->Script Trace Definition),通常篩選Duration數(shù)列大于某些值(比如3000毫秒)的事件來跟蹤運行得比較慢的進(jìn)程。

?

如:導(dǎo)出的腳本如下,這里把trace的腳本整理為一個存儲過程,以方便執(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代碼啟動跟蹤:

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í)行之后會顯示出traceid,請記住這個traceid,會用它來停止和關(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小時專業(yè)轉(zhuǎn)載。

總結(jié)

以上是生活随笔為你收集整理的【飞秋】SQL Server性能调教系列(4)--Profiler(上)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。