锁表问题

本贴最后更新于 2782 天前,其中的信息可能已经时移世改

几种锁表情况



解决方法

查看表空间使用率

SELECT b.file_id 文件ID号,
b.tablespace_name 表空间名,
b.bytes/1024/1024||'M'字节数,
(b.bytes-sum(nvl(a.bytes,0)))/1024/1024||'M' 已使用,
SUM(nvl(a.bytes,0))/1024/1024||'M' 剩余空间,
100 - sum(nvl(a.bytes,0))/(b.bytes)*100 占用百分比,
c.Contents 内容 
from dba_free_space a,dba_data_files b,dba_tablespaces c 
WHERE a.file_id=b.file_id AND a.tablespace_name=c.tablespace_name 
group by b.tablespace_name,b.file_id,b.bytes,c.Contents 
ORDER by b.file_id;

查找当前表级锁

select sess.sid, 
    sess.serial#, 
    lo.oracle_username, 
    lo.os_user_name, 
    ao.object_name, 
    lo.locked_mode 
    from v$locked_object lo, 
    dba_objects ao, 
    v$session sess 
where ao.object_id = lo.object_id and lo.session_id = sess.sid;

杀掉锁表进程

alter system kill session '436,35123';

监控当前数据库谁在运行什么 SQL 语句

select osuser, username, sql_text  
from  v$session a, v$sqltext b 
where  a.sql_address =b.address order by address, piece;

找使用 CPU 多的用户 session

select a.sid,spid,status,substr(a.program,1,40) prog, a.terminal,osuser,value/60/100 value 
from  v$session a,v$process b,v$sesstat c 
where  c.statistic#=12 and  
       c.sid=a.sid and  
       a.paddr=b.addr  
       order by value desc;

查看死锁信息

SELECT (SELECT username
          FROM v$session
         WHERE SID = a.SID) blocker, a.SID, 'is blocking',
       (SELECT username
          FROM v$session
         WHERE SID = b.SID) blockee, b.SID
  FROM v$lock a, v$lock b
 WHERE a.BLOCK = 1 AND b.request > 0 AND a.id1 = b.id1 AND a.id2 = b.id2;

具有最高等待的对象

SELECT   o.OWNER,o.object_name, o.object_type, a.event,
         SUM (a.wait_time + a.time_waited) total_wait_time
    FROM v$active_session_history a, dba_objects o
   WHERE a.sample_time BETWEEN SYSDATE - 30 / 2880 AND SYSDATE
     AND a.current_obj# = o.object_id
GROUP BY o.OWNER,o.object_name, o.object_type, a.event
ORDER BY total_wait_time DESC;

SELECT   a.session_id, s.osuser, s.machine, s.program, o.owner, o.object_name,
         o.object_type, a.event,
         SUM (a.wait_time + a.time_waited) total_wait_time
    FROM v$active_session_history a, dba_objects o, v$session s
   WHERE a.sample_time BETWEEN SYSDATE - 30 / 2880 AND SYSDATE
     AND a.current_obj# = o.object_id
     AND a.session_id = s.SID
GROUP BY o.owner,
         o.object_name,
         o.object_type,
         a.event,
         a.session_id,
         s.program,
         s.machine,
         s.osuser
ORDER BY total_wait_time DESC;

查询当前连接会话数

select s.value,s.sid,a.username
from 
v$sesstat S,v$statname N,v$session A
where 
n.statistic#=s.statistic# and
name='session pga memory'
and s.sid=a.sid
order by s.value;

等待最多的用户

SELECT   s.SID, s.username, SUM (a.wait_time + a.time_waited) total_wait_time
    FROM v$active_session_history a, v$session s
   WHERE a.sample_time BETWEEN SYSDATE - 30 / 2880 AND SYSDATE
GROUP BY s.SID, s.username
ORDER BY total_wait_time DESC;

等待最多的 SQL

SELECT   a.program, a.session_id, a.user_id, d.username, s.sql_text,
         SUM (a.wait_time + a.time_waited) total_wait_time
    FROM v$active_session_history a, v$sqlarea s, dba_users d
   WHERE a.sample_time BETWEEN SYSDATE - 30 / 2880 AND SYSDATE
     AND a.sql_id = s.sql_id
     AND a.user_id = d.user_id
GROUP BY a.program, a.session_id, a.user_id, s.sql_text, d.username;

查看消耗资源最多的 SQL

SELECT hash_value, executions, buffer_gets, disk_reads, parse_calls
FROM V$SQLAREA
WHERE buffer_gets > 10000000 OR disk_reads > 1000000
ORDER BY buffer_gets + 100 * disk_reads DESC;

查看某条 SQL 语句的资源消耗

SELECT hash_value, buffer_gets, disk_reads, executions, parse_calls
FROM V$SQLAREA
WHERE hash_Value = 228801498 AND address = hextoraw('CBD8E4B0');

查询会话执行的实际 SQL

SELECT   a.SID, a.username, s.sql_text
    FROM v$session a, v$sqltext s
   WHERE a.sql_address = s.address
     AND a.sql_hash_value = s.hash_value
     AND a.status = 'ACTIVE'
ORDER BY a.username, a.SID, s.piece;

显示正在等待锁的所有会话

SELECT * FROM DBA_WAITERS;
  • 数据库

    据说 99% 的性能瓶颈都在数据库。

    343 引用 • 723 回帖

相关帖子

回帖

欢迎来到这里!

我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。

注册 关于
请输入回帖内容 ...