【Azure SQL】數(shù)據(jù)庫性能分析

前置條件

  • 用戶有查詢數(shù)據(jù)統(tǒng)計(jì)權(quán)限
GRANT VIEW DATABASE STATE TO database_user;

CPU性能問題

正在發(fā)生

  • 查看前X個(gè)CPU消耗查詢 (匯總)
SELECT TOP 10 GETDATE() runtime, * FROM (
    SELECT query_stats.query_hash,
                SUM (query_stats.cpu_time) 'Total_Request_Cpu_Time_Ms',
                SUM (logical_reads) 'Total_Request_Logical_Reads',
                MIN (start_time) 'Earliest_Request_start_Time',
                COUNT (*) 'Number_Of_Requests',
                SUBSTRING (REPLACE(REPLACE(MIN (query_stats.statement_text),CHAR (10),' '),CHAR (13),' '),1,256) AS "Statement_Text" FROM (
                SELECT req.*,
                SUBSTRING (ST.text,(req.statement_start_offset /2)+1,((CASE statement_end_offset WHEN-1 THEN DATALENGTH(ST.text) ELSE req.statement_end_offset END-req.statement_start_offset)/2)+1) AS statement_text 
                FROM sys.dm_exec_requests AS req CROSS APPLY sys.dm_exec_sql_text (req.sql_handle) AS ST) AS query_stats 
                GROUP BY query_hash) AS t 
ORDER BY Total_Request_Cpu_Time_Ms DESC;
  • 查看前X個(gè)CPU消耗查詢(按會話)
PRINT '--top 10 Active CPU Consuming Queries by sessions--'; 
SELECT TOP 10 req.session_id,req.start_time,cpu_time 'cpu_time_ms',OBJECT_NAME(ST.objectid,ST.dbid) 'ObjectName',
SUBSTRING (REPLACE(REPLACE(SUBSTRING (ST.text,(req.statement_start_offset /2)+1,((CASE statement_end_offset WHEN-1 THEN DATALENGTH(ST.text) ELSE req.statement_end_offset END-req.statement_start_offset)/2)+1),CHAR (10),' '),CHAR (13),' '),1,512) AS statement_text 
FROM sys.dm_exec_requests AS req CROSS APPLY sys.dm_exec_sql_text (req.sql_handle) AS ST 
ORDER BY cpu_time DESC;
GO

歷史發(fā)生

  • 統(tǒng)計(jì)某個(gè)時(shí)間段內(nèi)前X CPU耗時(shí)查詢
DECLARE @nums INT = 15;
DECLARE @start_hours INT = -8;
DECLARE @end_hours INT = -0;

WITH AggregatedCPU AS (
    SELECT
        q.query_hash,
        SUM ( count_executions * avg_cpu_time / 1000.0 ) AS total_cpu_millisec,
        SUM ( count_executions * avg_cpu_time / 1000.0 ) / SUM ( count_executions ) AS avg_cpu_millisec,
        MAX ( rs.max_cpu_time / 1000.00 ) AS max_cpu_millisec,
                SUM (count_executions *  avg_logical_io_reads) / SUM ( count_executions ) AS avg_io_reads,
        MAX ( max_logical_io_reads ) max_logical_reads,
        SUM ( count_executions ) AS total_executions,
        MIN ( qt.query_sql_text ) AS sampled_query_text 
    FROM
        sys.query_store_query_text AS qt
        JOIN sys.query_store_query AS q ON qt.query_text_id= q.query_text_id
        JOIN sys.query_store_plan AS p ON q.query_id= p.query_id
        JOIN sys.query_store_runtime_stats AS rs ON rs.plan_id= p.plan_id
        JOIN sys.query_store_runtime_stats_interval AS rsi ON rsi.runtime_stats_interval_id= rs.runtime_stats_interval_id 
    WHERE rsi.start_time >= DATEADD( HOUR,@start_hours,GETUTCDATE())
        AND rsi.start_time <=  DATEADD( HOUR,@end_hours,GETUTCDATE())
        AND count_executions > 10
    GROUP BY
        q.query_hash 
    ),
    OrderedCPU AS (
    SELECT
        query_hash,
        total_cpu_millisec,
        avg_cpu_millisec,
        max_cpu_millisec,
                avg_io_reads,
        max_logical_reads,
        total_executions,
        sampled_query_text,
        ROW_NUMBER ( ) OVER ( ORDER BY avg_cpu_millisec DESC, query_hash ASC ) AS RN 
    FROM
        AggregatedCPU 
    ) SELECT
    OD.total_cpu_millisec,
    OD.avg_cpu_millisec,
    OD.max_cpu_millisec,
        OD.avg_io_reads,
    OD.max_logical_reads,
    OD.total_executions,
    OD.sampled_query_text,
    OD.RN 
FROM
    OrderedCPU AS OD 
WHERE
    OD.RN <= @nums 
ORDER BY
    avg_cpu_millisec DESC;

