如何提高下面给出的查询性能
select
distinct o1.id as c1,
a1.id as c2,
o1.t1_id as c3,
o1.t2_id as c4,
o1.t_p_id as c5
from
ord o1
left outer join
acc a1
on o1.end_user_id=a1.id
left outer join
acc a2
on 1.t1_id=a2.id
left outer join
acc a3
on o1.t2_id=a3.id
left outer join
acc a4 on
o1.t_p_id=account4_.id
where
a1.account_id=1 or a2.account_id=1 or a3.account_id=1 or a4.account_id=1;
答案 0 :(得分:1)
在我看来,同一个acc表上的这么多左外连接可能是造成性能问题的原因。
我建议弄清楚你的意图,然后尝试在同一张桌子上消除这么多左连接。
对于一般分析,我认为@ScottMarlowe是对的,你需要提供更多的信息,比如索引,解释结果等......
答案 1 :(得分:0)
我不是专家,但从我的观点来看,你可以使用'exists'命令而不是'left join'。
我尝试使用'exists'命令编写您的查询,如下所示:
select
distinct o1.id as c1,
a1.id as c2,
o1.t1_id as c3,
o1.t2_id as c4,
o1.t_p_id as c5
from ord o1
where exists (select 1
from acc a1 where o1.end_user_id=a1.id and a1.account_id=1)
or exists (select 1
from acc a2 where o1.end_user_id=a2.id and a2.account_id=1)
or exists (select 1
from acc a3 where o1.end_user_id=a3.id and a3.account_id=1)
or exists (select 1
from acc a4 where o1.end_user_id=a4.id and a4.account_id=1);
我希望它有所帮助。