【轉載】SQL Server中查詢CPU佔用高的SQL語句

來源:互聯網
上載者:User

標籤:

本文導讀:觸發器造成死結、作業多且頻繁、中間表的大量使用、遊標的大量使用、索引的設計不合理、事務操作頻繁、SQL語句設計不合理,都會造成查詢效率低下、影響伺服器效能的發揮。我們可以使用sql server內建的效能分析追蹤工具sql profiler分析資料庫設計所產生問題的來源,進行有針對性的處理;下面介紹SQL Server中如何查詢CPU佔用高的SQL語句

SQL Server中查詢CPU佔用高的情況,會用到sys.sysprocesses ,dm_exec_sessions ,dm_exec_requests

 

一、查看當前的資料庫使用者串連有多少

 

USE master

GO

SELECT * FROM sys.[sysprocesses] WHERE [spid]>50 --AND DB_NAME([dbid])=‘gposdb‘

SELECT COUNT(*) FROM [sys].[dm_exec_sessions] WHERE [session_id]>50

 

二、選取前10個最耗CPU時間的會話

 

 SQL 代碼   複製
SELECT TOP 10 
[session_id], 
[request_id], 
[start_time] AS ‘開始時間‘, 
[status] AS ‘狀態‘, 
[command] AS ‘命令‘, 
dest.[text] AS ‘sql語句‘, 
DB_NAME([database_id]) AS ‘資料庫名‘, 
[blocking_session_id] AS ‘正在阻塞其他會話的會話ID‘, 
[wait_type] AS ‘等待資源類型‘, 
[wait_time] AS ‘等待時間‘, 
[wait_resource] AS ‘等待的資源‘, 
[reads] AS ‘物理讀次數‘, 
[writes] AS ‘寫次數‘, 
[logical_reads] AS ‘邏輯讀次數‘, 
[row_count] AS ‘返回結果行數‘ 
FROM sys.[dm_exec_requests] AS der 
CROSS APPLY 
sys.[dm_exec_sql_text](der.[sql_handle]) AS dest 
WHERE [session_id]>50 AND DB_NAME(der.[database_id])=‘gposdb‘ 
ORDER BY [cpu_time] DESC 

 

三、查詢前10個最耗CPU時間的SQL語句

 

 SQL 代碼   複製
--在SSMS裡選擇以文字格式設定顯示結果 
SELECT TOP 10 
dest.[text] AS ‘sql語句‘ 
FROM sys.[dm_exec_requests] AS der 
CROSS APPLY 
sys.[dm_exec_sql_text](der.[sql_handle]) AS dest 
WHERE [session_id]>50 
ORDER BY [cpu_time] DESC 

 

四、查詢會話中有多少個worker在等待 

 

 SQL 代碼   複製
SELECT TOP 10 
 [session_id], 
 [request_id], 
 [start_time] AS ‘開始時間‘, 
 [status] AS ‘狀態‘, 
 [command] AS ‘命令‘, 
 dest.[text] AS ‘sql語句‘, 
 DB_NAME([database_id]) AS ‘資料庫名‘, 
 [blocking_session_id] AS ‘正在阻塞其他會話的會話ID‘, 
 der.[wait_type] AS ‘等待資源類型‘, 
 [wait_time] AS ‘等待時間‘, 
 [wait_resource] AS ‘等待的資源‘, 
 [dows].[waiting_tasks_count] AS ‘當前進行中等待的任務數‘, 
 [reads] AS ‘物理讀次數‘, 
 [writes] AS ‘寫次數‘, 
 [logical_reads] AS ‘邏輯讀次數‘, 
 [row_count] AS ‘返回結果行數‘ 
 FROM sys.[dm_exec_requests] AS der 
 INNER JOIN [sys].[dm_os_wait_stats] AS dows 
 ON der.[wait_type]=[dows].[wait_type] 
 CROSS APPLY 
 sys.[dm_exec_sql_text](der.[sql_handle]) AS dest 
 WHERE [session_id]>50 
 ORDER BY [cpu_time] DESC 

 

五、查詢CPU佔用高的語句

 

 SQL 代碼   複製
SELECT TOP 10 
 total_worker_time/execution_count AS avg_cpu_cost, plan_handle, 
 execution_count, 
 (SELECT SUBSTRING(text, statement_start_offset/2 + 1, 
 (CASE WHEN statement_end_offset = -1 
 THEN LEN(CONVERT(nvarchar(max), text)) * 2 
 ELSE statement_end_offset 
 END - statement_start_offset)/2) 
 FROM sys.dm_exec_sql_text(sql_handle)) AS query_text 
FROM sys.dm_exec_query_stats 
ORDER BY [avg_cpu_cost] DESC 

【轉載】SQL Server中查詢CPU佔用高的SQL語句

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.