My SQL Toolkit

本文提供了一系列针对 SQL Server 的性能调优技巧,包括如何测量查询耗时、检查数据库锁定状态以及分析活跃会话等关键操作。通过这些实用的 SQL 脚本,可以帮助 DBA 和开发者更有效地诊断并解决数据库性能瓶颈。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

--************************<耗时>***************************************

DBCC DROPCLEANBUFFERS

 

declare @timediff datetime

select @timediff=getdate()

--some statements

select 耗时=datediff(ms,@timediff,getdate())

 

 

--************************<锁定>****************************************

select name,snapshot_isolation_state,snapshot_isolation_state_desc,is_read_committed_snapshot_on

from sys.databases

sp_lock

select * FROM sys.dm_tran_locks where resource_database_id=db_id() and resource_type in ('Object')

select * from sys.dm_exec_sessions where session_id>50

select * from sys.sysprocesses where blocked<>0

select * from sys.sysprocesses where dbid=db_id()

--and hostname=<hostname> 

select a.*,b.blocking_session_id

from sys.dm_tran_locks a,sys.dm_os_waiting_tasks b where a.lock_owner_address=b.resource_address

 

SELECT spid=request_session_id,restype=resource_type,

dbname=DB_NAME(resource_database_id),objid=resource_associated_entity_id,

objname=OBJECT_NAME(case when  resource_associated_entity_id<=2147483647  then resource_associated_entity_id else 0 end),

resdescrip=resource_description,

request_mode,request_type,request_status 

FROM sys.dm_tran_locks

WHERE resource_database_id = DB_ID()

--and  request_session_id=<spid>

--and resource_type in ('Object')

order by request_session_id,resource_type

 

SELECT spid=request_session_id,restype=resource_type,

dbname=DB_NAME(resource_database_id),objid=resource_associated_entity_id,

objname=OBJECT_NAME(case when  resource_associated_entity_id<=2147483647  then resource_associated_entity_id else 0 end),

锁数=count(1),request_mode,request_type,request_status 

FROM sys.dm_tran_locks

WHERE resource_database_id = DB_ID()

--and  request_session_id=<spid>

group by request_session_id,resource_type,resource_database_id,

resource_associated_entity_id,request_mode,request_type,request_status

order by request_session_id,resource_type

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值