我的应用程序使用Oracle数据库,速度很慢或似乎已完全停止.
如何找出最昂贵的查询,以便进一步调查?
这个显示当前"活动"的SQL: -
select S.USERNAME, s.sid, s.osuser, t.sql_id, sql_text from v$sqltext_with_newlines t,V$SESSION s where t.address =s.sql_address and t.hash_value = s.sql_hash_value and s.status = 'ACTIVE' and s.username <> 'SYSTEM' order by s.sid,t.piece /
这显示了锁.有时事情变得缓慢,但这是因为它被阻止等待锁定:
select object_name, object_type, session_id, type, -- Type or system/user lock lmode, -- lock mode in which session holds lock request, block, ctime -- Time since current mode was granted from v$locked_object, all_objects, v$lock where v$locked_object.object_id = all_objects.object_id AND v$lock.id1 = all_objects.object_id AND v$lock.sid = v$locked_object.session_id order by session_id, ctime desc, object_name /
这对于查找长操作(例如全表扫描)来说是一个很好的选择.如果是因为大量的短期操作,那么什么都不会出现.
COLUMN percent FORMAT 999.99 SELECT sid, to_char(start_time,'hh24:mi:ss') stime, message,( sofar/totalwork)* 100 percent FROM v$session_longops WHERE sofar/totalwork < 1 /
试试这个,它会给你当前运行超过60秒的查询.请注意,如果SQL有多行,它会为每个运行的查询打印多行.看看sid,serial#来看看属于哪里.
select s.username,s.sid,s.serial#,s.last_call_et/60 mins_running,q.sql_text from v$session s join v$sqltext_with_newlines q on s.sql_address = q.address where status='ACTIVE' and type <>'BACKGROUND' and last_call_et> 60 order by sid,serial#,q.piece
V $ SESSION_LONGOPS
如果你寻找sofar!= totalwork,你会看到那些尚未完成的,但是当操作完成时不会删除这些条目,所以你也可以在那里看到很多历史.