--调试语句性能前记得清空执行计划

每次执行需优化SQL前,带上清除缓存的设置SQL。

平常在进行SQL Server性能优化时,为了确保真实还原性能问题,我们需要关闭SQL Server自身的执行计划及缓存。可以通过以下设置清除缓存。

1 DBCC DROPCLEANBUFFERS  --清除缓冲区
2 DBCC FREEPROCCACHE  --删除计划高速缓存中的元素

 

开启查询IO读取统计、查询时间统计。

SET STATISTICS TIME ON --执行时间
SET STATISTICS IO ON --IO读取


--查询当前的事务

select 
t2.session_id as SPID,
t2.transaction_id,
transaction_begin_time,
N'已执行'+ltrim(datediff(mi,transaction_begin_time,getdate()))+N'分钟' as mi,
case transaction_type 
when 1 then N'读/写事务' 
when 2 then N'只读事务' 
when 3 then N'系统事务' 
when 4 then N'分布式事务' end tran_Type,
case transaction_state
when 0 then N'事务尚未完全初始化'
when 1 then N'事务已初始化但尚未启动'
when 2 then N'事务处于活动状态'
when 3 then N'事务已结束。该状态用于只读事务'
when 4 then N'已对分布式事务启动提交进程'
when 5 then N'事务处于准备就绪状态且等待解析'
when 6 then N'事务已提交'
when 7 then N'事务正在被回滚'
when 0 then N'事务已回滚'
end transaction_state,
client_net_address,
client_tcp_port,
program_name,
t2.text

from
sys.dm_tran_active_transactions t1 join (
select 
a.session_id,
transaction_id,
client_net_address,
client_tcp_port,
text,c.program_name
from sys.dm_tran_session_transactions a join (
select session_id,a2.text,client_net_address,client_tcp_port from sys.dm_exec_connections a1
cross apply sys.dm_exec_sql_text(a1.most_recent_sql_Handle) a2
) b on a.session_id=b.session_id
left join sys.dm_exec_sessions c on a.session_id=c.session_id
where is_user_transaction=1
)t2 on t1.transaction_ID=t2.transaction_ID
ORDER BY t2.transaction_id
View Code

相关文章:

  • 2021-06-08
  • 2022-01-24
  • 2022-03-02
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-02-12
  • 2021-12-29
猜你喜欢
  • 2022-02-13
  • 2021-11-29
  • 2022-01-26
  • 2022-02-20
  • 2022-12-23
  • 2022-02-01
  • 2022-01-10
相关资源
相似解决方案