我正在使用 Oracle 11gr2 数据库。有没有一种方法可以检查某个特定查询执行了多少次以及当前日期或过去 24 小时内该执行的总运行时间是多少?它就像 sql 运行时间的历史记录。请提出您的建议。
最佳答案
select sql_text, sql_fulltext, executions, elapsed_time/1000000 seconds
from gv$sql
order by executions desc;
这只会返回尚未在共享池中过期的查询,并且只提供累积运行时间。但是,如果您只对最常见的查询感兴趣,那么它们很可能是最近使用过的,并且不会过时。
AWR 报告或 DBA_HIST_* 表之一也可能有帮助。如果您有网格控制,您可以非常轻松地选择 24 小时期间并生成显示热门查询的报告。
更新:
我认为没有任何方法可以获取每个单独执行的统计信息。历史性能 View 通过采样来工作,它们并不能捕获所有内容。实际上,这对于性能调整来说已经足够了。如果某件事发生的频率不足以进行采样,则不值得进行调整。
AWR 可用于查找每天的大致执行次数:
--Number of Executions today.
select
--Executions at last snap.
(
select executions_total
from dba_hist_sqlstat
where snap_id =
(
--Latest snapshot.
select max(snap_id) keep
(dense_rank last order by begin_interval_time) end_snap_id
from dba_hist_snapshot
)
and sql_id = '5ms6rbzdnq16t'
)
-
--Executions at beginning of day.
(
select executions_total
from dba_hist_sqlstat
where snap_id =
(
--Snapshot for beginning of the day.
--Assumes there are hourly snapshots that start on time.
select snap_id begin_snap_id
from dba_hist_snapshot
where trunc(begin_interval_time, 'hh') = trunc(sysdate)
)
and sql_id = '5ms6rbzdnq16t'
) executions_today
from dual;
关于performance - 如何检查查询执行的次数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20787787/