侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

Oracle阻塞(blockingblocked)实例详解

数据库  /  管理员 发布于 6年前   169

一、概述:

阻塞是DBA经常碰到的情形,尤其是不良的应用程序设计所造成的阻塞将导致数据库性能的严重下降,直至数据库崩溃。对DBA而言,有必要知道如何定位到当前系统有哪些阻塞,到底谁是阻塞者,谁是被阻塞者。本文对此给出了描述并做了相关演示。

二、演示阻塞:

--更新表,注,提示符scott@CNMMBO表明用户为scott的session,用户名不同,session不同。scott@CNMMBO> update emp set sal=sal*1.1 where empno=7788;1 row updated.scott@CNMMBO> @my_env SPID        SID  SERIAL# USERNAME    PROGRAM------------ ---------- ---------- --------------- ------------------------------------------------11205       1073    4642 robin      oracle@SZDB (TNS V1-V3) --另起两个session更新同样的行,这两个session都会处于等待,直到第一个session提交或回滚leshami@CNMMBO> update scott.emp set sal=sal+100 where empno=7788; goex_admin@CNMMBO> update scott.emp set sal=sal-50 where empno=7788; --下面在第一个session 查询阻塞情况scott@CNMMBO> @blocker  BLOCK_MSGBLOCK-------------------------------------------------- ----------pts/5 ('1073,4642') is blocking 1067,10438         1pts/5 ('1073,4642') is blocking 1065,4464          1--上面的结果表明session 1073,4642 阻塞了后面的2个--即session 1073,4642是阻塞者,后面2个session是被阻塞者 --Author : Leshami--Blog  : http://blog.csdn.net/leshami --下面查询正在阻塞的session id,SQL语句以及被阻塞的时间scott@CNMMBO> @blocking_session_detail.sql 'SID='||A.SID||'WAITCLASS='||A.WAIT_CLASS||'TIME='||A.SECONDS_IN_WAIT||CHR(10)||'QUERY='||B.SQL_TEXT------------------------------------------------------------------------sid=1067 Wait Class=Application Time=5995 Query=update scott.emp set sal=sal+100 where empno=7788 sid=1065 Wait Class=Application Time=225 Query=update scott.emp set sal=sal-50 where empno=7788 --下面的查询阻塞时锁的持有情况 scott@CNMMBO> @request_lock_type USERNAME   SID TY LMODE    REQUEST      ID1    ID2------------------------------ ---------- -- ----------- ----------- ---------- ----------SCOTT    1073 TX Exclusive  None      524319   27412LESHAMI   1067 TX None    Exclusive    524319   27412GOEX_ADMIN  1065 TX None    Exclusive    524319   27412--可以看到LESHAMI,GOEX_ADMIN 2个用户都在请求524319/27412上的Exclusive锁,而此时已经被SCOTT加了Exclusive锁 --查询阻塞时锁的持有详细信息scott@CNMMBO> @request_lock_detail     SID USERNAME       OSUSER     TERMINAL         OBJECT_NAME     TY Lock Mode  Req_Mode---------- -------------------- --------------- ------------------------- -------------------- -- ----------- --------------------   1065 GOEX_ADMIN      robin      pts/1           EMP         TM Row Excl   1065 GOEX_ADMIN      robin      pts/1           Trans-524319     TX --Waiting-- Exclusive   1067 LESHAMI       robin      pts/0           EMP         TM Row Excl   1067 LESHAMI       robin      pts/0           Trans-524319     TX --Waiting-- Exclusive   1073 SCOTT        robin      pts/5           EMP         TM Row Excl   1073 SCOTT        robin      pts/5           Trans-524319     TX Exclusive 

三、文中涉及到的相关SQL脚本完整代码如下:

