我正在尝试提高下面的查询速度,但无法更改必填字段。所以我被困在这里。请帮我摆脱这个陷阱。一点点提示或灵感也很有用!!
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
答案 0 :(得分:0)
将相关子查询替换为instance1
以消除所有连接:
instance2
(我故意将代码行留得更长,以便条件排成一行。这有助于明确列之间的差异,以及它们几乎完全相同的事情。)< / p>