MSSQL Server运维常用SQL命令

1、数据库连接数

select name, state, state_desc from sys.databases;

查询结果:

2、数据库状态

select name, state, state_desc from sys.databases;

查询结果:

3、数据文件状态

select a.name, b.physical_name, b.state, b.state_desc from sys.databases as a, sys.master_files as b where a.database_id = b.database_id;

4、平均每秒事务数TPS

5、 缓存命中率

SELECT (a.cntr_value * 1.0 / b.cntr_value) * 100.0 as BufferCacheHitRatio FROM sys.dm_os_performance_counters a JOIN (SELECT cntr_value, OBJECT_NAME FROM sys.dm_os_performance_counters WHERE counter_name = 'Buffer cache hit ratio base' AND OBJECT_NAME = 'SQLServer:Buffer Manager') b ON a.OBJECT_NAME = b.OBJECT_NAME WHERE a.counter_name = 'Buffer cache hit ratio' AND a.OBJECT_NAME = 'SQLServer:Buffer Manager'

6、平均每秒SQL编译数

select cntr_value from sys.dm_os_performance_counters where counter_name = 'SQL Compilations/sec';

7、平均每秒SQL重编译数

select cntr_value from sys.dm_os_performance_counters where counter_name = 'Lock Requests/sec'and instance_name = '_Total';

8、每秒全表扫描数

select cntr_value from sys.dm_os_performance_counters where counter_name = 'Full Scans/sec';

9、平均每秒batch数

select cntr_value from sys.dm_os_performance_counters where counter_name = 'Batch Requests/sec';

10、每秒用户错误数

select cntr_value from sys.dm_os_performance_counters where counter_name = 'Errors/sec' and instance_name = '_Total';

11、每秒锁等待次数

select cntr_value from sys.dm_os_performance_counters where counter_name = 'Lock Waits/sec'and instance_name = '_Total';

12、'每秒锁请求次数

select cntr_value from sys.dm_os_performance_counters where counter_name = 'Lock Requests/sec'and instance_name = '_Total';

13、每秒锁超时次数

select cntr_value from sys.dm_os_performance_counters where counter_name = 'Lock Timeouts/sec'and instance_name = '_Total';

14、每秒锁死次数

select cntr_value from sys.dm_os_performance_counters where counter_name = 'Lock Requests/sec'and instance_name = '_Total';

15、查看死锁

SELECT request_session_id spid,OBJECT_NAME(resource_associated_entity_id)tableName FROM sys.dm_tran_locks WHERE resource_type='OBJECT ';

16、杀死死锁

Kill pid

17、显示死锁相关信息

exec sp_who2 pid

18、死锁跟踪

select * from sys.dm_xe_sessions where name = 'system_health'

SELECT

xed.value('@timestamp','datetime')as Creation_Date,

xed.query('.')AS Extend_Event

FROM

(

SELECT CAST([target_data] AS XML)AS Target_Data

FROM sys.dm_xe_session_targets AS xt

INNER JOIN sys.dm_xe_sessions AS xs

ON xs.address= xt.event_session_address

WHERE xs.name=N'system_health'

AND xt.target_name=N'ring_buffer'

) AS XML_Data

CROSS APPLY Target_Data.nodes('RingBufferTarget/event[@name="xml_deadlock_report"]')AS XEventData(xed)

ORDER BY Creation_Date DESC

19、每秒检查点写入Page数

select cntr_value from sys.dm_os_performance_counters where counter_name = 'Checkpoint pages/sec';

20、Lazy Writes/sec

select * from sys.dm_os_performance_counters where counter_name = 'Lazy writes/sec';

21、Always on 状态

select a.name, b.database_state, b.database_state_desc from sys.databases as a, sys.dm_hadr_database_replica_states as b where a.database_id = b.database_id and b.is_local=1;

22、慢查询

步骤一、---先清除sql server的缓存

dbcc freeProcCache

SELECT creation_time N'语句编译时间'

,last_execution_time N'上次执行时间'

,total_physical_reads N'物理读取总次数'

,total_logical_reads/execution_count N'每次逻辑读次数'

,total_logical_reads N'逻辑读取总次数'

