尝试优化 PL/SQL 查询
Try to optimized a PL/SQL query
我正在尝试提高下面查询的速度,但是必填字段无法更改。所以我被困在这里了。请帮我摆脱这个陷阱。一点提示或灵感也很有帮助!!
select cg.province_id,
(select count(distinct(c.guidance_user_id))
from case_guidance c
where c.guidance_status = '2'
and c.province_id = cg.province_id) as guidance_cnt,
(select count(distinct(c.guidance_user_id))
from case_guidance c
where c.guidance_status = '2'
and c.guidance_user_type = 'role.type.teacher'
and c.province_id = cg.province_id) as guidance_teacher_cnt,
(select count(distinct(c.guidance_user_id))
from case_guidance c
where c.guidance_status = '2'
and c.guidance_user_type = 'role.type.jyy'
and c.province_id = cg.province_id) as guidance_jyy_cnt,
(select count(distinct(c.guidance_user_id))
from case_guidance c
where c.guidance_status = '2'
and c.guidance_user_type = 'role.type.expert'
and c.province_id = cg.province_id) as guidance_expert_cnt,
(select count(distinct(c.case_id))
from case_guidance c
where c.guidance_status = '2'
and c.province_id = cg.province_id) as guidance_case_cnt
from case_guidance cg
where cg.province_id is not null
group by cg.province_id
order by guidance_cnt desc
将相关子查询替换为CASE
以消除所有连接:
select
province_id,
count(distinct(case when guidance_status = '2' then guidance_user_id else null end)) guidance_cnt,
count(distinct(case when guidance_status = '2' and guidance_user_type = 'role.type.teacher' then guidance_user_id else null end)) guidance_teacher_cnt,
count(distinct(case when guidance_status = '2' and guidance_user_type = 'role.type.jyy' then guidance_user_id else null end)) guidance_jyy_cnt,
count(distinct(case when guidance_status = '2' and guidance_user_type = 'role.type.expert' then guidance_user_id else null end)) guidance_expert_cnt,
count(distinct(case when guidance_status = '2' then case_id else null end)) guidance_case_cnt
from case_guidance
group by province_id;
order by guidance_cnt desc
(我故意将代码行留得特别长,以便条件一致。这有助于弄清楚列之间的区别,并且它们都在做几乎完全相同的事情。)
我正在尝试提高下面查询的速度,但是必填字段无法更改。所以我被困在这里了。请帮我摆脱这个陷阱。一点提示或灵感也很有帮助!!
select cg.province_id,
(select count(distinct(c.guidance_user_id))
from case_guidance c
where c.guidance_status = '2'
and c.province_id = cg.province_id) as guidance_cnt,
(select count(distinct(c.guidance_user_id))
from case_guidance c
where c.guidance_status = '2'
and c.guidance_user_type = 'role.type.teacher'
and c.province_id = cg.province_id) as guidance_teacher_cnt,
(select count(distinct(c.guidance_user_id))
from case_guidance c
where c.guidance_status = '2'
and c.guidance_user_type = 'role.type.jyy'
and c.province_id = cg.province_id) as guidance_jyy_cnt,
(select count(distinct(c.guidance_user_id))
from case_guidance c
where c.guidance_status = '2'
and c.guidance_user_type = 'role.type.expert'
and c.province_id = cg.province_id) as guidance_expert_cnt,
(select count(distinct(c.case_id))
from case_guidance c
where c.guidance_status = '2'
and c.province_id = cg.province_id) as guidance_case_cnt
from case_guidance cg
where cg.province_id is not null
group by cg.province_id
order by guidance_cnt desc
将相关子查询替换为CASE
以消除所有连接:
select
province_id,
count(distinct(case when guidance_status = '2' then guidance_user_id else null end)) guidance_cnt,
count(distinct(case when guidance_status = '2' and guidance_user_type = 'role.type.teacher' then guidance_user_id else null end)) guidance_teacher_cnt,
count(distinct(case when guidance_status = '2' and guidance_user_type = 'role.type.jyy' then guidance_user_id else null end)) guidance_jyy_cnt,
count(distinct(case when guidance_status = '2' and guidance_user_type = 'role.type.expert' then guidance_user_id else null end)) guidance_expert_cnt,
count(distinct(case when guidance_status = '2' then case_id else null end)) guidance_case_cnt
from case_guidance
group by province_id;
order by guidance_cnt desc
(我故意将代码行留得特别长,以便条件一致。这有助于弄清楚列之间的区别,并且它们都在做几乎完全相同的事情。)