IO性能問題

當(dāng)前發(fā)生

  • 查看一個(gè)小時(shí)內(nèi)每15S , IO使用情況
SELECT end_time, avg_data_io_percent, avg_log_write_percent
FROM sys.dm_db_resource_stats
ORDER BY end_time DESC;

歷史發(fā)生

  • 統(tǒng)計(jì)時(shí)間段IO等待情況
DECLARE @begin_hours INT = - 48;
DECLARE @end_hours INT = - 0;

WITH Aggregated AS (
    SELECT
        q.query_hash,
        SUM ( total_query_wait_time_ms ) total_wait_time_ms,
        SUM ( total_query_wait_time_ms / avg_query_wait_time_ms ) AS total_executions,
        MIN ( qt.query_sql_text ) AS sampled_query_text,
        MIN ( wait_category_desc ) AS wait_category_desc 
    FROM
        sys.query_store_query_text AS qt
        JOIN sys.query_store_query AS q ON qt.query_text_id= q.query_text_id
        JOIN sys.query_store_plan AS p ON q.query_id= p.query_id
        JOIN sys.query_store_wait_stats AS waits ON waits.plan_id= p.plan_id
        JOIN sys.query_store_runtime_stats_interval AS rsi ON rsi.runtime_stats_interval_id= waits.runtime_stats_interval_id 
    WHERE
        wait_category_desc = 'Buffer IO' 
        AND rsi.start_time >= DATEADD( HOUR, @begin_hours, GETUTCDATE( ) ) 
        AND rsi.start_time <= DATEADD( HOUR, @end_hours, GETUTCDATE( ) ) 
    GROUP BY
        q.query_hash 
    ),
    Ordered AS (
    SELECT
        query_hash,
        total_executions,
        total_wait_time_ms,
        sampled_query_text,
        wait_category_desc,
        ROW_NUMBER ( ) OVER ( ORDER BY total_wait_time_ms DESC, query_hash ASC ) AS RN 
    FROM
        Aggregated 
    ) SELECT
    OD.total_executions,
    OD.total_wait_time_ms,
    OD.sampled_query_text,
    OD.wait_category_desc,
    OD.RN 
FROM
    Ordered AS OD 
WHERE
    OD.RN<= 15 
ORDER BY
    total_wait_time_ms DESC;
GO
  • 寫入日志最多查詢
DECLARE @begin_hours INT = - 45;
DECLARE @end_hours INT = - 0;
WITH AggregatedLogUsed AS (
    SELECT
        q.query_hash,
        SUM ( count_executions * avg_cpu_time / 1000.0 ) AS total_cpu_millisec,
        SUM ( count_executions * avg_cpu_time / 1000.0 ) / SUM ( count_executions ) AS avg_cpu_millisec,
        SUM ( count_executions * avg_log_bytes_used ) AS total_log_bytes_used,
        MAX ( rs.max_cpu_time / 1000.00 ) AS max_cpu_millisec,
        SUM ( count_executions * avg_logical_io_reads ) / SUM ( count_executions ) AS avg_logical_io_reads,
        MAX ( max_logical_io_reads ) max_logical_reads,
        SUM ( count_executions ) AS total_executions,
        MAX ( qt.query_sql_text ) AS sampled_query_text 
    FROM
        sys.query_store_query_text AS qt
        JOIN sys.query_store_query AS q ON qt.query_text_id = q.query_text_id
        JOIN sys.query_store_plan AS p ON q.query_id = p.query_id
        JOIN sys.query_store_runtime_stats AS rs ON rs.plan_id = p.plan_id
        JOIN sys.query_store_runtime_stats_interval AS rsi ON rsi.runtime_stats_interval_id = rs.runtime_stats_interval_id 
    WHERE
        rs.execution_type_desc IN ( 'Regular', 'Aborted', 'Exception' ) 
        AND rsi.start_time >= DATEADD( HOUR, @begin_hours, GETUTCDATE( ) ) 
        AND rsi.start_time <= DATEADD( HOUR, @end_hours, GETUTCDATE( ) ) 
    GROUP BY
        q.query_hash 
    ),
    OrderedLogUsed AS (
    SELECT
        query_hash,
        total_log_bytes_used,
        total_executions,
        avg_logical_io_reads,
        max_logical_reads,
        sampled_query_text,
        ROW_NUMBER ( ) OVER ( ORDER BY total_log_bytes_used DESC, query_hash ASC ) AS RN 
    FROM
        AggregatedLogUsed 
    ) SELECT
    OD.total_log_bytes_used,
    ( OD.total_log_bytes_used / OD.total_executions ) avg_log_bytes_used,
    OD.total_executions,
    OD.avg_logical_io_reads,
    OD.max_logical_reads,
    OD.sampled_query_text,
    OD.RN 
FROM
    OrderedLogUsed AS OD 
WHERE
    OD.RN <= 15 
ORDER BY
    total_log_bytes_used DESC;
