SlideShare a Scribd company logo
1 of 34
Download to read offline
blog.sqlora.com@Andrej_SQL
MERGE SQL Statement: Lesser Known Facets
Andrej Pashchenko
About me
• Working at Trivadis Germany, Düsseldorf
• Focusing on Oracle:
• Data Warehousing
• Application Development
• Application Performance
• Course instructor „Oracle New Features for
Developers“
@Andrej_SQL blog.sqlora.com
• MERGE is a part of SQL 2003 and
has been introduced in Oracle 9i
• Since then, the MERGE has been
well adopted and widely used,
but sometimes still has some
confusing or unexpected
behavior
ORA-30926
ORA-30926
• ORA-30926 is definitely the most confusing error related to MERGE
• The error description is somewhat confusing too:
• One of the reasons is clearly documented:
• Create an „overlaping“ bonus list and try to merge it in employee table
INSERT INTO scott.bonus (ename, job, sal, comm)
SELECT ename, job, sal, sal*0.2 comm
FROM scott.emp
WHERE deptno = 30;
INSERT INTO scott.bonus (ename, job, sal, comm)
SELECT ename, job, sal, sal*0.1 comm
FROM scott.emp
WHERE job = 'MANAGER’;
SQL> MERGE INTO scott.emp e
2 USING scott.bonus b
3 ON (b.ename = e.ename)
4 WHEN MATCHED THEN UPDATE set e.comm = b.comm;
USING scott.bonus b
*
ERROR at line 2:
ORA-30926: unable to get a stable set of rows in the source tables
ORA-30926 - Example
Sales department 20%
Each manager 10%
But SALES also has a manager:
BLAKE will be updated twice
• Check the duplicates in the source with respect to the ON-keys:
SQL> MERGE INTO scott.emp e
2 USING scott.bonus b
3 ON (b.ename = e.ename)
4 WHEN MATCHED THEN
5 UPDATE SET e.comm = b.comm;
USING scott.bonus b
*
ERROR at line 2:
ORA-30926: unable to get a stable
set of rows in the source tables
ORA-30926 - How to find the problem?
SQL> SELECT ename
2 FROM scott.bonus b
3 GROUP BY ename
4 HAVING COUNT(*) > 1;
ENAME
----------
BLAKE
SQL>SELECT ename, MAX(comm) comm FROM scott.bonus b GROUP BY ename;
• Find the correct way to avoid duplicates. Often this is a business question, e.g. use MAX or
SUM:
• Fix the problem in the source data or directly in your query:
SQL> MERGE INTO scott.emp e
2 USING (SELECT ename, MAX(comm) comm
3 FROM scott.bonus b
4 GROUP BY ename) b
5 ON (b.ename = e.ename)
6 WHEN MATCHED THEN UPDATE set e.comm = b.comm;
8 rows merged.
ORA-30926 Fixing the problem
• What does the documentation say about ORA-30926:
ORA-30926: unable to get a stable set of rows in the source tables
Cause: A stable set of rows could not be got because of large dml activity or a non-
deterministic where clause.
Action: Remove any non-deterministic where clauses and reissue the dml.
The whole execution three times?
• Have you noticed that the execution takes much longer if you get ORA-30926?
SQL> MERGE INTO scott.emp e
2 USING scott.bonus b
3 ON (b.ename = e.ename)
4 WHEN MATCHED THEN UPDATE SET e.comm = b.comm;
USING scott.bonus b
*
ERROR at line 2:
ORA-30926: unable to get a stable set of rows in the source tables
-----------------------------------------------------------------
| Id | Operation | Name | Starts | E-Rows | A-Rows |
-----------------------------------------------------------------
| 0 | MERGE STATEMENT | | 3 | | 0 |
| 1 | MERGE | EMP | 3 | | 0 |
| 2 | VIEW | | 3 | | 21 |
|* 3 | HASH JOIN | | 3 | 9 | 21 |
| 4 | TABLE ACCESS FULL| BONUS | 3 | 9 | 27 |
| 5 | TABLE ACCESS FULL| EMP | 3 | 14 | 26 |
-----------------------------------------------------------------
Write Consistency
and DML restarts
Write Consistency and DML Restarts (UPDATE)
Session 1 Session 2
SQL> UPDATE emp
2 SET sal = 1000
3 WHERE ename = 'JAMES';
t1
SQL> UPDATE emp
2 SET comm = nvl(comm,0) + 1000
3 WHERE sal < 1000;
t2
COMMIT;t3
t4
SQL> SELECT ename, sal, comm ENAME SAL COMM
FROM emp WHERE sal < 1000; ---------- ---------- ----------
SMITH 800
JAMES 950
ENAME SAL COMM
---------- ---------- ----------
SMITH 800 1000
JAMES 1000
Cannot update JAMES‘s row an waits
Session 2 can now update JAMES, but JAMES‘s
salary is not less than 1000 anymore
Write Consistency and DML Restart
Get SCN1
Identify rows to be updated
in consistent mode (per SCN1)
Get row in
current mode
tracked columns
unchanged?
Rollback all changes
made so far
Request new
SCN
SELECT FOR UPDATE
(current mode)
Identify rows to be updated
in consistent mode (new SCN)
tracked columns
unchanged?
Update locked rows
Update the row
Yes
YesNo
Up to
5000
times
No
Tracked: columns in
WHERE and :OLD, :NEW
values in BEFORE EACH
ROW trigger
Write Consistency and DML Restarts (MERGE)
Session 1 Session 2
SQL> UPDATE emp
2 SET sal = 1000
3 WHERE ename = 'JAMES';
t1
SQL> MERGE INTO emp t
2 USING (SELECT 1000 comm FROM dual) q
3 ON (t.sal < 1000)
4 WHEN MATCHED THEN UPDATE
5 SET t.comm = nvl(t.comm,0)+q.comm;
t2
COMMIT;t3
t4
ENAME SAL COMM
---------- ---------- ----------
SMITH 800 1000
JAMES 1000 1000
Waits for locked row
Session 2 can now update JAMES, but JAMES‘s
salary is not less than 1000 anymore
Write Consistency and DML Restarts (MERGE)
Session 1 Session 2
SQL> UPDATE emp
2 SET sal = 1000
3 WHERE ename = 'JAMES';
t1
SQL> MERGE INTO emp t
2 USING (SELECT 1000 comm FROM dual) q
3 ON (t.sal < 1000)
4 WHEN MATCHED THEN UPDATE
5 SET t.comm = nvl(t.comm,0)+q.comm
6 WHERE (t.sal < 1000) ;
t2
COMMIT;t3
t4
ENAME SAL COMM
---------- ---------- ----------
SMITH 800 1000
JAMES 1000
Waits for locked row
Write Consistency and DML Restarts (MERGE)
Session 1 Session 2
SQL> UPDATE emp
2 SET comm = 500
3 WHERE ename = 'JAMES';
t1
SQL> MERGE INTO emp t
2 USING (SELECT 1000 comm FROM dual) q
3 ON (t.sal < 1000)
4 WHEN MATCHED THEN UPDATE
5 SET t.comm = nvl(t.comm,0)+ q.comm;
t2
COMMIT;t3
t4
ENAME SAL COMM
---------- ---------- ----------
SMITH 800 1000
JAMES 950 1500
Waits for locked row
Write Consistency and DML Restart
• MERGE can show a different behavior
regarding DML restarts
• There was a bug until 18c about not
tracking ON-columns, but it is still
there even in 19c in some cases
• But MERGE is tracking columns in SET
clause thus preventing “lost updates”
during running statements (no replace
for locking strategy in your app)
• In case of DML restart triggers can fire
multiple times, so avoid any non-
transactional logic or autonomous
transactions in triggers!
ORA-30926 revisited
Write Consistency and DML Restart
• Obviously Oracle is using the same mechanism of mini rollbacks as with write
consistency also to ensure its deterministic behavior
• SET-columns are tracked
• Even within the same session updating the column to another value will be detected
• The MERGE statement will be restarted
• As a result, we can observe the mentioned triple effort: MERGE and than rollback,
SELECT FOR UPDATE and then MERGE again
ORA-38104
ORA-38104
• Oracle doesn’t allow to update columns used in ON clause
• If you feel like you have to do this, verify your requirements carefully
• Sometimes useful for ad hoc data manipulation, fixing erroneous data, etc.
EMPNO ENAME
7839 KING
7782 CLARK
7934 MILLER
EMPNO ROLE_NAME
7839 DEFAULT
7782 SALES
EMP
EMP_ROLES
• Assign the role
ACCOUNTING to each
employee of deptno=10
• If an employee is
assigned the DEFAULT
role, overwrite this
assignment.
EMPNO ROLE_NAME
7839 ACCOUNTING
7782 SALES
7782 ACCOUNTING
7934 ACCOUNTING
EMP_ROLES
MERGE?
ORA-38104
• The straightforward approach doesn’t work:
SQL> MERGE INTO emp_roles t
2 USING (SELECT empno, 'ACCOUNTING' role_name, 'DEFAULT' old_role
3 FROM emp
4 WHERE deptno = 10
5 ) q
6 ON (t.empno = q.empno and t.role_name = q.old_role)
7 WHEN MATCHED THEN UPDATE SET t.role_name = q.role_name
8 WHEN NOT MATCHED THEN INSERT VALUES (q.empno, q.role_name) ;
ON (t.empno = q.empno and t.role_name = q.old_role)
*
ERROR at line 6:
ORA-38104: Columns referenced in the ON Clause cannot be updated:
"T"."ROLE_NAME"
ORA-38104
• Using WHERE clause instead of ON only seems to work, because the result is wrong:
no new role assignment for MILLER (7782)
EMPNO ROLE_NAME
7839 ACCOUNTING
7782 SALES
7934 ACCOUNTING
EMP_ROLES
SQL> MERGE INTO emp_roles t
2 USING (SELECT empno, 'ACCOUNTING' role_name
3 FROM emp
4 WHERE deptno =10
5 ) q
6 ON (t.empno = q.empno)
7 WHEN MATCHED THEN UPDATE
8 SET role_name = q.role_name
9 WHERE t.role_name = 'DEFAULT'
10 WHEN NOT MATCHED THEN INSERT (empno, role_name)
11 VALUES (q.empno, q.role_name) ;
2 rows merged.
ORA-38104 – Using ROWID
• Doing the whole logic in USING subquery and merging on ROWID
• At the price of increased complexity and performance penalty of one more join
EMPNO ROLE_NAME
7839 ACCOUNTING
7782 SALES
7782 ACCOUNTING
7934 ACCOUNTING
EMP_ROLES
SQL> MERGE INTO emp_roles t
2 USING (SELECT r.rowid rid, 'ACCOUNTING' new_role_name
3 , e.empno
4 FROM emp e LEFT JOIN emp_roles r
5 ON e.empno = r.empno
6 AND r.role_name = 'DEFAULT'
7 WHERE e.deptno = 10
8 ) q
9 ON (t.rowid = q.rid )
10 WHEN MATCHED THEN UPDATE
11 SET role_name = q.new_role_name
12 WHEN NOT MATCHED THEN INSERT (empno, role_name)
13 VALUES (q.empno, q.new_role_name) ;
3 rows merged.
ORA-38104 – Fooling the Parser
• Using a subquery
EMPNO ROLE_NAME
7839 ACCOUNTING
7782 SALES
7782 ACCOUNTING
7934 ACCOUNTING
EMP_ROLES
SQL> MERGE INTO emp_roles t
2 USING (SELECT empno, 'ACCOUNTING' role_name
3 , 'DEFAULT' old_role
4 FROM emp
5 WHERE deptno = 10
6 ) q
7 ON ( t.empno = q.empno
8 AND (SELECT t.role_name FROM dual) = q.old_role )
9 WHEN MATCHED THEN UPDATE SET role_name = q.role_name
10 WHEN NOT MATCHED THEN INSERT (empno, role_name)
11 VALUES (q.empno, q.role_name) ;
3 rows merged.
Idee: Blog Lukas Eder
ORA-38104 – Fooling the Parser
• Using a view as a merge target and hiding a column inside NVL()
EMPNO ROLE_NAME
7839 ACCOUNTING
7782 SALES
7782 ACCOUNTING
7934 ACCOUNTING
EMP_ROLES
SQL> MERGE INTO
2 (SELECT empno, role_name
3 , nvl(role_name,'NEVER') check_role_name
4 FROM emp_roles) t
5 USING (SELECT empno, 'ACCOUNTING' role_name
6 , 'DEFAULT' old_role
7 FROM emp
8 WHERE deptno = 10 ) q
9 ON ( t.empno = q.empno
10 AND t.check_role_name = q.old_role )
11 WHEN MATCHED THEN UPDATE SET role_name = q.role_name
12 WHEN NOT MATCHED THEN INSERT (empno, role_name)
13 VALUES (q.empno, q.role_name) ;
3 rows merged.
Idee: Blog Lukas Eder
ORA-38104 – Fooling the Parser
• Using row value expressions
EMPNO ROLE_NAME
7839 ACCOUNTING
7782 SALES
7782 ACCOUNTING
7934 ACCOUNTING
EMP_ROLES
SQL> MERGE INTO emp_roles t
2 USING (SELECT empno, 'ACCOUNTING' role_name
3 , 'DEFAULT' old_role
4 FROM emp
5 WHERE deptno = 10 ) q
6 ON ( 1=2 OR
7 (t.empno, t.role_name) = ((q.empno, q.old_role)) )
8 WHEN MATCHED THEN UPDATE SET role_name = q.role_name
9 WHEN NOT MATCHED THEN INSERT (empno, role_name)
10 VALUES (q.empno, q.role_name) ;
3 rows merged.
Idee: Blog Lukas Eder
ORA-38104 – Can be executed multiple times
• All previous examples could only be executed once! This one is “multi-executable”
EMPNO ROLE_NAME
7839 ACCOUNTING
7782 SALES
7782 ACCOUNTING
7934 ACCOUNTING
EMP_ROLES
SQL> MERGE INTO emp_roles t
2 USING (
3 SELECT DISTINCT
4 FIRST_VALUE(r.rowid)
5 OVER(PARTITION BY e.empno
6 ORDER BY DECODE(r.role_name,
7 'ACCOUNTING',1,2)) rid
8 , e.empno, 'ACCOUNTING' new_role_name
9 FROM emp e LEFT JOIN emp_roles r
10 ON e.empno = r.empno
11 AND r.role_name in ('DEFAULT', 'ACCOUNTING')
12 WHERE e.deptno = 10
13 ) q
14 ON (t.rowid = q.rid )
15 WHEN MATCHED THEN UPDATE SET role_name = q.new_role_name
16 WHEN NOT MATCHED THEN INSERT (empno, role_name)
17 VALUES (q.empno, q.new_role_name) ;
3 rows merged.
Direct-Path Insert
&
Parallel DML
Direct Path and MERGE?
SQL> MERGE /*+ append */
2 INTO tx2 USING tx ON (tx.n = tx2.n)
3 WHEN NOT MATCHED THEN INSERT (N)
VALUES (tx.n) ;
10,000 rows merged.
----------------------------------------
| Id | Operation | Name |
----------------------------------------
| 0 | MERGE STATEMENT | |
| 1 | MERGE | TX2 |
| 2 | VIEW | |
|* 3 | HASH JOIN RIGHT OUTER| |
| 4 | TABLE ACCESS FULL | TX2 |
| 5 | TABLE ACCESS FULL | TX |
----------------------------------------
SQL> SELECT count(*) FROM tx2;
ORA-12838: cannot read/modify an object
after modifying it in parallel
SQL> INSERT /*+ append*/ INTO tx2
2 SELECT n FROM tx
3 WHERE n NOT IN (SELECT n FROM tx2);
10,000 rows inserted.
-----------------------------------------------
| Id | Operation | Name
-----------------------------------------------
| 0 | INSERT STATEMENT |
| 1 | LOAD AS SELECT | TX2
| 2 | OPTIMIZER STATISTICS GATHERING |
|* 3 | HASH JOIN RIGHT ANTI NA |
| 4 | TABLE ACCESS FULL | TX2
| 5 | TABLE ACCESS FULL | TX
-----------------------------------------------
direct path write has happend
Space Management with PDML and MERGE?
SQL> MERGE /*+ append parallel*/
2 INTO t_tgt_join t0
3 USING ( SELECT ...
----------------------------------------
| Id | Operation |
----------------------------------------
| 0 | MERGE STATEMENT |
| 1 | PX COORDINATOR |
| 2 | PX SEND QC (RANDOM) |
| 3 | MERGE |
| 4 | PX RECEIVE |
SEGMENT_NAME BLOCKS CNT
------------ ------ -------
T_TGT_JOIN 8 1088
... 7 rows ...
T_TGT_JOIN 128 4647
... 20 rows ...
T_TGT_JOIN 1024 34
30 rows selected.
SQL> INSERT /*+ append parallel */
2 INTO t_tgt_join t0
3 SELECT ...
--------------------------------------------------
|Id | Operation
--------------------------------------------------
| 0 | INSERT STATEMENT
| 1 | PX COORDINATOR
| 2 | PX SEND QC (RANDOM)
| 3 | LOAD AS SELECT (HIGH WATER MARK BROKERED)
| 4 | OPTIMIZER STATISTICS GATHERING
SEGMENT_NAME BLOCKS CNT
------------ ---------- ---------
T_TGT_JOIN 8 1024
T_TGT_JOIN 128 4216
T_TGT_JOIN 256 20
T_TGT_JOIN 384 2
T_TGT_JOIN 512 8
T_TGT_JOIN 640 8
T_TGT_JOIN 768 4
T_TGT_JOIN 896 1
T_TGT_JOIN 1024 134
Conclusion
• It‘s easy to find out the reason for ORA-30926
• But be careful when fixing it only by technical methods. Another discussion with business
stakeholders may be necessary.
• Don’t execute heavy batch DML in environments with large user activity.
• Don‘t use any non-transactional logic inside the triggers
• Be careful overcoming ORA-38104 restriction. Rethink your use case if possible.
• Keep in mind a different behavior for PDML and Online Statistics Gathering
Links
• Oracle documentation, MERGE
• Tom Kyte about DML restarts I and II
• Ruslan Dautkhanov, Oracle’s Write Consistency
• Lukas Eder about ORA-38104
• MERGE and ORA-30926

More Related Content

What's hot

Oracle statistics by example
Oracle statistics by exampleOracle statistics by example
Oracle statistics by exampleMauro Pagano
 
Your tuning arsenal: AWR, ADDM, ASH, Metrics and Advisors
Your tuning arsenal: AWR, ADDM, ASH, Metrics and AdvisorsYour tuning arsenal: AWR, ADDM, ASH, Metrics and Advisors
Your tuning arsenal: AWR, ADDM, ASH, Metrics and AdvisorsJohn Kanagaraj
 
Ash masters : advanced ash analytics on Oracle
Ash masters : advanced ash analytics on Oracle Ash masters : advanced ash analytics on Oracle
Ash masters : advanced ash analytics on Oracle Kyle Hailey
 
My Experience Using Oracle SQL Plan Baselines 11g/12c
My Experience Using Oracle SQL Plan Baselines 11g/12cMy Experience Using Oracle SQL Plan Baselines 11g/12c
My Experience Using Oracle SQL Plan Baselines 11g/12cNelson Calero
 
Direct SGA access without SQL
Direct SGA access without SQLDirect SGA access without SQL
Direct SGA access without SQLKyle Hailey
 
Oracle Database performance tuning using oratop
Oracle Database performance tuning using oratopOracle Database performance tuning using oratop
Oracle Database performance tuning using oratopSandesh Rao
 
SQL Macros - Game Changing Feature for SQL Developers?
SQL Macros - Game Changing Feature for SQL Developers?SQL Macros - Game Changing Feature for SQL Developers?
SQL Macros - Game Changing Feature for SQL Developers?Andrej Pashchenko
 
Exploring Oracle Database Performance Tuning Best Practices for DBAs and Deve...
Exploring Oracle Database Performance Tuning Best Practices for DBAs and Deve...Exploring Oracle Database Performance Tuning Best Practices for DBAs and Deve...
Exploring Oracle Database Performance Tuning Best Practices for DBAs and Deve...Aaron Shilo
 
Oracle Performance Tuning Fundamentals
Oracle Performance Tuning FundamentalsOracle Performance Tuning Fundamentals
Oracle Performance Tuning FundamentalsEnkitec
 
Performance Stability, Tips and Tricks and Underscores
Performance Stability, Tips and Tricks and UnderscoresPerformance Stability, Tips and Tricks and Underscores
Performance Stability, Tips and Tricks and UnderscoresJitendra Singh
 
Performance Tuning With Oracle ASH and AWR. Part 1 How And What
Performance Tuning With Oracle ASH and AWR. Part 1 How And WhatPerformance Tuning With Oracle ASH and AWR. Part 1 How And What
Performance Tuning With Oracle ASH and AWR. Part 1 How And Whatudaymoogala
 
Tanel Poder - Scripts and Tools short
Tanel Poder - Scripts and Tools shortTanel Poder - Scripts and Tools short
Tanel Poder - Scripts and Tools shortTanel Poder
 
Oracle Database Performance Tuning Advanced Features and Best Practices for DBAs
Oracle Database Performance Tuning Advanced Features and Best Practices for DBAsOracle Database Performance Tuning Advanced Features and Best Practices for DBAs
Oracle Database Performance Tuning Advanced Features and Best Practices for DBAsZohar Elkayam
 
How a Developer can Troubleshoot a SQL performing poorly on a Production DB
How a Developer can Troubleshoot a SQL performing poorly on a Production DBHow a Developer can Troubleshoot a SQL performing poorly on a Production DB
How a Developer can Troubleshoot a SQL performing poorly on a Production DBCarlos Sierra
 
PostgreSQL Extensions: A deeper look
PostgreSQL Extensions:  A deeper lookPostgreSQL Extensions:  A deeper look
PostgreSQL Extensions: A deeper lookJignesh Shah
 
Understanding SQL Trace, TKPROF and Execution Plan for beginners
Understanding SQL Trace, TKPROF and Execution Plan for beginnersUnderstanding SQL Trace, TKPROF and Execution Plan for beginners
Understanding SQL Trace, TKPROF and Execution Plan for beginnersCarlos Sierra
 
Tanel Poder - Performance stories from Exadata Migrations
Tanel Poder - Performance stories from Exadata MigrationsTanel Poder - Performance stories from Exadata Migrations
Tanel Poder - Performance stories from Exadata MigrationsTanel Poder
 
Enable GoldenGate Monitoring with OEM 12c/JAgent
Enable GoldenGate Monitoring with OEM 12c/JAgentEnable GoldenGate Monitoring with OEM 12c/JAgent
Enable GoldenGate Monitoring with OEM 12c/JAgentBobby Curtis
 

What's hot (20)

Oracle statistics by example
Oracle statistics by exampleOracle statistics by example
Oracle statistics by example
 
Your tuning arsenal: AWR, ADDM, ASH, Metrics and Advisors
Your tuning arsenal: AWR, ADDM, ASH, Metrics and AdvisorsYour tuning arsenal: AWR, ADDM, ASH, Metrics and Advisors
Your tuning arsenal: AWR, ADDM, ASH, Metrics and Advisors
 
Ash masters : advanced ash analytics on Oracle
Ash masters : advanced ash analytics on Oracle Ash masters : advanced ash analytics on Oracle
Ash masters : advanced ash analytics on Oracle
 
My Experience Using Oracle SQL Plan Baselines 11g/12c
My Experience Using Oracle SQL Plan Baselines 11g/12cMy Experience Using Oracle SQL Plan Baselines 11g/12c
My Experience Using Oracle SQL Plan Baselines 11g/12c
 
Direct SGA access without SQL
Direct SGA access without SQLDirect SGA access without SQL
Direct SGA access without SQL
 
Oracle Database performance tuning using oratop
Oracle Database performance tuning using oratopOracle Database performance tuning using oratop
Oracle Database performance tuning using oratop
 
SQL Macros - Game Changing Feature for SQL Developers?
SQL Macros - Game Changing Feature for SQL Developers?SQL Macros - Game Changing Feature for SQL Developers?
SQL Macros - Game Changing Feature for SQL Developers?
 
Exploring Oracle Database Performance Tuning Best Practices for DBAs and Deve...
Exploring Oracle Database Performance Tuning Best Practices for DBAs and Deve...Exploring Oracle Database Performance Tuning Best Practices for DBAs and Deve...
Exploring Oracle Database Performance Tuning Best Practices for DBAs and Deve...
 
Oracle Performance Tuning Fundamentals
Oracle Performance Tuning FundamentalsOracle Performance Tuning Fundamentals
Oracle Performance Tuning Fundamentals
 
AWR and ASH Deep Dive
AWR and ASH Deep DiveAWR and ASH Deep Dive
AWR and ASH Deep Dive
 
Performance Stability, Tips and Tricks and Underscores
Performance Stability, Tips and Tricks and UnderscoresPerformance Stability, Tips and Tricks and Underscores
Performance Stability, Tips and Tricks and Underscores
 
Ash and awr deep dive hotsos
Ash and awr deep dive hotsosAsh and awr deep dive hotsos
Ash and awr deep dive hotsos
 
Performance Tuning With Oracle ASH and AWR. Part 1 How And What
Performance Tuning With Oracle ASH and AWR. Part 1 How And WhatPerformance Tuning With Oracle ASH and AWR. Part 1 How And What
Performance Tuning With Oracle ASH and AWR. Part 1 How And What
 
Tanel Poder - Scripts and Tools short
Tanel Poder - Scripts and Tools shortTanel Poder - Scripts and Tools short
Tanel Poder - Scripts and Tools short
 
Oracle Database Performance Tuning Advanced Features and Best Practices for DBAs
Oracle Database Performance Tuning Advanced Features and Best Practices for DBAsOracle Database Performance Tuning Advanced Features and Best Practices for DBAs
Oracle Database Performance Tuning Advanced Features and Best Practices for DBAs
 
How a Developer can Troubleshoot a SQL performing poorly on a Production DB
How a Developer can Troubleshoot a SQL performing poorly on a Production DBHow a Developer can Troubleshoot a SQL performing poorly on a Production DB
How a Developer can Troubleshoot a SQL performing poorly on a Production DB
 
PostgreSQL Extensions: A deeper look
PostgreSQL Extensions:  A deeper lookPostgreSQL Extensions:  A deeper look
PostgreSQL Extensions: A deeper look
 
Understanding SQL Trace, TKPROF and Execution Plan for beginners
Understanding SQL Trace, TKPROF and Execution Plan for beginnersUnderstanding SQL Trace, TKPROF and Execution Plan for beginners
Understanding SQL Trace, TKPROF and Execution Plan for beginners
 
Tanel Poder - Performance stories from Exadata Migrations
Tanel Poder - Performance stories from Exadata MigrationsTanel Poder - Performance stories from Exadata Migrations
Tanel Poder - Performance stories from Exadata Migrations
 
Enable GoldenGate Monitoring with OEM 12c/JAgent
Enable GoldenGate Monitoring with OEM 12c/JAgentEnable GoldenGate Monitoring with OEM 12c/JAgent
Enable GoldenGate Monitoring with OEM 12c/JAgent
 

Similar to MERGE SQL Statement: Lesser Known Facets

Similar to MERGE SQL Statement: Lesser Known Facets (20)

Les02 Restricting And Sorting Data
Les02 Restricting And Sorting DataLes02 Restricting And Sorting Data
Les02 Restricting And Sorting Data
 
Les02
Les02Les02
Les02
 
The Five Best Things To Happen To SQL
The Five Best Things To Happen To SQLThe Five Best Things To Happen To SQL
The Five Best Things To Happen To SQL
 
Restricting and sorting data
Restricting and sorting data Restricting and sorting data
Restricting and sorting data
 
chap2 (3).ppt
chap2 (3).pptchap2 (3).ppt
chap2 (3).ppt
 
Database Management System
Database Management SystemDatabase Management System
Database Management System
 
Les03
Les03Les03
Les03
 
Les06 Subqueries
Les06 SubqueriesLes06 Subqueries
Les06 Subqueries
 
Les02.pptx
Les02.pptxLes02.pptx
Les02.pptx
 
Using SQL to process hierarchies
Using SQL to process hierarchiesUsing SQL to process hierarchies
Using SQL to process hierarchies
 
COIS 420 - Practice02
COIS 420 - Practice02COIS 420 - Practice02
COIS 420 - Practice02
 
Sql2
Sql2Sql2
Sql2
 
SQL WORKSHOP::Lecture 2
SQL WORKSHOP::Lecture 2SQL WORKSHOP::Lecture 2
SQL WORKSHOP::Lecture 2
 
حل اسئلة الكتاب السعودى فى شرح قواعد البيانات اوراكل
حل اسئلة الكتاب السعودى فى شرح قواعد البيانات اوراكلحل اسئلة الكتاب السعودى فى شرح قواعد البيانات اوراكل
حل اسئلة الكتاب السعودى فى شرح قواعد البيانات اوراكل
 
Les02[1]Restricting and Sorting Data
Les02[1]Restricting and Sorting DataLes02[1]Restricting and Sorting Data
Les02[1]Restricting and Sorting Data
 
Sangam 19 - Analytic SQL
Sangam 19 - Analytic SQLSangam 19 - Analytic SQL
Sangam 19 - Analytic SQL
 
Les02
Les02Les02
Les02
 
7992267.ppt
7992267.ppt7992267.ppt
7992267.ppt
 
Les09[1]Manipulating Data
Les09[1]Manipulating DataLes09[1]Manipulating Data
Les09[1]Manipulating Data
 
Sql 3
Sql 3Sql 3
Sql 3
 

More from Andrej Pashchenko

Polymorphic Table Functions in 18c
Polymorphic Table Functions in 18cPolymorphic Table Functions in 18c
Polymorphic Table Functions in 18cAndrej Pashchenko
 
Properly Use Parallel DML for ETL
Properly Use Parallel DML for ETLProperly Use Parallel DML for ETL
Properly Use Parallel DML for ETLAndrej Pashchenko
 
Polymorphic Table Functions in 18c
Polymorphic Table Functions in 18cPolymorphic Table Functions in 18c
Polymorphic Table Functions in 18cAndrej Pashchenko
 
Online Statistics Gathering for ETL
Online Statistics Gathering for ETLOnline Statistics Gathering for ETL
Online Statistics Gathering for ETLAndrej Pashchenko
 
SQL Pattern Matching – should I start using it?
SQL Pattern Matching – should I start using it?SQL Pattern Matching – should I start using it?
SQL Pattern Matching – should I start using it?Andrej Pashchenko
 
Pure SQL for batch processing
Pure SQL for batch processingPure SQL for batch processing
Pure SQL for batch processingAndrej Pashchenko
 
An unconventional approach for ETL of historized data
An unconventional approach for ETL of historized dataAn unconventional approach for ETL of historized data
An unconventional approach for ETL of historized dataAndrej Pashchenko
 

More from Andrej Pashchenko (7)

Polymorphic Table Functions in 18c
Polymorphic Table Functions in 18cPolymorphic Table Functions in 18c
Polymorphic Table Functions in 18c
 
Properly Use Parallel DML for ETL
Properly Use Parallel DML for ETLProperly Use Parallel DML for ETL
Properly Use Parallel DML for ETL
 
Polymorphic Table Functions in 18c
Polymorphic Table Functions in 18cPolymorphic Table Functions in 18c
Polymorphic Table Functions in 18c
 
Online Statistics Gathering for ETL
Online Statistics Gathering for ETLOnline Statistics Gathering for ETL
Online Statistics Gathering for ETL
 
SQL Pattern Matching – should I start using it?
SQL Pattern Matching – should I start using it?SQL Pattern Matching – should I start using it?
SQL Pattern Matching – should I start using it?
 
Pure SQL for batch processing
Pure SQL for batch processingPure SQL for batch processing
Pure SQL for batch processing
 
An unconventional approach for ETL of historized data
An unconventional approach for ETL of historized dataAn unconventional approach for ETL of historized data
An unconventional approach for ETL of historized data
 

Recently uploaded

Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfPayment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfkalichargn70th171
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...Health
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrainmasabamasaba
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisamasabamasaba
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...SelfMade bd
 
10 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 202410 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 2024Mind IT Systems
 
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) SolutionIntroducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) SolutionOnePlan Solutions
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...panagenda
 