,total_logical_writes N'逻辑写入总次数'

,execution_count N'执行次数'

,total_worker_time/1000 N'所用的CPU总时间ms'

,total_elapsed_time/1000 N'总花费时间ms'

,(total_elapsed_time / execution_count)/1000 N'平均时间ms'

,SUBSTRING(st.text, (qs.statement_start_offset/2) + 1,

((CASE statement_end_offset

WHEN -1 THEN DATALENGTH(st.text)

ELSE qs.statement_end_offset END

  • qs.statement_start_offset)/2) + 1) N'执行语句'

FROM sys.dm_exec_query_stats AS qs

CROSS APPLY sys.dm_exec_sql_text(qs.sql_handle) st

where SUBSTRING(st.text, (qs.statement_start_offset/2) + 1,

((CASE statement_end_offset

WHEN -1 THEN DATALENGTH(st.text)

ELSE qs.statement_end_offset END

  • qs.statement_start_offset)/2) + 1) not like '%fetch%'

ORDER BY total_elapsed_time / execution_count DESC;

步骤二、正在执行的慢查询语句

SELECT TOP 1 ST.transaction_id AS TransactionID ,

st.session_id ,

DB_NAME(DT.database_id) AS DatabaseName ,

ses.host_name ,

ses.login_name ,

ses.status,

AT.transaction_begin_time AS TransactionStartTime ,

s.text ,

c.connect_time ,

DATEDIFF(second, AT.transaction_begin_time, GETDATE()) "exec_time(s)" ,

DATEDIFF(minute, AT.transaction_begin_time, GETDATE()) AS Tran_run_time ,

CASE AT.transaction_type

WHEN 1 THEN 'Read/Write Transaction'

WHEN 2 THEN 'Read-Only Transaction'

WHEN 3 THEN 'System Transaction'

WHEN 4 THEN 'Distributed Transaction'

END AS TransactionType ,

CASE AT.transaction_state

WHEN 0 THEN 'Transaction Not Initialized'

WHEN 1 THEN 'Transaction Initialized & Not Started'

WHEN 2 THEN 'Active Transaction'

WHEN 3 THEN 'Transaction Ended'

WHEN 4 THEN 'Distributed Transaction Initiated Commit Process'

WHEN 5 THEN 'Transaction in Prepared State & Waiting Resolution'

WHEN 6 THEN 'Transaction Committed'

WHEN 7 THEN 'Transaction Rolling Back'

WHEN 8 THEN 'Transaction Rolled Back'

END AS TransactionState

FROM sys.dm_tran_session_transactions AS ST

INNER JOIN sys.dm_tran_active_transactions AS AT ON ST.transaction_id = AT.transaction_id

INNER JOIN sys.dm_tran_database_transactions AS DT ON ST.transaction_id = DT.transaction_id

LEFT JOIN sys.dm_exec_connections AS C ON st.session_id = c.session_id

LEFT JOIN sys.dm_exec_sessions AS ses ON c.session_id = ses.session_id

CROSS APPLY sys.dm_exec_sql_text(c.most_recent_sql_Handle) s

WHERE DATEDIFF(second, AT.transaction_begin_time, GETDATE()) > 2

步骤三、排查历史慢查询语句

SELECT TOP 20

[Total IO] = (qs.total_logical_reads + qs.total_logical_writes)

, [Average IO] = (qs.total_logical_reads + qs.total_logical_writes) /

qs.execution_count

, qs.execution_count

, SUBSTRING (qt.text,(qs.statement_start_offset/2) + 1,

((CASE WHEN qs.statement_end_offset = -1

THEN LEN(CONVERT(NVARCHAR(MAX), qt.text)) * 2

ELSE qs.statement_end_offset

END - qs.statement_start_offset)/2) + 1) AS [Individual Query]

, qt.text AS [Parent Query]

, DB_NAME(qt.dbid) AS DatabaseName

, qp.query_plan

FROM sys.dm_exec_query_stats qs

CROSS APPLY sys.dm_exec_sql_text(qs.sql_handle) as qt

CROSS APPLY sys.dm_exec_query_plan(qs.plan_handle) qp

