我有一个作为函数的一部分运行的查询,该函数生成一个包含计数、平均值和逗号分隔列表的单行表,如下所示:
select
(select
count(*)
from vw_disp_details
where round = 2013
and rating = 1) applicants,
(select
count(*)
from vw_disp_details
where round = 2013
and rating = 1
and applied != 'yes') s_applicants,
(select
LISTAGG(discipline, ',')
WITHIN GROUP (ORDER BY discipline)
from (select discipline,
count(*) discipline_number
from vw_disp_details
where round = 2013
and rating = 1
group by discipline)) disciplines,
(select
LISTAGG(discipline_count, ',')
WITHIN GROUP (ORDER BY discipline)
from (select discipline,
count(*) discipline_count
from vw_disp_details
where round = 2013
and rating = 1
group by discipline)) disciplines_count,
(select
round(avg(util.getawardstocols(application_id,'1','AWARD_NAME')), 2)
from vw_disp_details
where round = 2013
and rating = 1) average_award_score,
(select
round(avg(age))
from vw_disp_details
where round = 2013
and rating = 1) average_age
from dual;
除了不是 6 个主要子查询,而是有 23 个。
这将返回类似这样的内容(如果它是 CSV):
applicants | s_applicants | disciplines | disciplines_count | average_award_score | average_age
107 | 67 | "speed,accuracy,strength" | 3 | 97 | 23
现在我以编程方式将 where 子句的“rating = 1”部分换成其他表达式。它们都运行得相当快,除了“rating = 1”需要大约 90 秒才能运行,这是因为 vw_disp_details 视图中的 rating 列本身是由子查询编译的:
(SELECT score
FROM read r,
eval_criteria_lookup ecl
WHERE r.criteria_id = ecl.criteria_id
AND r.application_id = a.lgo_application_id
AND criteria_description = 'Overall Score'
AND type = 'ABC'
) reader_rank
因此,当函数运行时,这个额外的查询似乎会大大减慢一切。
我的问题是,有没有更好(更有效)的方法来运行这样的查询,基本上只是一系列计数和平均值,我如何重构以优化速度,以便评级 = 1 查询不需要运行 90 秒。