Domanda

I have a following relationship between several of my tables:

TableA -- 1-to-1 -- TableB -- 1-to-1 -- TableC
                      |___ -- 1-to-1 -- TableD
                      |___ -- 1-to-1 -- TableE

The relation between TableA and TableB is mandatory, however relation between TableB and TableC, TableD, and TableE are optional. I need to pull information from all three latter tables, if available in any; would that mean that the only option I have is to invoke LEFT OUTER JOIN on all the joins? i.e.

SELECT a.a1
       , c.c1
       , c.c2
       , d.d1
       , e.e1
       , e.e2
       , e.e3
FROM TableA a
JOIN TableB b 
   ON a.A_ID = b.AF_ID
LEFT OUTER JOIN TableC c 
   ON b.B_ID = c.FB_ID
LEFT OUTER JOIN TableD d 
   ON b.B_ID = d.FB_ID
LEFT OUTER JOIN TableE e 
   ON b.B_ID = e.FB_ID

Wouldn't this cause the last three joins not to use indexes (Oracle 11.2)? Is there another way to write the query?

È stato utile?

Soluzione

If you wanted to "force" Oracle you use joins, you could phrase the query as:

SELECT a.a1,
       (select c.c1 from TableC c where b.B_ID = c.FB_ID) as c1,
       (select c.c2 from TableC c where b.B_ID = c.FB_ID) as c2,
       (select d.d1 from TableD d where b.B_ID = d.FB_ID) as d1,
       (select e.e1 from TableE e where b.B_ID = e.FB_ID) as e1,
       (select e.e2 from TableE e where b.B_ID = e.FB_ID) as e2,
       (select e.e3 from TableE e where b.B_ID = e.FB_ID) as e3
FROM TableA a JOIN
     TableB b 
     ON a.A_ID = b.AF_ID;

This formulation might make it more clear how the indexes get used. (And, this assumes that there is at least one match in each of the optional tables.)

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top