ORDER BY [Average IO] DESC

或者:

use master

-- SELECT * FROM dbo.sysprocesses WHERE spid IN (SELECT blocked FROM dbo.sysprocesses where blocked <> 0);

SELECT

es.session_id,

database_name=DB_NAME(er.database_id),

er.cpu_time,

er.reads,

er.writes,

er.logical_reads,

login_name,

er.status,

blocking_session_id,

wait_type,

wait_resource,

wait_time,

individual_query=SUBSTRING(qt.text,(er.statement_start_offset/2)+1,((CASE WHEN er.statement_end_offset=-1 THEN LEN(CONVERT(NVARCHAR(MAX),qt.text))* 2 ELSE er.statement_end_offset END-er.statement_start_offset)/2)+1),

parent_query=qt.text,

program_name,

host_name,

nt_domain,

start_time,

DATEDIFF(MS,er.start_time,GETDATE())as duration,

(SELECT query_plan FROM sys.dm_exec_query_plan (er.plan_handle))AS query_plan

FROM

sys.dm_exec_requests er

INNER JOIN sys.dm_exec_sessions es ON er.session_id=es.session_id

CROSS APPLY sys.dm_exec_sql_text (er.sql_handle)AS qt

WHERE

es.session_id> 50

AND es.session_Id NOT IN(@@SPID)

ORDER BY

1, 2

说明:

logical_reads:逻辑读,衡量语句的执行开销。如果大于10w,说明此语句开销很大。可以检查下索引是否合理

status:进程的状态。running 表示正在运行,sleeping 表示处于睡眠中,未运行任何语句,suspend 表示等待,runnable 等待cpu 调度

blocking_session_id: 如果不为0,例如 60 。表示52号进程正在被60阻塞。50 进程必须等待60执行完成,才能执行下面的语句

host_name :发出请求的服务器名

program_name:发出请求的应用程序名

duration: 请求的执行时间

23、最耗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])='DB_name' -- DB_name 根据自己写

ORDER BY [cpu_time] DESC


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

24、查看具体的CPU耗时SQL

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

25、查看CPU数和user scheduler数和最大工作线程数,检查worker是否用完也可以排查CPU占用情况

--查看CPU数和user scheduler数目

SELECT cpu_count,scheduler_count FROM sys.dm_os_sys_info

--查看最大工作线程数

SELECT max_workers_count FROM sys.dm_os_sys_info

26、查看CPU占用高的语句

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;

27、查询正在执行的语句

SELECT [Spid] = session_Id, ecid, [Database] = DB_NAME(sp.dbid),

[User] = nt_username, [Status] = er.status,

[Wait] = wait_type,

[Individual Query] = SUBSTRING(qt.text, er.statement_start_offset / 2, (CASE WHEN er.statement_end_offset = - 1 THEN LEN(CONVERT(NVARCHAR(MAX), qt.text))

* 2 ELSE er.statement_end_offset END - er.statement_start_offset) / 2),

[Parent Query] = qt.text,

Program = program_name, Hostname,

nt_domain, start_time

FROM

sys.dm_exec_requests er INNER JOIN sys.sysprocesses sp ON er.session_id = sp.spid

CROSS APPLY sys.dm_exec_sql_text(er.sql_handle) AS qt

WHERE session_Id > 50 /* Ignore system spids.*/ AND session_Id NOT IN (@@SPID)

或者用下面的SQL语句排查:

SELECT [Spid] = session_id ,

ecid ,

[Database] = DB_NAME(sp.dbid) ,

[User] = nt_username ,

[Status] = er.status ,

[Wait] = wait_type ,

[Individual Query] = SUBSTRING(qt.text,

er.statement_start_offset / 2,

( CASE WHEN er.statement_end_offset = -1

THEN LEN(CONVERT(NVARCHAR(MAX), qt.text))

* 2

ELSE er.statement_end_offset

END - er.statement_start_offset )

/ 2) ,

[Parent Query] = qt.text ,

Program = program_name ,

hostname ,

nt_domain ,

start_time

FROM sys.dm_exec_requests er

INNER JOIN sys.sysprocesses sp ON er.session_id = sp.spid

