Oracle Merge vs Select then Insert or Update

前端 未结 2 1711
天命终不由人
天命终不由人 2021-02-03 10:39

What is faster?

the Merge statement

    MERGE INTO table_name 
     USING dual
     ON (row_id = \'some_id\')
    WHEN MATCHED THEN
     UPDATE SET col_n         


        
2条回答
  •  一向
    一向 (楼主)
    2021-02-03 11:36

    The rule of thumb is, if you can do it in one SQL, it'll generally perform better than doing it in multiple SQL statements.

    I'd go with the MERGE if it does the job.

    Also - another suggestion: you can avoid repeating data in your statement, e.g.:

    MERGE INTO table
     USING (SELECT 'some_id' AS newid,
                   'some_val' AS newval
            FROM dual)
     ON (rowid = newid)
    WHEN MATCHED THEN
     UPDATE SET colname = newval
    WHEN NOT MATCHED THEN
     INSERT (rowid, colname)
     VALUES (newid, newval)
    

提交回复
热议问题