473,788 Members | 2,759 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

MERGE in V8.2 FP4 (on condition returns multiple rows from target)

Hi:

This is a merge questions which has been posted and answered... in my
case need more clairification when target table (tableB) matched
multiple rows to be updated based on the ON condition for a single row
from source table (tableA). There is a one to many relationship from
source to target table (because of the ON condition). We also know
upfront that thsi merge will result in all update and no inserts.

The source table is tableA and target is tableB. tableA has 2.5 million
rows and tableB after applying ON condition qualifies 8 million rows
for update only.

DB2 can update multiple rows on target tableB (tested it using the
sample database). But in this case, the merge is very slow and runs
forvever... I can only see it reading rows from application snapshot
which is in billions. There are indexes on both the tables to satifsy
the ON condition and the where conditions. db2advis has nothing to
advise here :)

Thanks for your help!

Vijay
The merge statement is:

merge into tableB as B
using (select cola, colb.... colz
from tableA
where col2 = 1266
and col3 ='C'
and batchno = '200602'
) as A
on B.col1 = A.col1
and B.col2 = A.col2
and B.col2 = 1266
and B.col3 IN ('C','A','D','P ')
and B.col4 <= '200602'
when matched then UPDATE
set cola = ... colb = ... colz =
when NOT matched then INSERT
(list of columns) VALUES (list of column values)

Apr 6 '06 #1
11 2560
I would run runstats on the tables and indexes with distribution on key
columns. If that does not help, then alter the tables to volatile to
encourage index usage.

Also consider installing the latest fixpack (FP11). FP4 is quite old,
and had more bugs than an ant farm.

Apr 6 '06 #2
Thanks for the inputs. The database is on 8.2 FP4 = 8.1 FP11 :)
Have current statistics for both the tables... I will give volaitle a
try.
Thanks again.
Vijay

Apr 6 '06 #3
UDBDBA wrote:
We also know
upfront that thsi merge will result in all update and no inserts.

If you know there will be no INSERT, why do you specify the WHEN NOT
MATCHED branch then?

It would be interesting to see the db2exfmt output after removing INSERT.

Cheers
Serge
--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab
Apr 6 '06 #4
Thanks Serge for looking into this splurging merge!

I had the query initially without the insert section and that ran
forever... So i modified and tried after adding insert section, still
runs forever. All rows from source table needs to be processed/merged
with target table. I have enabled VOLATILE for both target & source
table.

The plan for the statement without insert section is given below.

Access Plan:
-----------
Total Cost: 2.79767e+06
Query Degree: 0

Rows
RETURN
( 1)
Cost
I/O
|
98031.8
UPDATE
( 2)
2.79767e+06
340016
/----+----\
98031.8 5.03874e+07
FETCH TABLE: STELLA
( 3) TARGET_TABLE
1.53744e+06
241984
/---+---\
98031.8 5.03874e+07
FILTER TABLE: STELLA
( 4) TARGET_TABLE
276995
143952
|
2.4508e+06
FILTER
( 5)
276002
143952
|
2.4508e+06
TBSCAN
( 6)
271953
143952
|
2.4508e+06
SORT
( 7)
269658
143952
|
2.4508e+06
HSJOIN
( 8)
263207
143952
/-----------+-----------\
418199 2.4508e+06
FETCH FETCH
( 9) ( 11)
40262.6 221849
5937.19 138015
/---+---\ /----+---\
418199 5.03874e+07 2.4508e+06 2.4508e+06
IXSCAN TABLE: STELLA IXSCAN TABLE: STAGE
( 10) TARGET_TABLE ( 12) SOURCE_TABLE
1261.09 19572.5
90.4042 1829
| |
5.03874e+07 2.4508e+06
INDEX: STELLA INDEX: DB2DW1
IX_TARGETTBL_MD C IDX_SOURCETBL

Apr 6 '06 #5
UDBDBA wrote:
/---+---\
98031.8 5.03874e+07
FILTER TABLE: STELLA
( 4) TARGET_TABLE
276995
143952
|
2.4508e+06
FILTER
( 5)
276002
143952
|
2.4508e+06
TBSCAN
( 6)
271953
143952
|
2.4508e+06
SORT
( 7)
269658
143952 This stack does the accounting to assure that each row in the target is
matched by at most one row in the source.
It gets added when Db2 fails to prove that the ON clause identifies
unique rows.
Now, semantically I don't see why it should care if a a single source
matches multiple targets.on B.col1 = A.col1
and B.col2 = A.col2

Is tableA(col1, col2) a unique key?

Cheers
Serge

PS; I hate hashjoins with MERGE. Drop the optimization level to 3
(disable hashjoin) and see what happens.

--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab
Apr 6 '06 #6
Yes. FOr the source tableA(col1,col 2) is unique (infact col1 is unique
on tableA). But the same col1 on target tableB is not unique.

Counts for tableA: Source table
=============== ==========
count(distinct col1) = 2450795
count(distinct col2) = 1
count(col1) = 2450795
count(col2) = 2450795

Counts for tableB: Target table
=============== ==========
count(distinct col1) = 15197539
count(distinct col2) = 1
count(col1) = 50387384
count(col2) = 7950845

Thanks for your time & appreciate all the help.

Vijay

Apr 7 '06 #7
Correction on the counts:

For counts on tableB: Target table
=============== ============
count(distinct col1) = 2450795
count(distinct col2) = 1
count(col1) = 50387384
count(col2) = 2450795

Thanks.

Vijay

Apr 7 '06 #8
Hi Serge:
I turned volatile ON and gave selectivity of 0.00001 for col2, col3 and
col4 (on all = predicates). This time the merge finished in 84 minutes
at optimizer level 5. That's a major improvement compared to running
forever. And you were right about HSJOIN, The new plan has NLJOIN and
does improve merge performance.