CROSS APPLY sys.dm_exec_sql_text(er.sql_handle) AS qt

WHERE session_id > 50 -- Ignore system spids.

AND session_id NOT IN ( @@SPID ) -- Ignore this current statement.

ORDER BY 1 ,

2;

28、查询数据库连接情况

select client_net_address '客户端IP',local_net_address '服务器的IP',* from sys.dm_exec_connections

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

29、查看连接对象

select client_net_address '客户端IP',local_net_address '服务器的IP',* from sys.dm_exec_connections

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

30、查看索引是否丢失

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;

31、查找持续时间最长的查询

SELECT

DB_NAME(dbid) 'Database Name',

physical_name 'File Location',

NumberReads 'Number of Reads',

BytesRead 'Bytes Read',

NumberWrites 'Number of Writes',

BytesWritten 'Bytes Written',

IoStallReadMS 'IO Stall Read',

IoStallWriteMS 'IO Stall Write',

IoStallMS as 'Total IO Stall (ms)'

FROM

fn_virtualfilestats(NULL,NULL) fs INNER JOIN

sys.master_files mf ON fs.dbid = mf.database_id

AND fs.fileid = mf.file_id

ORDER BY

DB_NAME(dbid);

32、排查连接对象

SELECT [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

ORDER BY [cpu_time] DESC;

33、查看占用较大的SQL语句

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存在等待的SQL查询,执行下面的查询语句就会显示出会话中有多少个worker在等待

SELECT TOP 50

[session_id],

[request_id],

[cpu_time],

[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

34、CPU占用高的SQL

SELECT 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;

35、CPU调度程序在磁盘上等待的查询

SELECT COUNT(*) Schedulers,

AVG(work_queue_count) AS [Avg Work Queue Count],

AVG(pending_disk_io_count) AS [Avg Pending DiskIO Count],

SUM(work_queue_count) AS [SUM Work Queue Count],

SUM(pending_disk_io_count) AS [SUM Pending DiskIO Count]

FROM sys.dm_os_schedulers WITH (NOLOCK)

WHERE scheduler_id < 255;

36、CPU调用过去一小时的详细信息

DECLARE @ms_ticks_now BIGINT

SELECT @ms_ticks_now = ms_ticks

FROM sys.dm_os_sys_info;

SELECT TOP 60 record_id

,dateadd(ms, - 1 * (@ms_ticks_now - [timestamp]), GetDate()) AS EventTime

,[SQLProcess (%)]

,SystemIdle

,100 - SystemIdle - [SQLProcess (%)] AS [OtherProcess (%)]

FROM (

SELECT record.value('(./Record/@id)[1]', 'int') AS record_id

,record.value('(./Record/SchedulerMonitorEvent/SystemHealth/SystemIdle)[1]', 'int') AS SystemIdle

,record.value('(./Record/SchedulerMonitorEvent/SystemHealth/ProcessUtilization)[1]', 'int') AS [SQLProcess (%)]

,TIMESTAMP

FROM (

SELECT TIMESTAMP

,convert(XML, record) AS record

FROM sys.dm_os_ring_buffers

WHERE ring_buffer_type = N'RING_BUFFER_SCHEDULER_MONITOR'

AND record LIKE '%<SystemHealth>%'

) AS x

相关推荐
Python私教1 小时前
model中能定义字段声明不存储到数据库吗
数据库·oracle
BestandW1shEs3 小时前
谈谈Mysql的常见基础问题
数据库·mysql
重生之Java开发工程师4 小时前
MySQL中的CAST类型转换函数
数据库·sql·mysql
教练、我想打篮球4 小时前
66 mysql 的 表自增长锁
数据库·mysql
Ljw...4 小时前
表的操作(MySQL)
数据库·mysql·表的操作
哥谭居民00014 小时前
MySQL的权限管理机制--授权表
数据库
wqq_9922502774 小时前
ssm旅游推荐系统的设计与开发
数据库·旅游
难以触及的高度4 小时前
mysql中between and怎么用
数据库·mysql
Jacky(易小天)5 小时前
MongoDB比较查询操作符中英对照表及实例详解
数据库·mongodb·typescript·比较操作符