如何检查Oracle数据库中长时间运行的查询


98

我的使用Oracle数据库的应用程序运行缓慢,或者似乎已完全停止。

如何找出最昂贵的查询,以便进一步调查?

Answers:


134

此图显示了当前处于“活动”状态的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
/

1
如果它们运行了x分钟以上,是否有办法安全地终止此类查询。尽管感谢您的回答@UmberFerrule
TommyT 2015年

2
@TommyT您可以使用alter system kill session如下所述:docs.oracle.com/cd/B28359_01/server.111/b28310/...
WW。

37

尝试此操作,它将使您当前运行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

我运行此查询,它告诉我一个无效的语句

有效。我测试了 您使用什么工具查询?它可能会与#符号混淆。尝试像这样更改开头和结尾:“从sid,q.piece的顺序中选择* *”
Carlos A. Ibarra

2
另外,您需要使用有权访问v $ session,v $ sqltext_with_newlines
WW的

此方法有效,但返回格式非常奇怪的查询的SQL文本。
伯恩哈德·多布勒


4
Step 1:Execute the query

column username format 'a10'
column osuser format 'a10'
column module format 'a16'
column program_name format 'a20'
column program format 'a20'
column machine format 'a20'
column action format 'a20'
column sid format '9999'
column serial# format '99999'
column spid format '99999'
set linesize 200
set pagesize 30
select
a.sid,a.serial#,a.username,a.osuser,c.start_time,
b.spid,a.status,a.machine,
a.action,a.module,a.program
from
v$session a, v$process b, v$transaction c,
v$sqlarea s
Where
a.paddr = b.addr
and a.saddr = c.ses_addr
and a.sql_address = s.address (+)
and to_date(c.start_time,'mm/dd/yy hh24:mi:ss') <= sysdate - (15/1440) -- running for 15 minutes
order by c.start_time
/   

Step 2: desc v$session

Step 3:select sid, serial#,SQL_ADDRESS, status,PREV_SQL_ADDR from v$session where sid='xxxx' //(enter the sid value)

Step 4: select sql_text from v$sqltext where address='XXXXXXXX';

Step 5: select piece, sql_text from v$sqltext where address='XXXXXX' order by piece;

1

您可以从数据库生成AWR(自动工作负载存储库)报告。

从SQL * Plus命令行运行:

SQL> @$ORACLE_HOME/rdbms/admin/awrrpt.sql

阅读与如何生成和理解AWR报告有关的文档。它将提供数据库性能和资源问题的完整视图。一旦我们熟悉了AWR报告,将有助于找到消耗资源的Top SQL。

另外,在12C EM Express UI中,我们可以生成AWR。


0

您可以使用以下查询来检查长时间运行的查询的详细信息,例如已完成百分比和剩余时间:

 SELECT SID, SERIAL#, OPNAME, CONTEXT, SOFAR, 
 TOTALWORK,ROUND(SOFAR/TOTALWORK*100,2) "%_COMPLETE" 
 FROM V$SESSION_LONGOPS 
 WHERE OPNAME NOT LIKE '%aggregate%' 
       AND TOTALWORK != 0 
       AND SOFAR <> TOTALWORK;

有关故障排除步骤的完整列表,您可以在此处查看:对长时间运行的会话进行故障排除


0
select sq.PARSING_SCHEMA_NAME, sq.LAST_LOAD_TIME, sq.ELAPSED_TIME, sq.ROWS_PROCESSED, ltrim(sq.sql_text), sq.SQL_FULLTEXT
  from v$sql sq, v$session se
 order by sq.ELAPSED_TIME desc, sq.LAST_LOAD_TIME desc;
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.