涉及视图
v$session
v$lock
v$session_wait
v$locked_object
v$sql
dba_objects
gv$active_session_history
手动锁表

  1. SELECT employee_id, salary, commission_pct, job_id
  2. FROM employees
  3. WHERE job_id = 'SA_REP'
  4. FOR UPDATE WAIT 5
  5. ORDER BY employee_id;

1.查看被锁住的对象

  1. select b.owner,b.object_name,a.session_id,a.locked_mode from v$locked_object a,dba_objects b where b.object_id = a.object_id;

2.查看被阻塞的会话

  1. SELECT SID,SERIAL#,USERNAME FROM V$SESSION WHERE SID IN (SELECT BLOCKING_SESSION FROM V$SESSION );

3.结束会话

  1. alter system kill session '267,763' immediate;
  2. alter system kill session '267,763,@1' immediate; 集群kill方式

4.查看具体阻塞sql会话信息

  1. SELECT b.sid,
  2. a.sql_id,
  3. a.sql_text,
  4. a.hash_value,
  5. b.username,
  6. b.machine,
  7. a.module,
  8. c.block,
  9. c.request,
  10. d.seconds_in_wait
  11. FROM v$sql a, v$session b, v$lock c, v$session_wait d
  12. WHERE c.type = 'TX'
  13. AND a.sql_id = b.sql_id
  14. AND b.sid = c.sid
  15. AND d.sid = b.sid
  16. UNION ALL
  17. SELECT b.sid,
  18. a.sql_id,
  19. a.sql_text,
  20. a.hash_value,
  21. b.username,
  22. b.machine,
  23. a.module,
  24. c.block,
  25. c.request,
  26. d.seconds_in_wait
  27. FROM v$sql a, v$session b, v$lock c, v$session_wait d
  28. WHERE c.type = 'TX'
  29. AND a.sql_id = b.prev_sql_id
  30. AND b.sid = c.sid
  31. AND d.sid = b.sid
  32. AND c.block = 1
  33. ----------------------------------集群查询-------------------------------------
  34. SELECT b.inst_id,
  35. b.sid,
  36. b.serial#,
  37. a.sql_id,
  38. a.sql_text,
  39. a.hash_value,
  40. b.username,
  41. b.machine,
  42. a.module,
  43. c.block,
  44. c.request,
  45. d.seconds_in_wait
  46. FROM gv$sql a, gv$session b, gv$lock c, gv$session_wait d
  47. WHERE c.type = 'TX'
  48. AND a.sql_id = b.sql_id
  49. AND b.sid = c.sid
  50. AND d.sid = b.sid
  51. UNION ALL
  52. SELECT
  53. b.inst_id,b.sid,
  54. b.serial#,
  55. a.sql_id,
  56. a.sql_text,
  57. a.hash_value,
  58. b.username,
  59. b.machine,
  60. a.module,
  61. c.block,
  62. c.request,
  63. d.seconds_in_wait
  64. FROM gv$sql a, gv$session b, gv$lock c, gv$session_wait d
  65. WHERE c.type = 'TX'
  66. AND a.sql_id = b.prev_sql_id
  67. AND b.sid = c.sid
  68. AND d.sid = b.sid
  69. AND c.block = 1;

