SQL SERVER佔用CPU太高優化

  操做系統是Windows2008R2 ,數據庫是SQL2014 64位。sql

  近階段服務器出現過幾回死機,管理員反饋機器內存使用率100%致使機器卡死。因而作了個監測服務器的軟件實時記錄CPU數據,幾日觀察得出數據以下:數據庫

  SQL優化方法:服務器

  一、查看鏈接對象session

SELECT * FROM sys.[sysprocesses] WHERE [spid]>50 --AND DB_NAME([dbid])='gposdb'
select client_net_address '客戶端IP',local_net_address '服務器的IP',* from sys.dm_exec_connections

 

  當前鏈接對象有67個其中‘WINAME’的主機名,‘jTDS’的進程名不屬於已知經常使用軟件,找到這臺主機並解決鏈接問題。在360流量防火牆中查看有哪一個軟件鏈接了服務器IP,除之。測試

二、而後使用下面語句看一下各項指標是否正常,是否有阻塞,正常狀況下搜索結果應該爲空。優化

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

 

查看是哪些SQL語句佔用較大可使用下面代碼spa

--在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

 

三、若是SQLSERVER存在要等待的資源,那麼執行下面語句就會顯示出會話中有多少個worker在等待操作系統

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語句3d

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

 

五、索引缺失查詢code

SELECT 
    DatabaseName = DB_NAME(database_id)
    ,[Number Indexes Missing] = count(*) 
FROM sys.dm_db_missing_index_details
GROUP BY DB_NAME(database_id)
ORDER BY 2 DESC;
SELECT  TOP 10 
        [Total Cost]  = ROUND(avg_total_user_cost * avg_user_impact * (user_seeks + user_scans),0) 
        , avg_user_impact
        , TableName = statement
        , [EqualityUsage] = equality_columns 
        , [InequalityUsage] = inequality_columns
        , [Include Cloumns] = included_columns
FROM        sys.dm_db_missing_index_groups g 
INNER JOIN    sys.dm_db_missing_index_group_stats s 
       ON s.group_handle = g.index_group_handle 
INNER JOIN    sys.dm_db_missing_index_details d 
       ON d.index_handle = g.index_handle
ORDER BY [Total Cost] DESC;

 

 找到索引缺失的表,根據查詢結果中的關鍵次逐一創建索引。

  作完這些測試,基本能找到問題。

相關文章
相關標籤/搜索