SQL SERVER占用CPU過高優化
阿新 • • 發佈:2019-05-04
應該 missing 執行 技術 code cpu過高 觀察 lec 卡死
操作系統是Windows2008R2 ,數據庫是SQL2014 64位。
近階段服務器出現過幾次死機,管理員反饋機器內存使用率100%導致機器卡死。於是做了個監測服務器的軟件實時記錄CPU數據,幾日觀察得出數據如下:
SQL優化方法:
1、查看連接對象
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,除之。
2、然後使用下面語句看一下各項指標是否正常,是否有阻塞,正常情況下搜索結果應該為空。
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語句占用較大可以使用下面代碼
--在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
3、如果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
4、查詢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
5、索引缺失查詢
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;
找到索引缺失的表,根據查詢結果中的關鍵次逐一建立索引。
做完這些測試,基本能找到問題。
SQL SERVER占用CPU過高優化