5.查询锁住对象及关联sql

  1. SELECT A.OWNER 方案名,
  2. A.OBJECT_NAME 表名,
  3. B.XIDUSN 回滚段号,
  4. B.XIDSLOT 槽号,
  5. B.XIDSQN 序列号,
  6. B.SESSION_ID 锁表SESSION_ID,
  7. B.ORACLE_USERNAME 锁表用户名,
  8. decode(D.type,
  9. 'XR',
  10. 'NULL',
  11. 'RS',
  12. 'SS(Row-S)',
  13. 'CF',
  14. 'SS(Row-S)',
  15. 'TM',
  16. 'TABLE LOCK',
  17. 'PW',
  18. 'TABLE LOCK',
  19. 'TO',
  20. 'TABLE LOCK',
  21. 'TS',
  22. 'TABLE LOCK',
  23. 'RT',
  24. 'ROW LOCK',
  25. 'TX',
  26. 'ROW LOCK',
  27. 'MR',
  28. 'S(Share)',
  29. NULL) 锁定方式,
  30. C.MACHINE 用户组,
  31. C.TERMINAL 机器名,
  32. B.OS_USER_NAME 系统用户名,
  33. B.PROCESS 系统进程id,
  34. DECODE(C.STATUS, 'INACTIVE', '不活动', 'ACTIVE', '活动') 活动情况,
  35. C.SERVER,
  36. C.SID,
  37. C.SERIAL#,
  38. C.PROGRAM 连接方式,
  39. C.LOGON_TIME,
  40. s.sql_text
  41. FROM ALL_OBJECTS A, V$LOCKED_OBJECT B, SYS.GV_$SESSION C, v$lock d,v$sql s
  42. WHERE( (A.OBJECT_ID = B.OBJECT_ID)
  43. AND (B.PROCESS = C.PROCESS)
  44. and C.sid = d.sid
  45. and B.LOCKED_MODE = D.LMODE)
  46. and c.prev_sql_id=s.sql_id
  47. ORDER BY 1, 2;
  48. -------------------------------------集群回话查询--------------------------------
  49. SELECT A.OWNER 方案名,
  50. A.OBJECT_NAME 表名,
  51. B.XIDUSN 回滚段号,
  52. B.XIDSLOT 槽号,
  53. B.XIDSQN 序列号,
  54. B.SESSION_ID 锁表SESSION_ID,
  55. C.inst_id,
  56. C.serial#,
  57. C.sid,
  58. B.ORACLE_USERNAME 锁表用户名,
  59. decode(D.type,
  60. 'XR',
  61. 'NULL',
  62. 'RS',
  63. 'SS(Row-S)',
  64. 'CF',
  65. 'SS(Row-S)',
  66. 'TM',
  67. 'TABLE LOCK',
  68. 'PW',
  69. 'TABLE LOCK',
  70. 'TO',
  71. 'TABLE LOCK',
  72. 'TS',
  73. 'TABLE LOCK',
  74. 'RT',
  75. 'ROW LOCK',
  76. 'TX',
  77. 'ROW LOCK',
  78. 'MR',
  79. 'S(Share)',
  80. NULL) 锁定方式,
  81. C.MACHINE 用户组,
  82. C.TERMINAL 机器名,
  83. B.OS_USER_NAME 系统用户名,
  84. B.PROCESS 系统进程id,
  85. DECODE(C.STATUS, 'INACTIVE', '不活动', 'ACTIVE', '活动') 活动情况,
  86. C.SERVER,
  87. C.SID,
  88. C.SERIAL#,
  89. C.PROGRAM 连接方式,
  90. C.LOGON_TIME,
  91. s.sql_text
  92. FROM ALL_OBJECTS A, gV$LOCKED_OBJECT B, SYS.GV_$SESSION C, gv$lock d,gv$sql s
  93. WHERE( (A.OBJECT_ID = B.OBJECT_ID)
  94. AND (B.PROCESS = C.PROCESS)
  95. and C.sid = d.sid
  96. and B.LOCKED_MODE = D.LMODE)
  97. and c.prev_sql_id=s.sql_id
  98. ORDER BY 1, 2;

6.查看历史锁

  1. select SAMPLE_TIME,session_id,sql_id,event,p1,BLOCKING_SESSION from gv$active_session_history where event = 'enq: TX - row lock contention'
  2. and sample_time >to_timestamp('2021-11-02 14:00:00','yyyy-mm-dd hh24:mi:ss')
  3. and sample_time <to_timestamp('2021-11-02 16:00:00','yyyy-mm-dd hh24:mi:ss')