Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8989905
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T22:22:35+00:00 2026-06-15T22:22:35+00:00

MERGE INTO table1 t1 USING (SELECT column1 FROM table2 join table3 on… WHERE table2.column5

  • 0
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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T22:22:36+00:00Added an answer on June 15, 2026 at 10:22 pm

    Yes, Oracle will rewrite the query to join table1 to the using view query. so if t1.column2 = somevalue AND t1.column3 = someothervalue is 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.

    set linesize 200 pagesize 200
    explain plan for
    merge....;
    select * from table(dbms_xplan.display());
    

    and you should see that Oracle has done this for you.

    eg:

    SQL> create table table1(id number primary key, t2_id number, str varchar2(20), notes varchar2(20));
    
    Table created.
    
    SQL> create table table2(id number primary key, notes varchar2(20));
    
    Table created.
    
    SQL>
    SQL> insert into table1
      2  select rownum, rownum, case mod(rownum, 100) when 0 then 'ONE' else 'TWO' end, null
      3  from dual connect by level <=1000000;
    
    1000000 rows created.
    
    SQL>
    SQL> insert into table2
      2  select rownum, dbms_random.string('x', 10)
      3  from dual connect by level <=1000000;
    
    1000000 rows created.
    
    SQL>
    SQL> create index table1_idx on table1(str);
    
    Index created.
    
    SQL> exec dbms_stats.gather_table_stats(user, 'TABLE1', method_opt=>'for all indexed columns size skewonly');
    
    PL/SQL procedure successfully completed.
    
    SQL> exec dbms_stats.gather_table_stats(user, 'TABLE2');
    
    PL/SQL procedure successfully completed.
    

    so ill add t1.str = 'ONE' when is very selective:

    SQL> explain plan for
      2  merge into table1 t1
      3  using (select * from table2 t where t.id > 1000) t2
      4  on (t2.id = t1.t2_id and t1.str = 'ONE')
      5  when matched then update
      6  set t1.notes = t2.notes;
    
    Explained.
    
    SQL> @explain ""
    
    Plan hash value: 2050534005
    
    ---------------------------------------------------------------------------------------------
    | Id  | Operation                      | Name       | Rows  | Bytes | Cost (%CPU)| Time     |
    ---------------------------------------------------------------------------------------------
    |   0 | MERGE STATEMENT                |            |   441 | 11025 |   929   (5)| 00:00:12 |
    |   1 |  MERGE                         | TABLE1     |       |       |            |          |
    |   2 |   VIEW                         |            |       |       |            |          |
    |*  3 |    HASH JOIN                   |            |   441 | 12348 |   929   (5)| 00:00:12 |
    |*  4 |     TABLE ACCESS BY INDEX ROWID| TABLE1     |   441 |  5733 |    69   (2)| 00:00:01 |
    |*  5 |      INDEX RANGE SCAN          | TABLE1_IDX |  8828 |       |    21   (0)| 00:00:01 |
    |*  6 |     TABLE ACCESS FULL          | TABLE2     |   994K|    14M|   848   (4)| 00:00:11 |
    -------------------------------------------------------------------------------------------
    
    Predicate Information (identified by operation id):
    ---------------------------------------------------
       3 - access("T"."ID"="T1"."T2_ID")
       4 - filter("T1"."T2_ID">1000)
       5 - access("T1"."STR"='ONE')
       6 - filter("T"."ID">1000)
    

    you can see its applied the index on table1

    INDEX RANGE SCAN          | TABLE1_IDX
    

    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).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The query: MERGE INTO app_role_data x USING (select ? name, ? xml FROM dual)
The following statement is in a Stored Procedure MERGE INTO table1 a USING (SELECT
MERGE INTO table_1 a USING (SELECT * from table_2) b ON ( a.row_id =
What's wrong with my UPDATE_OR_INSERT code below? MERGE INTO EMAIL_LIST d USING (SELECT 'foo@gmail.com'
I am running an oracle query in sqldeveloper: merge into invoices c using (select
I am trying to merge the data from two tables into one result but
using the following query in SQLDeveloper to test merge everything goes fine: merge into
How can you get the number of affected rows from executing a MERGE INTO...
I am using merge command to insert a non-exist record into table. When I
What is faster? the Merge statement MERGE INTO table_name USING dual ON (row_id =

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.