1. 程式人生 > >SQLServer常用運維SQL整理

SQLServer常用運維SQL整理

今天線上SQLServer資料庫的CPU被打爆了,緊急情況下,分析了資料庫阻塞、連線分佈、最耗CPU的TOP10 SQL、查詢SQL並行度配置、查詢SQL 重編譯的原因等等

整理了一些常用的SQL

1. 查詢資料庫阻塞

SELECT * FROM  sys.sysprocesses WHERE blocked<>0  

查詢結果中,重點看Blocked這一列,先找出最多的SID,然後迴圈找出Root的阻塞根源SID

查詢阻塞根源Session的SQL

DBCC Inputbuffer(sid)

2. 查詢SQL連線分佈

SELECT Hostname FROM  sys.sysprocesses WHERE hostname<>''

3. 查詢最消耗CPU的SQL Top10

select top(10) st.text as Query, qs.total_worker_time, qs.execution_count from 
sys.dm_exec_query_stats as qs CROSS Apply sys.dm_exec_sql_text(qs.sql_handle) AS st
order by qs.total_worker_time desc

4. 檢視SQLServer並行度

SELECT value_in_use  FROM sys.configurations WHERE name = 'max degree of parallelism'

並行度如果設定為1,To suppress parallel plan generation, set max degree of parallelism to 1

將阻止並行編譯生成SQL執行計劃,最大並行度設定為1

設定策略和具體設定方法,請參考:https://docs.microsoft.com/en-us/sql/database-engine/configure-windows/configure-the-max-degree-of-parallelism-server-configuration-option?view=sql-server-2017

USE DatabaseName ;  
GO   
EXEC sp_configure 'show advanced options', 1;  
GO  
RECONFIGURE WITH OVERRIDE;  
GO  
EXEC sp_configure 'max degree of parallelism', 16;  
GO  
RECONFIGURE WITH OVERRIDE;  
GO

  

5. 查詢SQL Server Recompilation Reasons

select dxmv.name, dxmv.map_key,dxmv.map_value from 
sys.dm_xe_map_values as dxmv where dxmv.name='statement_recompile_cause' order by dxmv.map_key

6. 將SQL Trace檔案存入一張表,做聚合分析(CPU、IO、執行時間等)

SELECT * INTO TabSQL
FROM fn_trace_gettable('C:\Users\***\Desktop\Trace\sql05trace20180606-業務.trc', default);
GO

對上述表資料進行聚合分析最耗時的SQL

select  top 100 	
	    replace(replace(replace(  substring(Textdata,1,6600) ,char(10),' '),char(13),' ') ,char(9),' ')  as '名稱',
		--substring(Textdata,1,6600)  as old,
       count(*) as '數量',
       sum(duration/1000) as '總執行時間ms',
       avg(duration/1000) as '平均執行時間ms',
       avg(cpu) as '平均CPU時間ms',
       avg(reads) as '平均讀次數',
       avg(writes) as '平均寫次數', LoginName
from TabSQL   t
group by   replace(replace(replace(  substring(Textdata,1,6600) ,char(10),' '),char(13),' ') ,char(9),' ') , LoginName
order by sum(duration) desc 

最耗IO的SQL

select  TOP 100 replace(replace(replace(  substring(Textdata,1,6600) ,char(10),' '),char(13),' ') ,char(9),' ') as '名稱' ,LoginName, 
       count(*) as '數量',
       sum(duration/1000) as '總執行時間ms',
       avg(duration/1000) as '平均執行時間ms',
       sum(cpu) as '總CPU時間ms',
       avg(cpu) as '平均CPU時間ms',
       sum(reads) as '總讀次數',
       avg(reads) as '平均讀次數',
       avg(writes) as '平均寫次數'
from TabSQL
group by replace(replace(replace(  substring(Textdata,1,6600) ,char(10),' '),char(13),' ') ,char(9),' ')  ,LoginName 
order by  sum(reads) desc

最耗CPU的SQL

SELECT TOP 100 replace(replace(replace(  substring(Textdata,1,6600) ,char(10),' '),char(13),' ') ,char(9),' ')  as '名稱',LoginName,
       count(*) as '數量',
       sum(duration/1000) as '總執行時間ms',
       avg(duration/1000) as '平均執行時間ms',
       sum(cpu) as '總CPU時間',
       avg(cpu) as '平均CPU時間',
       avg(reads) as '平均讀次數',
       avg(writes) as '平均寫次數'
from TabSQL
group by replace(replace(replace(  substring(Textdata,1,6600) ,char(10),' '),char(13),' ') ,char(9),' ')   ,LoginName
order by avg(cpu) desc

 

  

 

周國慶

2019