By using selectivity, did i just get lucky?

Here is the new access plan:

Access Plan:
-----------
Total Cost: 48.673
Query Degree: 0

Rows
RETURN
( 1)
Cost
I/O
|
9.80318e-06
UPDATE
( 2)
48.673
4.3619
/----+----\
9.80318e-06 5.03874e+07
FILTER TABLE: STELLA
( 3) TARGET_TABLE
48.6726
4.36189
|
0.000245079
NLJOIN
( 4)
48.6726
4.36189
/-----------+-----------\
0.000245079 5.23167e-11
FETCH FETCH
( 5) ( 7)
35.8071 25.7169
3.36186 2.00003
/---+---\ /----+---\
24.5079 2.4508e+06 3.3155e-05 5.03874e+07
IXSCAN TABLE: STAGE IXSCAN TABLE: STELLA
( 6) SOURCE_TABLE ( 8) TARGET_TABLE
21.2964 25.7164
2 2
| |
2.4508e+06 5.03874e+07
INDEX: DB2DW1 INDEX: DB2DW1
IDX604012212290 0 IDX604012214100 0

thanks!

Vijay

Apr 7 '06 #9
UDBDBA wrote:
Hi Serge:
I turned volatile ON and gave selectivity of 0.00001 for col2, col3 and
col4 (on all = predicates). This time the merge finished in 84 minutes
at optimizer level 5. That's a major improvement compared to running
forever. And you were right about HSJOIN, The new plan has NLJOIN and
does improve merge performance.

By using selectivity, did i just get lucky?

Here is the new access plan:

Access Plan:
-----------
Total Cost: 48.673
Query Degree: 0

Rows
RETURN
( 1)
Cost
I/O
|
9.80318e-06
UPDATE
( 2)
48.673
4.3619
/----+----\
9.80318e-06 5.03874e+07
FILTER TABLE: STELLA
( 3) TARGET_TABLE
48.6726
4.36189
|
0.000245079
NLJOIN
( 4)
48.6726
4.36189
/-----------+-----------\
0.000245079 5.23167e-11
FETCH FETCH
( 5) ( 7)
35.8071 25.7169
3.36186 2.00003
/---+---\ /----+---\
24.5079 2.4508e+06 3.3155e-05 5.03874e+07
IXSCAN TABLE: STAGE IXSCAN TABLE: STELLA
( 6) SOURCE_TABLE ( 8) TARGET_TABLE
21.2964 25.7164
2 2
| |
2.4508e+06 5.03874e+07
INDEX: DB2DW1 INDEX: DB2DW1
IDX604012212290 0 IDX604012214100 0

Perfect plan!
I'm a bit confused because the mess above the join also went away, but I
take it. I imagine that the SELECTIVITY helped to change to NLJOIN.

This is how a well behaved MERGE should look like!
Now you can add your INSERT back it AFTER the UPDATE branch.
This should add a above TEMP (can't be helped) and the INSERT on top of
that.

Cheers
Serge
--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab
Apr 7 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
12388
by: Agoston Bejo | last post by:
Hi, when a SELECT in an INSERT INTO statement returns no rows, NO_DATA_FOUND exception is raised. How do I write a "nice" SELECT INTO when I expect that 0 or 1 row gets returned, and the variable should be NULL if no rows are returned. By "nice" I mean that my intention is clear from the source code and no exception-catching is involved, since that would suggest that I'm handling such a condition that shouldn't occur. Therefore the...
3
49561
by: spartacus | last post by:
Hi, Does SQL support update to multiple rows where values coming from a sub-query? e.g insert into TABLE1 select column1, column2, column3 from TABLE2
3
2289
by: baka | last post by:
Dear Sir/madam Here i am having some one sql which returns more rows than the required rows after comaparing tables simple sql statement will be lokk like SELECT t.empcd, s.daicd, s.chucd, s.shocd, t.juncd, s.shoname FROM temptbl AS t,shomast AS s WHERE t.shoname=s.shoname;
1
2111
by: srinivasdevakumar | last post by:
MY doubt is how to retrive multiple rows from data base on one condition by using JSP. please tell me
18
4012
by: Apple001 | last post by:
Hi all! I am having trouble with joining multiple rows into one row. I will appreciate any help. For columns in the query, I have: invID(autot number), entryDate, invDate, vendor, invoiceAmount from table named tblInvoice, and building, account, percent (allocation in percentage for each building) from table named tblAllocation. This is how the table looks like: InvID / entryDate / invDate / vendor / invoiceAmount / building /...
11
3519
by: giloosh | last post by:
i would like to filter out a bunch rows from a table of sql row results. i have a checkbox on each row named checkbox (x = the current row id) i will submit the form and take all the rows that are checked and filter out the ones that are not checked. so if i had 10 rows and i checked 3 of them then when i submit the form only those 3 rows will show up.
2
5339
by: Michael | last post by:
It seems that a gridview allows us to delete only a single row at a time. How to extend this functionality to select multiple rows and delete all of the selected rows in a single stroke? just like what hotmail web UI is doing now (having the option of selecting multiple rows (using the checkbox provided) and perform a set of operations on them)
3
3927
by: nigelesquire | last post by:
Please help! I'm trying to clone and delete multiple rows with JavaScript. I need two delete buttons that work...! I only have one for now, but it's not working properly, the output count is messing up. Problems:
5
25709
bilibytes
by: bilibytes | last post by:
Hi, i am having big headaches in trying to find out how to perform a very very simple query. having a table like this one: +--------------+----------+---------+ | name | object1 | object2 | +--------------+----------+---------+ | Mr Brown | 1 | 1 |
0
9498
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10373
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10177
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10118
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8995
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7519
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5403
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3677
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2897
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.