Chinsurah Escorts ☎️8617697112 Starting From 5K to 15K High Profile Escorts ...
Chinsurah Escorts ☎️8617697112  Starting From 5K to 15K High Profile Escorts ...Chinsurah Escorts ☎️8617697112  Starting From 5K to 15K High Profile Escorts ...
Chinsurah Escorts ☎️8617697112 Starting From 5K to 15K High Profile Escorts ...Nitya salvi
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrandmasabamasaba
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Modelsaagamshah0812
 
%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Hararemasabamasaba
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsArshad QA
 
%+27788225528 love spells in Vancouver Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Vancouver Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Vancouver Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Vancouver Psychic Readings, Attraction spells,Br...masabamasaba
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsJhone kinadey
 
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfintroduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfVishalKumarJha10
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...masabamasaba
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...harshavardhanraghave
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Steffen Staab
 

Recently uploaded (20)

Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfPayment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
10 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 202410 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 2024
 
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) SolutionIntroducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
Chinsurah Escorts ☎️8617697112 Starting From 5K to 15K High Profile Escorts ...
Chinsurah Escorts ☎️8617697112  Starting From 5K to 15K High Profile Escorts ...Chinsurah Escorts ☎️8617697112  Starting From 5K to 15K High Profile Escorts ...
Chinsurah Escorts ☎️8617697112 Starting From 5K to 15K High Profile Escorts ...
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
%+27788225528 love spells in Vancouver Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Vancouver Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Vancouver Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Vancouver Psychic Readings, Attraction spells,Br...
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfintroduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 