robin@SZDB:~/dba_scripts/custom/sql> more my_env.sql SELECT spid, s.sid, s.serial#, p.username, p.programFROM v$process p, v$session sWHERE p.addr = s.paddr   AND s.sid = (SELECT sid          FROM v$mystat          WHERE rownum = 1);robin@SZDB:~/dba_scripts/custom/sql> more blocker.sql col block_msg format a50; select c.terminal||' ('''||a.sid||','||c.serial#||''') is blocking '||b.sid||','||d.serial# block_msg, a.block from v$lock a,v$lock b,v$session c,v$session d  where a.id1=b.id1  and a.id2=b.id2  and a.block>0 and a.sid <>b.sid  and a.sid=c.sid  and b.sid=d.SID; robin@SZDB:~/dba_scripts/custom/sql> more blocking_session_detail.sql--To find the query for blocking session--Access Privileges: SELECT on v$session, v$sqlareaSELECT   'sid='     || a.SID     || ' Wait Class='     || a.wait_class     || ' Time='     || a.seconds_in_wait     || CHR (10)     || ' Query='     || b.sql_text  FROM v$session a, v$sqlarea b  WHERE a.blocking_session IS NOT NULL AND a.sql_address = b.addressORDER BY a.blocking_session/robin@SZDB:~/dba_scripts/custom/sql> more request_lock_type.sql--This script generates a report of users waiting for locks.--Access Privileges: SELECT on v$session, v$lockSELECT sn.username, m.sid, m.type,    DECODE(m.lmode, 0, 'None',1, 'Null',2, 'Row Share',3, 'Row Excl.',4, 'Share',5, 'S/Row Excl.',6, 'Exclusive',        lmode, ltrim(to_char(lmode,'990'))) lmode,    DECODE(m.request,0, 'None', 1, 'Null', 2, 'Row Share', 3, 'Row Excl.', 4, 'Share', 5, 'S/Row Excl.', 6, 'Exclusive', request, ltrim(to_char(m.request,        '990'))) request, m.id1, m.id2FROM v$session sn, v$lock mWHERE (sn.sid = m.sid AND m.request != 0)    OR (sn.sid = m.sid        AND m.request = 0 AND lmode != 4        AND (id1, id2) IN (SELECT s.id1, s.id2   FROM v$lock sWHERE request != 0       AND s.id1 = m.id1    AND s.id2 = m.id2)        )ORDER BY id1, id2, m.request; robin@SZDB:~/dba_scripts/custom/sql> more request_lock_detail.sqlset linesize 190col osuser format a15col username format a20 wrapcol object_name format a20 wrapcol terminal format a25 wrapcol Req_Mode format a20select B.SID, C.USERNAME, C.OSUSER, C.TERMINAL,    DECODE(B.ID2, 0, A.OBJECT_NAME,      'Trans-'||to_char(B.ID1)) OBJECT_NAME,   B.TYPE,    DECODE(B.LMODE,0,'--Waiting--',           1,'Null',           2,'Row Share',           3,'Row Excl',          4,'Share',           5,'Sha Row Exc',      6,'Exclusive','Other') "Lock Mode",    DECODE(B.REQUEST,0,' ',           1,'Null',           2,'Row Share',           3,'Row Excl',           4,'Share',           5,'Sha Row Exc',           6,'Exclusive',           'Other') "Req_Mode" from DBA_OBJECTS A, V$LOCK B, V$SESSION Cwhere A.OBJECT_ID(+) = B.ID1 and B.SID = C.SID and C.USERNAME is not nullorder by B.SID, B.ID2;


  • 上一条:
    Oracle中插入特殊字符:&amp;和&#39;的解决方法汇总
    下一条:
    oracle复习笔记之PL/SQL程序所要了解的知识点
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • 分库分表的目的、优缺点及具体实现方式介绍(0个评论)
    • DevDB - 在 VS 代码中直接访问数据库(0个评论)
    • 在ubuntu系统中实现mysql数据存储目录迁移流程步骤(0个评论)
    • 在mysql中使用存储过程批量新增测试数据流程步骤(0个评论)
    • php+mysql数据库批量根据条件快速更新、连表更新sql实现(0个评论)
    • 近期文章
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf文件功能(0个评论)
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • 在go + gin中gorm实现指定搜索/区间搜索分页列表功能接口实例(0个评论)
    • 在go语言中实现IP/CIDR的ip和netmask互转及IP段形式互转及ip是否存在IP/CIDR(0个评论)
    • PHP 8.4 Alpha 1现已发布!(0个评论)
    • Laravel 11.15版本发布 - Eloquent Builder中添加的泛型(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2017-06
    • 2017-08
    • 2017-09
    • 2017-10
    • 2017-11
    • 2018-01
    • 2018-05
    • 2018-10
    • 2018-11
    • 2020-02
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2020-07
    • 2020-08
    • 2020-09
    • 2021-02
    • 2021-04
    • 2021-07
    • 2021-08
    • 2021-11
    • 2021-12
    • 2022-02
    • 2022-03
    • 2022-05
    • 2022-06
    • 2022-07
    • 2022-08
    • 2022-09
    • 2022-10
    • 2022-11
    • 2022-12
    • 2023-01
    • 2023-03
    • 2023-04
    • 2023-05
    • 2023-07
    • 2023-08
    • 2023-10
    • 2023-11
    • 2023-12
    • 2024-01
    • 2024-03
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客