GO

連接數(shù)查詢

SELECT
    c.session_id, c.net_transport, c.encrypt_option,
    c.auth_scheme, s.host_name, s.program_name,
    s.client_interface_name, s.login_name, s.nt_domain,
    s.nt_user_name, s.original_login_name, c.connect_time,
    s.login_time
FROM sys.dm_exec_connections AS c
JOIN sys.dm_exec_sessions AS s
    ON c.session_id = s.session_id
WHERE c.session_id = @@SPID;

服務(wù)器使用情況

SELECT  
    AVG(avg_cpu_percent) AS 'Average CPU use in percent',
    MAX(avg_cpu_percent) AS 'Maximum CPU use in percent',
    AVG(avg_data_io_percent) AS 'Average data IO in percent',
    MAX(avg_data_io_percent) AS 'Maximum data IO in percent',
    AVG(avg_log_write_percent) AS 'Average log write use in percent',
    MAX(avg_log_write_percent) AS 'Maximum log write use in percent',
    AVG(avg_memory_usage_percent) AS 'Average memory use in percent',
    MAX(avg_memory_usage_percent) AS 'Maximum memory use in percent'
FROM sys.dm_db_resource_stats;

前X個(gè)消耗最多平均CPU時(shí)間查詢

SELECT TOP 10 query_stats.query_hash AS "Query Hash",
    SUM(query_stats.total_worker_time) / SUM(query_stats.execution_count) AS "Avg CPU Time",
     MIN(query_stats.statement_text) AS "Statement Text"
FROM
    (SELECT QS.*,
        SUBSTRING(ST.text, (QS.statement_start_offset/2) + 1,
            ((CASE statement_end_offset
                WHEN -1 THEN DATALENGTH(ST.text)
                ELSE QS.statement_end_offset END
            - QS.statement_start_offset)/2) + 1) AS statement_text
FROM sys.dm_exec_query_stats AS QS
CROSS APPLY sys.dm_exec_sql_text(QS.sql_handle) as ST) as query_stats
GROUP BY query_stats.query_hash
ORDER BY 2 DESC;
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末陆盘,一起剝皮案震驚了整個(gè)濱河市厨喂,隨后出現(xiàn)的幾起案子卷要,更是在濱河造成了極大的恐慌,老刑警劉巖掉丽,帶你破解...
    沈念sama閱讀 206,311評論 6 481
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件拓售,死亡現(xiàn)場離奇詭異折剃,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)绪抛,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,339評論 2 382
  • 文/潘曉璐 我一進(jìn)店門资铡,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人幢码,你說我怎么就攤上這事笤休。” “怎么了症副?”我有些...
    開封第一講書人閱讀 152,671評論 0 342
  • 文/不壞的土叔 我叫張陵店雅,是天一觀的道長政基。 經(jīng)常有香客問我,道長闹啦,這世上最難降的妖魔是什么沮明? 我笑而不...
    開封第一講書人閱讀 55,252評論 1 279
  • 正文 為了忘掉前任,我火速辦了婚禮窍奋,結(jié)果婚禮上荐健,老公的妹妹穿的比我還像新娘。我一直安慰自己琳袄,他們只是感情好江场,可當(dāng)我...
    茶點(diǎn)故事閱讀 64,253評論 5 371
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著窖逗,像睡著了一般址否。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上滑负,一...
    開封第一講書人閱讀 49,031評論 1 285
  • 那天在张,我揣著相機(jī)與錄音,去河邊找鬼矮慕。 笑死帮匾,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的痴鳄。 我是一名探鬼主播瘟斜,決...
    沈念sama閱讀 38,340評論 3 399
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼痪寻!你這毒婦竟也來了螺句?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 36,973評論 0 259
  • 序言:老撾萬榮一對情侶失蹤橡类,失蹤者是張志新(化名)和其女友劉穎蛇尚,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體顾画,經(jīng)...
    沈念sama閱讀 43,466評論 1 300
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡取劫,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 35,937評論 2 323
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了研侣。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片谱邪。...
    茶點(diǎn)故事閱讀 38,039評論 1 333
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖庶诡,靈堂內(nèi)的尸體忽然破棺而出惦银,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 33,701評論 4 323
  • 正文 年R本政府宣布扯俱,位于F島的核電站书蚪,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏蘸吓。R本人自食惡果不足惜善炫,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,254評論 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望库继。 院中可真熱鬧箩艺,春花似錦、人聲如沸宪萄。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,259評論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽拜英。三九已至静汤,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間居凶,已是汗流浹背虫给。 一陣腳步聲響...
    開封第一講書人閱讀 31,485評論 1 262
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留侠碧,地道東北人抹估。 一個(gè)月前我還...
    沈念sama閱讀 45,497評論 2 354
  • 正文 我出身青樓,卻偏偏與公主長得像弄兜,于是被迫代替她去往敵國和親药蜻。 傳聞我的和親對象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 42,786評論 2 345