MERGE SQL Statement: Lesser Known Facets

  • 1. blog.sqlora.com@Andrej_SQL MERGE SQL Statement: Lesser Known Facets Andrej Pashchenko
  • 2. About me • Working at Trivadis Germany, Düsseldorf • Focusing on Oracle: • Data Warehousing • Application Development • Application Performance • Course instructor „Oracle New Features for Developers“ @Andrej_SQL blog.sqlora.com
  • 3.
  • 4.
  • 5. • MERGE is a part of SQL 2003 and has been introduced in Oracle 9i • Since then, the MERGE has been well adopted and widely used, but sometimes still has some confusing or unexpected behavior
  • 7. ORA-30926 • ORA-30926 is definitely the most confusing error related to MERGE • The error description is somewhat confusing too: • One of the reasons is clearly documented:
  • 8. • Create an „overlaping“ bonus list and try to merge it in employee table INSERT INTO scott.bonus (ename, job, sal, comm) SELECT ename, job, sal, sal*0.2 comm FROM scott.emp WHERE deptno = 30; INSERT INTO scott.bonus (ename, job, sal, comm) SELECT ename, job, sal, sal*0.1 comm FROM scott.emp WHERE job = 'MANAGER’; SQL> MERGE INTO scott.emp e 2 USING scott.bonus b 3 ON (b.ename = e.ename) 4 WHEN MATCHED THEN UPDATE set e.comm = b.comm; USING scott.bonus b * ERROR at line 2: ORA-30926: unable to get a stable set of rows in the source tables ORA-30926 - Example Sales department 20% Each manager 10% But SALES also has a manager: BLAKE will be updated twice
  • 9. • Check the duplicates in the source with respect to the ON-keys: SQL> MERGE INTO scott.emp e 2 USING scott.bonus b 3 ON (b.ename = e.ename) 4 WHEN MATCHED THEN 5 UPDATE SET e.comm = b.comm; USING scott.bonus b * ERROR at line 2: ORA-30926: unable to get a stable set of rows in the source tables ORA-30926 - How to find the problem? SQL> SELECT ename 2 FROM scott.bonus b 3 GROUP BY ename 4 HAVING COUNT(*) > 1; ENAME ---------- BLAKE SQL>SELECT ename, MAX(comm) comm FROM scott.bonus b GROUP BY ename; • Find the correct way to avoid duplicates. Often this is a business question, e.g. use MAX or SUM:
  • 10. • Fix the problem in the source data or directly in your query: SQL> MERGE INTO scott.emp e 2 USING (SELECT ename, MAX(comm) comm 3 FROM scott.bonus b 4 GROUP BY ename) b 5 ON (b.ename = e.ename) 6 WHEN MATCHED THEN UPDATE set e.comm = b.comm; 8 rows merged. ORA-30926 Fixing the problem • What does the documentation say about ORA-30926: ORA-30926: unable to get a stable set of rows in the source tables Cause: A stable set of rows could not be got because of large dml activity or a non- deterministic where clause. Action: Remove any non-deterministic where clauses and reissue the dml.
  • 11. The whole execution three times? • Have you noticed that the execution takes much longer if you get ORA-30926? SQL> MERGE INTO scott.emp e 2 USING scott.bonus b 3 ON (b.ename = e.ename) 4 WHEN MATCHED THEN UPDATE SET e.comm = b.comm; USING scott.bonus b * ERROR at line 2: ORA-30926: unable to get a stable set of rows in the source tables ----------------------------------------------------------------- | Id | Operation | Name | Starts | E-Rows | A-Rows | ----------------------------------------------------------------- | 0 | MERGE STATEMENT | | 3 | | 0 | | 1 | MERGE | EMP | 3 | | 0 | | 2 | VIEW | | 3 | | 21 | |* 3 | HASH JOIN | | 3 | 9 | 21 | | 4 | TABLE ACCESS FULL| BONUS | 3 | 9 | 27 | | 5 | TABLE ACCESS FULL| EMP | 3 | 14 | 26 | -----------------------------------------------------------------
  • 13. Write Consistency and DML Restarts (UPDATE) Session 1 Session 2 SQL> UPDATE emp 2 SET sal = 1000 3 WHERE ename = 'JAMES'; t1 SQL> UPDATE emp 2 SET comm = nvl(comm,0) + 1000 3 WHERE sal < 1000; t2 COMMIT;t3 t4 SQL> SELECT ename, sal, comm ENAME SAL COMM FROM emp WHERE sal < 1000; ---------- ---------- ---------- SMITH 800 JAMES 950 ENAME SAL COMM ---------- ---------- ---------- SMITH 800 1000 JAMES 1000 Cannot update JAMES‘s row an waits Session 2 can now update JAMES, but JAMES‘s salary is not less than 1000 anymore
  • 14. Write Consistency and DML Restart Get SCN1 Identify rows to be updated in consistent mode (per SCN1) Get row in current mode tracked columns unchanged? Rollback all changes made so far Request new SCN SELECT FOR UPDATE (current mode) Identify rows to be updated in consistent mode (new SCN) tracked columns unchanged? Update locked rows Update the row Yes YesNo Up to 5000 times No Tracked: columns in WHERE and :OLD, :NEW values in BEFORE EACH ROW trigger
  • 15. Write Consistency and DML Restarts (MERGE) Session 1 Session 2 SQL> UPDATE emp 2 SET sal = 1000 3 WHERE ename = 'JAMES'; t1 SQL> MERGE INTO emp t 2 USING (SELECT 1000 comm FROM dual) q 3 ON (t.sal < 1000) 4 WHEN MATCHED THEN UPDATE 5 SET t.comm = nvl(t.comm,0)+q.comm; t2 COMMIT;t3 t4 ENAME SAL COMM ---------- ---------- ---------- SMITH 800 1000 JAMES 1000 1000 Waits for locked row Session 2 can now update JAMES, but JAMES‘s salary is not less than 1000 anymore
  • 16. Write Consistency and DML Restarts (MERGE) Session 1 Session 2 SQL> UPDATE emp 2 SET sal = 1000 3 WHERE ename = 'JAMES'; t1 SQL> MERGE INTO emp t 2 USING (SELECT 1000 comm FROM dual) q 3 ON (t.sal < 1000) 4 WHEN MATCHED THEN UPDATE 5 SET t.comm = nvl(t.comm,0)+q.comm 6 WHERE (t.sal < 1000) ; t2 COMMIT;t3 t4 ENAME SAL COMM ---------- ---------- ---------- SMITH 800 1000 JAMES 1000 Waits for locked row
  • 17. Write Consistency and DML Restarts (MERGE) Session 1 Session 2 SQL> UPDATE emp 2 SET comm = 500 3 WHERE ename = 'JAMES'; t1 SQL> MERGE INTO emp t 2 USING (SELECT 1000 comm FROM dual) q 3 ON (t.sal < 1000) 4 WHEN MATCHED THEN UPDATE 5 SET t.comm = nvl(t.comm,0)+ q.comm; t2 COMMIT;t3 t4 ENAME SAL COMM ---------- ---------- ---------- SMITH 800 1000 JAMES 950 1500 Waits for locked row
  • 18. Write Consistency and DML Restart • MERGE can show a different behavior regarding DML restarts • There was a bug until 18c about not tracking ON-columns, but it is still there even in 19c in some cases • But MERGE is tracking columns in SET clause thus preventing “lost updates” during running statements (no replace for locking strategy in your app) • In case of DML restart triggers can fire multiple times, so avoid any non- transactional logic or autonomous transactions in triggers!
  • 20. Write Consistency and DML Restart • Obviously Oracle is using the same mechanism of mini rollbacks as with write consistency also to ensure its deterministic behavior • SET-columns are tracked • Even within the same session updating the column to another value will be detected • The MERGE statement will be restarted • As a result, we can observe the mentioned triple effort: MERGE and than rollback, SELECT FOR UPDATE and then MERGE again
  • 22. ORA-38104 • Oracle doesn’t allow to update columns used in ON clause • If you feel like you have to do this, verify your requirements carefully • Sometimes useful for ad hoc data manipulation, fixing erroneous data, etc. EMPNO ENAME 7839 KING 7782 CLARK 7934 MILLER EMPNO ROLE_NAME 7839 DEFAULT 7782 SALES EMP EMP_ROLES • Assign the role ACCOUNTING to each employee of deptno=10 • If an employee is assigned the DEFAULT role, overwrite this assignment. EMPNO ROLE_NAME 7839 ACCOUNTING 7782 SALES 7782 ACCOUNTING 7934 ACCOUNTING EMP_ROLES MERGE?
  • 23. ORA-38104 • The straightforward approach doesn’t work: SQL> MERGE INTO emp_roles t 2 USING (SELECT empno, 'ACCOUNTING' role_name, 'DEFAULT' old_role 3 FROM emp 4 WHERE deptno = 10 5 ) q 6 ON (t.empno = q.empno and t.role_name = q.old_role) 7 WHEN MATCHED THEN UPDATE SET t.role_name = q.role_name 8 WHEN NOT MATCHED THEN INSERT VALUES (q.empno, q.role_name) ; ON (t.empno = q.empno and t.role_name = q.old_role) * ERROR at line 6: ORA-38104: Columns referenced in the ON Clause cannot be updated: "T"."ROLE_NAME"
  • 24. ORA-38104 • Using WHERE clause instead of ON only seems to work, because the result is wrong: no new role assignment for MILLER (7782) EMPNO ROLE_NAME 7839 ACCOUNTING 7782 SALES 7934 ACCOUNTING EMP_ROLES SQL> MERGE INTO emp_roles t 2 USING (SELECT empno, 'ACCOUNTING' role_name 3 FROM emp 4 WHERE deptno =10 5 ) q 6 ON (t.empno = q.empno) 7 WHEN MATCHED THEN UPDATE 8 SET role_name = q.role_name 9 WHERE t.role_name = 'DEFAULT' 10 WHEN NOT MATCHED THEN INSERT (empno, role_name) 11 VALUES (q.empno, q.role_name) ; 2 rows merged.
  • 25. ORA-38104 – Using ROWID • Doing the whole logic in USING subquery and merging on ROWID • At the price of increased complexity and performance penalty of one more join EMPNO ROLE_NAME 7839 ACCOUNTING 7782 SALES 7782 ACCOUNTING 7934 ACCOUNTING EMP_ROLES SQL> MERGE INTO emp_roles t 2 USING (SELECT r.rowid rid, 'ACCOUNTING' new_role_name 3 , e.empno 4 FROM emp e LEFT JOIN emp_roles r 5 ON e.empno = r.empno 6 AND r.role_name = 'DEFAULT' 7 WHERE e.deptno = 10 8 ) q 9 ON (t.rowid = q.rid ) 10 WHEN MATCHED THEN UPDATE 11 SET role_name = q.new_role_name 12 WHEN NOT MATCHED THEN INSERT (empno, role_name) 13 VALUES (q.empno, q.new_role_name) ; 3 rows merged.
  • 26. ORA-38104 – Fooling the Parser • Using a subquery EMPNO ROLE_NAME 7839 ACCOUNTING 7782 SALES 7782 ACCOUNTING 7934 ACCOUNTING EMP_ROLES SQL> MERGE INTO emp_roles t 2 USING (SELECT empno, 'ACCOUNTING' role_name 3 , 'DEFAULT' old_role 4 FROM emp 5 WHERE deptno = 10 6 ) q 7 ON ( t.empno = q.empno 8 AND (SELECT t.role_name FROM dual) = q.old_role ) 9 WHEN MATCHED THEN UPDATE SET role_name = q.role_name 10 WHEN NOT MATCHED THEN INSERT (empno, role_name) 11 VALUES (q.empno, q.role_name) ; 3 rows merged. Idee: Blog Lukas Eder
  • 27. ORA-38104 – Fooling the Parser • Using a view as a merge target and hiding a column inside NVL() EMPNO ROLE_NAME 7839 ACCOUNTING 7782 SALES 7782 ACCOUNTING 7934 ACCOUNTING EMP_ROLES SQL> MERGE INTO 2 (SELECT empno, role_name 3 , nvl(role_name,'NEVER') check_role_name 4 FROM emp_roles) t 5 USING (SELECT empno, 'ACCOUNTING' role_name 6 , 'DEFAULT' old_role 7 FROM emp 8 WHERE deptno = 10 ) q 9 ON ( t.empno = q.empno 10 AND t.check_role_name = q.old_role ) 11 WHEN MATCHED THEN UPDATE SET role_name = q.role_name 12 WHEN NOT MATCHED THEN INSERT (empno, role_name) 13 VALUES (q.empno, q.role_name) ; 3 rows merged. Idee: Blog Lukas Eder
  • 28. ORA-38104 – Fooling the Parser • Using row value expressions EMPNO ROLE_NAME 7839 ACCOUNTING 7782 SALES 7782 ACCOUNTING 7934 ACCOUNTING EMP_ROLES SQL> MERGE INTO emp_roles t 2 USING (SELECT empno, 'ACCOUNTING' role_name 3 , 'DEFAULT' old_role 4 FROM emp 5 WHERE deptno = 10 ) q 6 ON ( 1=2 OR 7 (t.empno, t.role_name) = ((q.empno, q.old_role)) ) 8 WHEN MATCHED THEN UPDATE SET role_name = q.role_name 9 WHEN NOT MATCHED THEN INSERT (empno, role_name) 10 VALUES (q.empno, q.role_name) ; 3 rows merged. Idee: Blog Lukas Eder
  • 29. ORA-38104 – Can be executed multiple times • All previous examples could only be executed once! This one is “multi-executable” EMPNO ROLE_NAME 7839 ACCOUNTING 7782 SALES 7782 ACCOUNTING 7934 ACCOUNTING EMP_ROLES SQL> MERGE INTO emp_roles t 2 USING ( 3 SELECT DISTINCT 4 FIRST_VALUE(r.rowid) 5 OVER(PARTITION BY e.empno 6 ORDER BY DECODE(r.role_name, 7 'ACCOUNTING',1,2)) rid 8 , e.empno, 'ACCOUNTING' new_role_name 9 FROM emp e LEFT JOIN emp_roles r 10 ON e.empno = r.empno 11 AND r.role_name in ('DEFAULT', 'ACCOUNTING') 12 WHERE e.deptno = 10 13 ) q 14 ON (t.rowid = q.rid ) 15 WHEN MATCHED THEN UPDATE SET role_name = q.new_role_name 16 WHEN NOT MATCHED THEN INSERT (empno, role_name) 17 VALUES (q.empno, q.new_role_name) ; 3 rows merged.
  • 31. Direct Path and MERGE? SQL> MERGE /*+ append */ 2 INTO tx2 USING tx ON (tx.n = tx2.n) 3 WHEN NOT MATCHED THEN INSERT (N) VALUES (tx.n) ; 10,000 rows merged. ---------------------------------------- | Id | Operation | Name | ---------------------------------------- | 0 | MERGE STATEMENT | | | 1 | MERGE | TX2 | | 2 | VIEW | | |* 3 | HASH JOIN RIGHT OUTER| | | 4 | TABLE ACCESS FULL | TX2 | | 5 | TABLE ACCESS FULL | TX | ---------------------------------------- SQL> SELECT count(*) FROM tx2; ORA-12838: cannot read/modify an object after modifying it in parallel SQL> INSERT /*+ append*/ INTO tx2 2 SELECT n FROM tx 3 WHERE n NOT IN (SELECT n FROM tx2); 10,000 rows inserted. ----------------------------------------------- | Id | Operation | Name ----------------------------------------------- | 0 | INSERT STATEMENT | | 1 | LOAD AS SELECT | TX2 | 2 | OPTIMIZER STATISTICS GATHERING | |* 3 | HASH JOIN RIGHT ANTI NA | | 4 | TABLE ACCESS FULL | TX2 | 5 | TABLE ACCESS FULL | TX ----------------------------------------------- direct path write has happend
  • 32. Space Management with PDML and MERGE? SQL> MERGE /*+ append parallel*/ 2 INTO t_tgt_join t0 3 USING ( SELECT ... ---------------------------------------- | Id | Operation | ---------------------------------------- | 0 | MERGE STATEMENT | | 1 | PX COORDINATOR | | 2 | PX SEND QC (RANDOM) | | 3 | MERGE | | 4 | PX RECEIVE | SEGMENT_NAME BLOCKS CNT ------------ ------ ------- T_TGT_JOIN 8 1088 ... 7 rows ... T_TGT_JOIN 128 4647 ... 20 rows ... T_TGT_JOIN 1024 34 30 rows selected. SQL> INSERT /*+ append parallel */ 2 INTO t_tgt_join t0 3 SELECT ... -------------------------------------------------- |Id | Operation -------------------------------------------------- | 0 | INSERT STATEMENT | 1 | PX COORDINATOR | 2 | PX SEND QC (RANDOM) | 3 | LOAD AS SELECT (HIGH WATER MARK BROKERED) | 4 | OPTIMIZER STATISTICS GATHERING SEGMENT_NAME BLOCKS CNT ------------ ---------- --------- T_TGT_JOIN 8 1024 T_TGT_JOIN 128 4216 T_TGT_JOIN 256 20 T_TGT_JOIN 384 2 T_TGT_JOIN 512 8 T_TGT_JOIN 640 8 T_TGT_JOIN 768 4 T_TGT_JOIN 896 1 T_TGT_JOIN 1024 134
  • 33. Conclusion • It‘s easy to find out the reason for ORA-30926 • But be careful when fixing it only by technical methods. Another discussion with business stakeholders may be necessary. • Don’t execute heavy batch DML in environments with large user activity. • Don‘t use any non-transactional logic inside the triggers • Be careful overcoming ORA-38104 restriction. Rethink your use case if possible. • Keep in mind a different behavior for PDML and Online Statistics Gathering
  • 34. Links • Oracle documentation, MERGE • Tom Kyte about DML restarts I and II • Ruslan Dautkhanov, Oracle’s Write Consistency • Lukas Eder about ORA-38104 • MERGE and ORA-30926