简体   繁体   中英

How to using Left Outer Join or Right Outer Join in Oracle 11g

I have a query using "=" in where clause, but it is long time to execute when many datas. How to use the Left Outer Join or Right Outer Join or something like that to increase performance This is query:

select sum(op.quantity * op.unit_amount) into paid_money
from tableA op , tableB ssl, tableC ss, tableD pl, tableE p
where  (op.id = ssl.id and ssL.id = ss.id and ss.type='A') 
    or 
    (op.id = pl.id and pl.id = p.id and p.type='B');

Your problem is not left or right joins. It is cross joins. You are doing many unnecessary cartesian products. I'm guessing this query will never finish. If it did, you'd get the wrong answer anyway.

Split this into two separate joins and then bring the results together. Only use the tables you need for each set of joins:

select SUM(val) into paid_money
from (select sum(op.quantity * op.unit_amount) as val
      from tableA op , tableB ssl
      where  (op.id = ssl.id and ssL.id = ss.id and ss.type='A')  
      union all
      select sum(op.quantity * op.unit_amount) as val
      from tableA op , tableD pl, tableD p 
      where (op.id = pl.id and pl.id = p.id and p.type='B')
     ) t

I haven't fixed your join syntax. But, you should learn to use the join keyword and to put the join conditions in an on clause rather than the where clause.

Are you sure that this query is returning the required data? To me it looks like it will be returning the cartesian product of op, ssl & ss for each op, pl, p match and vice versa.

I would advise that you split it into two seperate queries, union them together, and then sum over the top.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM