轉載地址:https://www.cnblogs.com/tianqing/p/11152799.htmlhtml
今天線上SQLServer數據庫的CPU被打爆了,緊急狀況下,分析了數據庫阻塞、鏈接分佈、最耗CPU的TOP10 SQL、查詢SQL並行度配置、查詢SQL 重編譯的緣由等等sql
整理了一些經常使用的SQL數據庫
1. 查詢數據庫阻塞windows
1
|
SELECT
*
FROM
sys.sysprocesses
WHERE
blocked<>0
|
查詢結果中,重點看Blocked這一列,先找出最多的SID,而後循環找出Root的阻塞根源SIDspa
查詢阻塞根源Session的SQLcode
1
|
DBCC Inputbuffer(sid)
|
2. 查詢SQL鏈接分佈server
1
|
SELECT
Hostname
FROM
sys.sysprocesses
WHERE
hostname<>
''
|
select * from master.dbo.sysprocesses where dbid = DB_ID('TestDB2')
*查詢某個數據庫用戶的鏈接狀況htm
sp_who 'sa'
select @@MAX_CONNECTIONS
SELECT @@CONNECTIONS
上面的查詢能夠獲得spid,根據spid,關閉進程就能夠了。blog
kill 54
3. 查詢最消耗CPU的SQL Top10進程
1
2
3
|
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並行度
1
|
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
1
2
3
4
5
6
7
8
9
10
|
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
1
2
|
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、執行時間等)
1
2
3
|
SELECT
*
INTO
TabSQL
FROM
fn_trace_gettable(
'C:\Users\***\Desktop\Trace\sql05trace20180606-業務.trc'
,
default
);
GO
|
對上述表數據進行聚合分析最耗時的SQL
1
2
3
4
5
6
7
8
9
10
11
12
|
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
1
2
3
4
5
6
7
8
9
10
11
12
|
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
1
2
3
4
5
6
7
8
9
10
11
|
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
|