查找运行时间超过 5 秒的查询
我的朋友要求我在他的 Oracle 数据库上查找长时间运行的查询(超过 5 秒)。他想在定期间隔后进行某种轮询,并想向自己发送警报,以便他知道哪个查询需要很长时间才能执行并向他发送查询和相应的会话。
我写了这个 Oracle 查询:
select sess.sid,
sess.username,
sess.paddr,
sess.machine,
optimizer_mode,
sess.schemaname,
hash_value,
address,
sess.sql_address,
cpu_time,
elapsed_time,
sql_text
from v$sql sql, v$session sess
where
sess.sql_hash_value = sql.hash_value
and sess.sql_address = sql.address
and sess.username is not null
and elapsed_time > 1000000 * 5
order by
cpu_time desc
但他说,当他手动运行查询并计算时间时,执行查询所花费的时间只是他从该特定查询生成的表中获得的结果的一小部分。
我想知道我的查询是否错误,我已经做了一些搜索,但查询似乎仍然很好。
数据库是Oracle 10g
建议???
I was asked by my friend to find long running queries (more than 5 second) on his oracle database. He wanted to do some sort of polling after a periodic interval and wanted to send himself an alert so that he knows which query are taking so long to execute and send query and corresponding session to him.
I wrote this Oracle query :
select sess.sid,
sess.username,
sess.paddr,
sess.machine,
optimizer_mode,
sess.schemaname,
hash_value,
address,
sess.sql_address,
cpu_time,
elapsed_time,
sql_text
from v$sql sql, v$session sess
where
sess.sql_hash_value = sql.hash_value
and sess.sql_address = sql.address
and sess.username is not null
and elapsed_time > 1000000 * 5
order by
cpu_time desc
But he says that when he runs a query manually and calculates the time, the time it spends in executing it is a fraction of the result he is getting from the table generated by this particular query.
I wonder if my query is wrong , I have done some search but it still seems the query is fine.
Database is Oracle 10g
Suggestions???
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
ELAPSED_TIME 是SQL 语句所有运行时间的累计时间。因此对于频繁执行的查询来说它会很高。
将此视为一个快速查询(HINT 样式注释的目的是获取 V$SQLAREA 中的 SQL_ID):
运行需要多长时间?这么长:
由于解析时间的原因,这是一个相对较大的微秒块。我们可以看到,再运行几次并不会增加太多的运行时间,而且平均值要低得多:
再运行 100000 次后...
现在,您想要的是找到已运行的活动会话连续做某事超过五秒钟。因此,您需要会话级计时,特别是 V$SESSION 上的 LAST_CALL_ET,它是会话执行某项操作的秒数(如果其状态为 ACTIVE)或自上次操作以来的总运行时间(如果其状态为 ACTIVE)处于非活动状态)。
因此,考虑这个查询。它很慢:
这足以使用 V$SESSION 上的查询进行监控。该语句搜索运行时间超过 3 秒的语句...
瞧!
了语句执行的最近时间。但是,没有视图可以公开语句的每个单独执行的指标。如果您想要此类详细信息,则需要开始这是一个单独的问题。
ELAPSED_TIME is the accumulated time for all the times that SQL statement has been run. So it will be high for frequently executed queries.
Consider this a snappy query (the HINT-style comment is for the purposes of getting the SQL_ID in V$SQLAREA):
How long does it take to run? This long:
That's a relatively large chunk o' microsecs, because of the parse time. We can see that running it a couple more times doesn't add much to the elapsed time, and the average is much lower:
And after running it another 100000 times ...
Now, what you wantt is to find active sessions which have been doing something continuously for more than five seconds. So you need session-level timings, and in particular the LAST_CALL_ET on V$SESSION, which is the number of seconds the session has been doing something (if its status is ACTIVE) or the total elapsed time since its last action (if its status is INACTIVE).
So, consider this query. It's slow:
That's long enough to monitor using a query on V$SESSION. This one searches for statements which have been running for more than 3 seconds...
and lo!
The LAST_ACTIVE_TIME on the V$SQLAREA view records the most recent time the statement was executed. However, there is no view which exposes the metrics for each individual execution of the statement. If you want that sort of detail you will need to start tracing. And that is a separate question.
使用追踪:
Use the tracing: