SlideShare a Scribd company logo
1 of 4
Last week I was frustrated by the performance of Update in the Reporting SQL. It was a Correlated Update on a
table (let’s say Target table) with around 21K records. The source table is equal to that target table and without
index on it.
The update SQL is like below:
UPDATE TMP_ACT1_20130310sub
SET NAME =
(SELECT DISTINCT NAME FROM TMP_ADDR1_20130310 tmp2
WHERE tmp2.subscriber_no = sub.subscriber_no AND NAME IS NOT NULL)

The sub-query will be executed as much as the number of row in the outer/ driving table. With 21K records,
Oracle will do FTS on the TMP_ADDR1_20130310 21K times. That is a HUGE number, and for sure a HUGE I/O.
For this case, we can simply create index on the inner table to reduce the I/O and speed up the execution time.
My headache was gone after creating index on TMP_ADDR1_20130310. Mission accomplished!!!

Further investigation, actually there are few other different ways when we are dealing with an Update
statement. In this exercise, I created a small comparison between below 3 update methods (so far that I know)
with Full Table Scan and also Index Scan:
-

Correlated Update
Merge
Update from Select

DBA series Correlated Update vs Merge.xlsx

Please go through the Excel sheet for the number 
Some highlighted items:

1. “Correlated Update” is not good at all if we don’t have index access on the inner table. It will produces
huge I/O since we have to FTS the inner table over and over again (as much as the number of rows in
the outer table)
2. If the join condition is not matched, “Correlated Update” will update the row with empty string. We
need to pay attention on this (please check “Data Changes” section below)
It will produce more Undo for those additional update
3. If the join condition is not matched, both “Merge” and “Update from Select” method will leave the row
as is. To me it is more reasonable
4. With “Update from Select” method, we need to create unique index on the inner table, otherwise we
will ended up with ORA- error
5. “Update from Select” method is little bit superior compare to the other 2 methods when we update
small percentage of outer table and there is index on the outer table, so that we can get benefit from
Index Range Scan
Sample Data
SQL>
2
3
4
5
6

create table ttarget
pctfree 99
as
select rownum id, lpad(chr(rownum), 70) data
from dual
connect by rownum<= 1000;

Table created.
SQL>
2
3
4
5
6

create table tsource
pctfree 99
as
select rownum id, lpad(chr(rownum)||'_CHG', 70) data
from dual
connect by rownum<= 500;

Table created.

SQL> exec dbms_stats.gather_table_stats(USER, 'TTARGET');
PL/SQL procedure successfully completed.
SQL> exec dbms_stats.gather_table_stats(USER, 'TSOURCE');
PL/SQL procedure successfully completed.

SQL> select table_name, blocks, empty_blocks, num_rows
2 from user_tables
3 where table_name in ('TTARGET', 'TSOURCE');
TABLE_NAME
BLOCKS EMPTY_BLOCKS
NUM_ROWS
------------------------------ ---------- ------------ ---------TSOURCE
518
0
500
TTARGET
1024
0
1000

SQL> create unique index idx_tsource on tsource(id);
Index created.
SQL> select
leaf_blocks,distinct_keys,avg_leaf_blocks_per_key,avg_data_blocks_per_key,cluste
ring_factor,uniqueness from user_indexes where index_name = 'IDX_TSOURCE';
LEAF_BLOCKS DISTINCT_KEYS AVG_LEAF_BLOCKS_PER_KEY AVG_DATA_BLOCKS_PER_KEY
CLUSTERING_FACTOR UNIQUENES
----------- ------------- ----------------------- ----------------------- ---------------- --------1
500
1
1
500 UNIQUE

SQL> create index idx_ttarget on ttarget (id);
Index created.
Data Changes
Correlated Update
For un-matched rows, we can see DATA is CHANGED
SQL> select id, trim(data) from ttarget where id > 500 and rownum< 3;
ID TRIM(DATA)
---------- -------------------501 )
502 ÷
SQL> select count(*) from ttarget where data like '%CHG%';
COUNT(*)
---------0
SQL> update ttarget a set data = (select data from tsource b where a.id=b.id);
1000 rows updated.
SQL> select count(*) from ttarget where data like '%CHG%';
COUNT(*)
---------500
SQL> select id, trim(data) from ttarget where id > 500 and rownum< 3;
ID TRIM(DATA)
---------- -------------------501
502

Merge
For un-matched rows, we can see data is not changed
SQL> select id, trim(data) from ttarget where id > 500 and rownum< 3;
ID TRIM(DATA)
---------- -------------------501 )
502 ÷
SQL> select count(*) from ttarget where data like '%CHG%';
COUNT(*)
---------0
SQL>
2
3
4
5

merge into ttarget a
using tsource b
on (a.id=b.id)
when matched then
update set a.data=b.data;

500 rows merged.
SQL> select count(*) from ttarget where data like '%CHG%';
COUNT(*)
---------500
SQL> select id, trim(data) from ttarget where id > 500 and rownum< 3;
ID TRIM(DATA)
---------- -------------------501 )
502 ÷

Update from Select
For un-matched rows, we can see data is not changed
SQL> select id, trim(data) from ttarget where id > 500 and rownum< 3;
ID TRIM(DATA)
---------- -------------------501 )
502 ÷
SQL> select count(*) from ttarget where data like '%CHG%';
COUNT(*)
---------0
SQL> update
2 (select a.datadtarget, b.datadsource
3
from ttarget a, tsource b
4
where a.id=b.id) c
5 set c.dtarget=c.dsource;
500 rows merged.
SQL> select count(*) from ttarget where data like '%CHG%';
COUNT(*)
---------500
SQL> select id, trim(data) from ttarget where id > 500 and rownum< 3;
ID TRIM(DATA)
---------- -------------------501 )
502 ÷

More Related Content

What's hot

Circular linked list
Circular linked listCircular linked list
Circular linked listmaamir farooq
 
CIS 336 Final Exam 2 (Devry)s
CIS 336 Final Exam 2 (Devry)sCIS 336 Final Exam 2 (Devry)s
CIS 336 Final Exam 2 (Devry)scritterc07
 
Sas-training-in-mumbai
Sas-training-in-mumbaiSas-training-in-mumbai
Sas-training-in-mumbaiUnmesh Baile
 
Merging tables using R
Merging tables using R Merging tables using R
Merging tables using R Rupak Roy
 
Unit ii(dsc++)
Unit ii(dsc++)Unit ii(dsc++)
Unit ii(dsc++)Durga Devi
 
R code descriptive statistics of phenotypic data by Avjinder Kaler
R code descriptive statistics of phenotypic data by Avjinder KalerR code descriptive statistics of phenotypic data by Avjinder Kaler
R code descriptive statistics of phenotypic data by Avjinder KalerAvjinder (Avi) Kaler
 
Deletion from single way linked list and search
Deletion from single way linked list and searchDeletion from single way linked list and search
Deletion from single way linked list and searchEstiak Khan
 
Session 8 connect your universal application with database .. builders & deve...
Session 8 connect your universal application with database .. builders & deve...Session 8 connect your universal application with database .. builders & deve...
Session 8 connect your universal application with database .. builders & deve...Moatasim Magdy
 
STACK, LINKED LIST ,AND QUEUE
STACK, LINKED LIST ,AND QUEUESTACK, LINKED LIST ,AND QUEUE
STACK, LINKED LIST ,AND QUEUEDev Chauhan
 
Doubly & Circular Linked Lists
Doubly & Circular Linked ListsDoubly & Circular Linked Lists
Doubly & Circular Linked ListsAfaq Mansoor Khan
 
Export Data using R Studio
Export Data using R StudioExport Data using R Studio
Export Data using R StudioRupak Roy
 
Sql Queries
Sql QueriesSql Queries
Sql Querieswebicon
 

What's hot (20)

MYSql manage db
MYSql manage dbMYSql manage db
MYSql manage db
 
Circular linked list
Circular linked listCircular linked list
Circular linked list
 
CIS 336 Final Exam 2 (Devry)s
CIS 336 Final Exam 2 (Devry)sCIS 336 Final Exam 2 (Devry)s
CIS 336 Final Exam 2 (Devry)s
 
Sas-training-in-mumbai
Sas-training-in-mumbaiSas-training-in-mumbai
Sas-training-in-mumbai
 
MYSQL join
MYSQL joinMYSQL join
MYSQL join
 
Merging tables using R
Merging tables using R Merging tables using R
Merging tables using R
 
Unit ii(dsc++)
Unit ii(dsc++)Unit ii(dsc++)
Unit ii(dsc++)
 
R code descriptive statistics of phenotypic data by Avjinder Kaler
R code descriptive statistics of phenotypic data by Avjinder KalerR code descriptive statistics of phenotypic data by Avjinder Kaler
R code descriptive statistics of phenotypic data by Avjinder Kaler
 
Sorting
SortingSorting
Sorting
 
Deletion from single way linked list and search
Deletion from single way linked list and searchDeletion from single way linked list and search
Deletion from single way linked list and search
 
Session 8 connect your universal application with database .. builders & deve...
Session 8 connect your universal application with database .. builders & deve...Session 8 connect your universal application with database .. builders & deve...
Session 8 connect your universal application with database .. builders & deve...
 
STACK, LINKED LIST ,AND QUEUE
STACK, LINKED LIST ,AND QUEUESTACK, LINKED LIST ,AND QUEUE
STACK, LINKED LIST ,AND QUEUE
 
linked list
linked listlinked list
linked list
 
MySql: Queries
MySql: QueriesMySql: Queries
MySql: Queries
 
Doubly & Circular Linked Lists
Doubly & Circular Linked ListsDoubly & Circular Linked Lists
Doubly & Circular Linked Lists
 
Export Data using R Studio
Export Data using R StudioExport Data using R Studio
Export Data using R Studio
 
Sql Queries
Sql QueriesSql Queries
Sql Queries
 
Linked Lists
Linked ListsLinked Lists
Linked Lists
 
Linked lists
Linked listsLinked lists
Linked lists
 
Singly & Circular Linked list
Singly & Circular Linked listSingly & Circular Linked list
Singly & Circular Linked list
 

Similar to Correlated update vs merge

Myth busters - performance tuning 101 2007
Myth busters - performance tuning 101 2007Myth busters - performance tuning 101 2007
Myth busters - performance tuning 101 2007paulguerin
 
Checking clustering factor to detect row migration
Checking clustering factor to detect row migrationChecking clustering factor to detect row migration
Checking clustering factor to detect row migrationHeribertus Bramundito
 
Migration from mysql to elasticsearch
Migration from mysql to elasticsearchMigration from mysql to elasticsearch
Migration from mysql to elasticsearchRyosuke Nakamura
 
SQL/MX 3.6 Select for update feature
SQL/MX 3.6 Select for update featureSQL/MX 3.6 Select for update feature
SQL/MX 3.6 Select for update featureFrans Jongma
 
How to teach an elephant to rock'n'roll
How to teach an elephant to rock'n'rollHow to teach an elephant to rock'n'roll
How to teach an elephant to rock'n'rollPGConf APAC
 
SQL Database Performance Tuning for Developers
SQL Database Performance Tuning for DevelopersSQL Database Performance Tuning for Developers
SQL Database Performance Tuning for DevelopersBRIJESH KUMAR
 
2. DBMS Experiment - Lab 2 Made in SQL Used
2. DBMS Experiment - Lab 2 Made in SQL Used2. DBMS Experiment - Lab 2 Made in SQL Used
2. DBMS Experiment - Lab 2 Made in SQL UsedTheVerse1
 
Prabu's sql quries
Prabu's sql quries Prabu's sql quries
Prabu's sql quries Prabu Cse
 
PPT of Common Table Expression (CTE), Window Functions, JOINS, SubQuery
PPT  of Common Table Expression (CTE), Window Functions, JOINS, SubQueryPPT  of Common Table Expression (CTE), Window Functions, JOINS, SubQuery
PPT of Common Table Expression (CTE), Window Functions, JOINS, SubQueryAbhishek590097
 
Sql notes, sql server,sql queries,introduction of SQL, Beginner in SQL
Sql notes, sql server,sql queries,introduction of SQL, Beginner in SQLSql notes, sql server,sql queries,introduction of SQL, Beginner in SQL
Sql notes, sql server,sql queries,introduction of SQL, Beginner in SQLPrashant Kumar
 
SQLSERVERQUERIES.pptx
SQLSERVERQUERIES.pptxSQLSERVERQUERIES.pptx
SQLSERVERQUERIES.pptxssuser6bf2d1
 
Sql Server 2008 New Programmability Features
Sql Server 2008 New Programmability FeaturesSql Server 2008 New Programmability Features
Sql Server 2008 New Programmability Featuressqlserver.co.il
 
Oracle tips and tricks
Oracle tips and tricksOracle tips and tricks
Oracle tips and tricksYanli Liu
 
MySQL Indexing : Improving Query Performance Using Index (Covering Index)
MySQL Indexing : Improving Query Performance Using Index (Covering Index)MySQL Indexing : Improving Query Performance Using Index (Covering Index)
MySQL Indexing : Improving Query Performance Using Index (Covering Index)Hemant Kumar Singh
 

Similar to Correlated update vs merge (20)

Myth busters - performance tuning 101 2007
Myth busters - performance tuning 101 2007Myth busters - performance tuning 101 2007
Myth busters - performance tuning 101 2007
 
Checking clustering factor to detect row migration
Checking clustering factor to detect row migrationChecking clustering factor to detect row migration
Checking clustering factor to detect row migration
 
Sql
SqlSql
Sql
 
Migration from mysql to elasticsearch
Migration from mysql to elasticsearchMigration from mysql to elasticsearch
Migration from mysql to elasticsearch
 
SQL/MX 3.6 Select for update feature
SQL/MX 3.6 Select for update featureSQL/MX 3.6 Select for update feature
SQL/MX 3.6 Select for update feature
 
How to teach an elephant to rock'n'roll
How to teach an elephant to rock'n'rollHow to teach an elephant to rock'n'roll
How to teach an elephant to rock'n'roll
 
SQL Database Performance Tuning for Developers
SQL Database Performance Tuning for DevelopersSQL Database Performance Tuning for Developers
SQL Database Performance Tuning for Developers
 
Babitha2.mysql
Babitha2.mysqlBabitha2.mysql
Babitha2.mysql
 
Babitha2 Mysql
Babitha2 MysqlBabitha2 Mysql
Babitha2 Mysql
 
2. DBMS Experiment - Lab 2 Made in SQL Used
2. DBMS Experiment - Lab 2 Made in SQL Used2. DBMS Experiment - Lab 2 Made in SQL Used
2. DBMS Experiment - Lab 2 Made in SQL Used
 
Prabu's sql quries
Prabu's sql quries Prabu's sql quries
Prabu's sql quries
 
PPT of Common Table Expression (CTE), Window Functions, JOINS, SubQuery
PPT  of Common Table Expression (CTE), Window Functions, JOINS, SubQueryPPT  of Common Table Expression (CTE), Window Functions, JOINS, SubQuery
PPT of Common Table Expression (CTE), Window Functions, JOINS, SubQuery
 
Les09 Manipulating Data
Les09 Manipulating DataLes09 Manipulating Data
Les09 Manipulating Data
 
Sql notes, sql server,sql queries,introduction of SQL, Beginner in SQL
Sql notes, sql server,sql queries,introduction of SQL, Beginner in SQLSql notes, sql server,sql queries,introduction of SQL, Beginner in SQL
Sql notes, sql server,sql queries,introduction of SQL, Beginner in SQL
 
Assignment#02
Assignment#02Assignment#02
Assignment#02
 
SQLSERVERQUERIES.pptx
SQLSERVERQUERIES.pptxSQLSERVERQUERIES.pptx
SQLSERVERQUERIES.pptx
 
Sql Server 2008 New Programmability Features
Sql Server 2008 New Programmability FeaturesSql Server 2008 New Programmability Features
Sql Server 2008 New Programmability Features
 
Oracle tips and tricks
Oracle tips and tricksOracle tips and tricks
Oracle tips and tricks
 
Les09
Les09Les09
Les09
 
MySQL Indexing : Improving Query Performance Using Index (Covering Index)
MySQL Indexing : Improving Query Performance Using Index (Covering Index)MySQL Indexing : Improving Query Performance Using Index (Covering Index)
MySQL Indexing : Improving Query Performance Using Index (Covering Index)
 

Recently uploaded

Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.Curtis Poe
 
Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfRankYa
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024Stephanie Beckett
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Manik S Magar
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxLoriGlavin3
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsPixlogix Infotech
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxNavinnSomaal
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningLars Bell
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteDianaGray10
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 

Recently uploaded (20)

Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.
 
Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdf
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and Cons
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptx
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine Tuning
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test Suite
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 

Correlated update vs merge

  • 1. Last week I was frustrated by the performance of Update in the Reporting SQL. It was a Correlated Update on a table (let’s say Target table) with around 21K records. The source table is equal to that target table and without index on it. The update SQL is like below: UPDATE TMP_ACT1_20130310sub SET NAME = (SELECT DISTINCT NAME FROM TMP_ADDR1_20130310 tmp2 WHERE tmp2.subscriber_no = sub.subscriber_no AND NAME IS NOT NULL) The sub-query will be executed as much as the number of row in the outer/ driving table. With 21K records, Oracle will do FTS on the TMP_ADDR1_20130310 21K times. That is a HUGE number, and for sure a HUGE I/O. For this case, we can simply create index on the inner table to reduce the I/O and speed up the execution time. My headache was gone after creating index on TMP_ADDR1_20130310. Mission accomplished!!! Further investigation, actually there are few other different ways when we are dealing with an Update statement. In this exercise, I created a small comparison between below 3 update methods (so far that I know) with Full Table Scan and also Index Scan: - Correlated Update Merge Update from Select DBA series Correlated Update vs Merge.xlsx Please go through the Excel sheet for the number  Some highlighted items: 1. “Correlated Update” is not good at all if we don’t have index access on the inner table. It will produces huge I/O since we have to FTS the inner table over and over again (as much as the number of rows in the outer table) 2. If the join condition is not matched, “Correlated Update” will update the row with empty string. We need to pay attention on this (please check “Data Changes” section below) It will produce more Undo for those additional update 3. If the join condition is not matched, both “Merge” and “Update from Select” method will leave the row as is. To me it is more reasonable 4. With “Update from Select” method, we need to create unique index on the inner table, otherwise we will ended up with ORA- error 5. “Update from Select” method is little bit superior compare to the other 2 methods when we update small percentage of outer table and there is index on the outer table, so that we can get benefit from Index Range Scan
  • 2. Sample Data SQL> 2 3 4 5 6 create table ttarget pctfree 99 as select rownum id, lpad(chr(rownum), 70) data from dual connect by rownum<= 1000; Table created. SQL> 2 3 4 5 6 create table tsource pctfree 99 as select rownum id, lpad(chr(rownum)||'_CHG', 70) data from dual connect by rownum<= 500; Table created. SQL> exec dbms_stats.gather_table_stats(USER, 'TTARGET'); PL/SQL procedure successfully completed. SQL> exec dbms_stats.gather_table_stats(USER, 'TSOURCE'); PL/SQL procedure successfully completed. SQL> select table_name, blocks, empty_blocks, num_rows 2 from user_tables 3 where table_name in ('TTARGET', 'TSOURCE'); TABLE_NAME BLOCKS EMPTY_BLOCKS NUM_ROWS ------------------------------ ---------- ------------ ---------TSOURCE 518 0 500 TTARGET 1024 0 1000 SQL> create unique index idx_tsource on tsource(id); Index created. SQL> select leaf_blocks,distinct_keys,avg_leaf_blocks_per_key,avg_data_blocks_per_key,cluste ring_factor,uniqueness from user_indexes where index_name = 'IDX_TSOURCE'; LEAF_BLOCKS DISTINCT_KEYS AVG_LEAF_BLOCKS_PER_KEY AVG_DATA_BLOCKS_PER_KEY CLUSTERING_FACTOR UNIQUENES ----------- ------------- ----------------------- ----------------------- ---------------- --------1 500 1 1 500 UNIQUE SQL> create index idx_ttarget on ttarget (id); Index created.
  • 3. Data Changes Correlated Update For un-matched rows, we can see DATA is CHANGED SQL> select id, trim(data) from ttarget where id > 500 and rownum< 3; ID TRIM(DATA) ---------- -------------------501 ) 502 ÷ SQL> select count(*) from ttarget where data like '%CHG%'; COUNT(*) ---------0 SQL> update ttarget a set data = (select data from tsource b where a.id=b.id); 1000 rows updated. SQL> select count(*) from ttarget where data like '%CHG%'; COUNT(*) ---------500 SQL> select id, trim(data) from ttarget where id > 500 and rownum< 3; ID TRIM(DATA) ---------- -------------------501 502 Merge For un-matched rows, we can see data is not changed SQL> select id, trim(data) from ttarget where id > 500 and rownum< 3; ID TRIM(DATA) ---------- -------------------501 ) 502 ÷ SQL> select count(*) from ttarget where data like '%CHG%'; COUNT(*) ---------0 SQL> 2 3 4 5 merge into ttarget a using tsource b on (a.id=b.id) when matched then update set a.data=b.data; 500 rows merged. SQL> select count(*) from ttarget where data like '%CHG%';
  • 4. COUNT(*) ---------500 SQL> select id, trim(data) from ttarget where id > 500 and rownum< 3; ID TRIM(DATA) ---------- -------------------501 ) 502 ÷ Update from Select For un-matched rows, we can see data is not changed SQL> select id, trim(data) from ttarget where id > 500 and rownum< 3; ID TRIM(DATA) ---------- -------------------501 ) 502 ÷ SQL> select count(*) from ttarget where data like '%CHG%'; COUNT(*) ---------0 SQL> update 2 (select a.datadtarget, b.datadsource 3 from ttarget a, tsource b 4 where a.id=b.id) c 5 set c.dtarget=c.dsource; 500 rows merged. SQL> select count(*) from ttarget where data like '%CHG%'; COUNT(*) ---------500 SQL> select id, trim(data) from ttarget where id > 500 and rownum< 3; ID TRIM(DATA) ---------- -------------------501 ) 502 ÷