2013-02-22 125 views
-1

服务表现的问题,如何提高查询的下面LEFT OUTER JOIN PostgreSQL中

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; 
+2

请阅读:http://wiki.postgresql.org/wiki/Slow_Query_Questions并发布更多信息。 – 2013-02-22 06:54:04

+0

没有表DDL,索引,'EXPLAIN ANALYZE'输出,服务器配置,无法说任何... – vyegorov 2013-02-22 07:07:15

+1

请详细说明。 http://stackoverflow.com/tags/postgresql-performance/info – 2013-02-22 07:08:23

回答

1

给出的性能在我看来,这么多左外连接在同一ACC表可能是这个原因造成的性能问题。

我会建议找出你的意图,然后尝试消除同一张桌子上的这么多左连接。

一般因素浅析,我认为@ScottMarlowe是正确的,你需要给更多的信息来源,如索引,解释结果,等...

0

我不是专家,但是从我的角度来看,而不是使用'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); 

我希望它能帮助。