Hi, I have two queries, and I want to understand which is better in terms of performance and memory. I also welcome other alternatives for these two.
Query 1:
SELECT DISTINCT a.no,
a.id1 ,
a.id2
FROM tbl_b b ,
tbl_a a ,
tbl_c c ,
tbl_d d
WHERE (
b.id1 = a.id1
AND a.id1 = c.id1
AND upper(c.flag) = 'Y'
AND c.id1 = d.id1
)
OR (
b.id2 = a.id2
AND a.id2 = c.id2
AND upper(c.flag) = 'Y'
AND c.id2 = d.id2
)
AND d.id3 = 10
Here tables b and d are very large tables running over 500,000 to millions of rows, while table a is relatively smaller.
My requirement is to pick up only those records from table a, whose id (either id1 or id2) is available in b,c,d tables, satisfying certain other conditions as well.
The alternate query that I have in hand is
Query 2:
SELECT DISTINCT a.no,
a.id1 ,
a.id2
FROM tbl_a a
where exists ( select a.id1, a.id2 from
tbl_c c where ((a.id1 = c.id1 or a.id2 = c.id2)
AND upper(c.active_flag) = 'Y'))
and exists ( select a.id1, a.id2 from
tbl_b b where b.id1 = a.id1 or b.id2 = a.id2)
and exists ( select a.id1, a.id2 from tbl_d d
where (a.id1 = d.id1 or a.id2 = d.id2)
AND d.id3 = 10)
Which is best performance-wise? I understand that query two occupies lesser space than query 1. But how do I select the best one?