tuning required for the query
658306Apr 22 2010 — edited Apr 30 2010Hi,
I am just wondering what could be the best SQL for the following scenario.
Table 1 Table2
---------- -------------
COL1 COL2 COL3 COL1 COL2 COL3
AAA 123 10 AAA 123 12
AAA 123 12
AAA 123 14
AAA 456 10
AAA 456 12
AAA 456 14
Given the above data, I want only the AAA/123/10 and AAA/123/14 records from table1, and leave the others alone.
The tables are huge (200 million) and indexed on all the three columns and my SQL is taking much time to complete.
SQL i have used
select col1, col2, col3 from (
select a.col1, a.col2, a.col3, b.col3 col4 from
table1 a
left join
table2 b
on
a.col1 = b.col1 and
a.col2 = b.col2 and
a.col3 = b.col3
where exists (select 1 from table2 c where a.col1 = c.col1 and a.col2 = c.col2)
) x
where x.col4 is null
Can anyone give me better solution than this?