堵塞、锁等待以及死锁是在数据库使用中常见的现象,简单的堵塞可以通过查询字典视图去定位锁头,且这个时候的锁头会话大概率都是active会话,清理它就可以了。
但是在日常生产中,经常会遇到一堆会话堵着堵着堵成一堆乱麻的情况,就像高速堵车一样,这个时候锁头很可能是一段时间之前的事务了,且很可能就是个idle会话。
这个时候最重要的就是分析并找出锁头,就像捋线头一样,然后通过锁头的事务号去分析并找出问题,然后跟客户确认是否清理锁头会话。
1)确认是否有锁或者线程等待
--单机
select * from v$lock where blocked =1;
select * from v$trxwait order by wait_time desc;
--DSC
select * from v$dsc_lock where blocked =1;
select * from v$dsc_trxwait order by wait_time desc;
2)确定锁头事务号
--单机
select distinct(wait_trx_id) from v$trx_wait not in
(select trx_id from v$trx_wait);
--DSC
select distinct(wait_trx_id) from v$dsc_trx_wait not in
(select trx_id from v$dsc_trx_wait);
3)找到锁头事务号后就好分析了,这个时候有如下两个选择:
一、通过事务ID到会话视图中分析会话信息
--单机
select * from v$sessions where trx_id in
(select distinct(wait_trx_id) from v$trx_wait not in
(select trx_id from v$trx_wait));
--DSC
select * from v$sessions where trx_id in
(select distinct(wait_trx_id) from v$dsc_trx_wait not in
(select trx_id from v$dsc_trx_wait));
二、根据事务ID到收集到的sqllog日志中grep取出所有与其相关的事务信息,确认造成死锁的原因。
文章
阅读量
获赞