我们在我们的一个应用程序中使用以下 SQL 查询来获取过去 24 小时内最高的 cpu 查询。但不幸的是,我们在执行此查询时发现内存消耗很大。您能否帮助我了解导致问题的真正原因。如果可能的话,请帮助我们进行更优化的查询,该查询占用的内存非常少。
SELECT
*
FROM
(
SELECT
ss.sql_text,
a.sql_id,
SUM(cpu_time_delta),
SUM(disk_reads_delta),
COUNT(*)
FROM
dba_hist_sqlstat a,
dba_hist_snapshot s,
v$sql ss
WHERE
s.snap_id = a.snap_id
AND a.sql_id = ss.sql_id
AND s.begin_interval_time > sysdate - 1
GROUP BY
ss.sql_text,
a.sql_id
ORDER BY
SUM(cpu_time_delta) DESC
)
WHERE
ROWNUM < 20;
我们尝试使用以下查询来查看性能改进,但不幸的是没有帮助
SELECT
ss.sql_text,
a.sql_id,
SUM(cpu_time_delta),
SUM(disk_reads_delta),
COUNT(*)
FROM
dba_hist_sqlstat a,
dba_hist_snapshot s,
v$sql ss
WHERE
a.sql_id IS NOT NULL
AND s.snap_id = a.snap_id
AND a.sql_id = ss.sql_id
AND s.begin_interval_time > sysdate - 1
GROUP BY
ss.sql_text,
a.sql_id
ORDER BY
SUM(cpu_time_delta) DESC
FETCH FIRST 20 ROWS ONLY;
SELECT
ss.sql_text,
a.sql_id,
SUM(cpu_time_delta),
SUM(disk_reads_delta),
COUNT(*)
FROM
dba_hist_sqlstat a,
(SELECT snap_id FROM dba_hist_snapshot WHERE begin_interval_time > sysdate - 1) s,
v$sql ss
WHERE
a.sql_id IS NOT NULL
AND s.snap_id = a.snap_id
AND a.sql_id = ss.sql_id
GROUP BY
ss.sql_text,
a.sql_id
ORDER BY
SUM(cpu_time_delta) DESC
FETCH FIRST 20 ROWS ONLY;