MERGE INTO table1 t1
USING
(SELECT column1
FROM table2 join table3 on...
WHERE table2.column5 = 'xyz') t2
ON (t1.column1 = t2.column1 AND t1.column2 = somevalue
AND t1.column3 = someothervalue)
WHEN not matched
THEN
INSERT ...
The “on” part will reject most of the rows, but does merge query all the rows that are inside “using” first? In that case that part will be running uselessly most of the times because 95% of the rows will not match t1.column2 = somevalue
AND t1.column3 = someothervalue. Or is Oracle smart enough to not do that?
Yes, Oracle will rewrite the query to join table1 to the using view query. so if
t1.column2 = somevalue AND t1.column3 = someothervalueis selective and Oracle realises this, you should see in the plan that the query will drive from TABLE1 and then join into the tables in the using view. just run an explain plan to check it. i.e.and you should see that Oracle has done this for you.
eg:
so ill add
t1.str = 'ONE'when is very selective:you can see its applied the index on table1
thus removing a lot of rows scanned on table1 (hash join was more appropriate considering my table, but in your case you may see a nested loop approach on the join step).