473,883 Members | 1,606 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

question: multiple row insert vs. merge

My app needs to insert thousand value rows into a mostly empty table
(data are read from a file). I can either use inserts, or use merge.
The advantage of using merge is that in the few cases where the table
is not empty, it can take care of the updating part, which makes the
app cleaner.

However, my concern is the merge state would slow dowm the insertion
of new data, since in most cases the table is empty.

So my questions (before I run any comprehensive tests) are:

1. would merge slower than insert if it is an empty table?
2. given that we can do multiple row value inserts, would insert be
even faster than merge with which there is no multipe row option?

Thanks a lot!
Sep 2 '08
24 7162
Henry J. wrote:
On Sep 10, 10:27 am, Serge Rielau <srie...@ca.ibm .comwrote:
>Henry J. wrote:
>>On Sep 7, 2:48 pm, Serge Rielau <srie...@ca.ibm .comwrote:
Good. Another question is, if I send a list of inserts each with two
value tuples, like this:
"insert into t values(?,?) (?,?)", instead of a list of "insert into t
values(?,?) ",would it speed up the insertion? Thanks a lot! This is
really helpful, Serge.
Yes. I did some measurements a while ago (very unscientific) and found
that it makes sense to bundle up to 100 rows. After that it doesn't make
a difference. Of course network latency plays a role (my client was local).
Hope this is my last question in this thread. Now I know it is faster
to send a batch of 1000
"insert into t values(?,?) (?,?)" than sending 2000 "insert into t
values(?,?) ". Is it equally fast if i send 1000 merge statements like
"merge into t using table (values(?,?) (?,?) ) ..."? Faster than 2000
"merge into t using table (values(?,?) ) ...", right?
I know I can find out by testing but tips from experts like you guys
save me tons of time ... Really appreciate it.
MERGE is a special child....
If you use a MERGE straight with a multi-row VALUES DB2 does not know
whether the new rows match to unique rows in the MERGE target.
If DB2 can't rely on that the MERGE statement gets a lot more complex
(and slower). Here is how to do it right:

MERGE INTO T USING (SELECT pk, MAX(c1), MAX(c2) FROM (VALUES (?, ?, ?),
...) AS V(pk, c1, c2)) AS S(pk, c1, c2)
ON T.pk = S.pk
WHEN MATCHED THEN UPDATE SET c1 = S.c1, c2 = S.c2
WHEN NOT MATCHED THEN INSERT VALUES (pk, c1, c2)

Not that in a case of 200 single-row merge teh semantics are different.
You can absolutely have dups since teh MERGEs are commulatative (sp??).

Cheers
Serge

--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab- Hide quoted text -

- Show quoted text -

Do you mean that we indeed can achieve similar performance boost using
merge? I'm not sure if I understand your trick with "select
max ...". Are you intended to do a "group by"? Can we use union to
remove dupe as in "values(?,? ,?) union values(?,?,?) union .... "?
Yes, you need a GROUP BY pk. If you want to make it messier use UNION.
But the result is the same.

Cheers
Serge
--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab
Sep 18 '08 #21
On Sep 17, 8:04*pm, Serge Rielau <srie...@ca.ibm .comwrote:
HenryJ. wrote:
On Sep 10, 10:27 am, Serge Rielau <srie...@ca.ibm .comwrote:
>HenryJ. wrote:
On Sep 7, 2:48 pm, Serge Rielau <srie...@ca.ibm .comwrote:
Good. *Another question is, if I send a list of inserts each withtwo
value tuples, like this:
"insert into t values(?,?) (?,?)", instead of a list of "insert into t
values(?,?)" ,would it speed up the insertion? *Thanks a lot! *This is
really helpful, Serge.
Yes. I did some measurements a while ago (very unscientific) and found
that it makes sense to bundle up to 100 rows. After that it doesn't make
a difference. Of course network latency plays a role (my client was local).
Hope this is my last question in this thread. *Now I know it is faster
to send a batch of 1000
"insert into t values(?,?) (?,?)" than sending 2000 "insert into t
values(?,?)" . *Is it equally fast if i send 1000 merge statements like
"merge into t using table (values(?,?) (?,?) ) ..."? *Faster than 2000
"merge into t using table (values(?,?) ) ...", right?
I know I can find out by testing but tips from experts like you guys
save me tons of time ... *Really appreciate it.
MERGE is a special child....
If you use a MERGE straight with a multi-row VALUES DB2 does not know
whether the new rows match to unique rows in the MERGE target.
If DB2 can't rely on that the MERGE statement gets a lot more complex
(and slower). Here is how to do it right:
MERGE INTO T USING (SELECT pk, MAX(c1), MAX(c2) FROM (VALUES (?, ?, ?),
...) AS V(pk, c1, c2)) AS S(pk, c1, c2)
ON T.pk = S.pk
WHEN MATCHED THEN UPDATE SET c1 = S.c1, c2 = S.c2
WHEN NOT MATCHED THEN INSERT VALUES (pk, c1, c2)
Not that in a case of 200 single-row merge teh semantics are different..
You can absolutely have dups since teh MERGEs are commulatative (sp??)..
Cheers
Serge
--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab- Hide quoted text -
- Show quoted text -
Do you mean that we indeed can achieve similar performance boost using
merge? * I'm not sure if I understand your trick with "select
max ...". *Are you intended to do a "group by"? * Can we use union to
remove dupe as in *"values(?,? ,?) union values(?,?,?) union .... "?

Yes, you need a GROUP BY pk. If you want to make it messier use UNION.
But the result is the same.

Cheers
Serge
--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab
So we need to use "group by" to let the optimizer know that there is
no dupe value?

How about "insert into t values (?,?)(?,?)"? It can also contain dupe
values?

Note that our app will never send dupe values to be inserted or
merged.

Thanks.
Sep 18 '08 #22
Henry J. wrote:
Note that our app will never send dupe values to be inserted or
merged.
DB2 cannot know that. The only way for DB2 to know is GROUP BY the PK.
Note that UNION actually (I thought more about it) does NOT work.
UNION assures uniqueness of the row. That is not enough. You need
uniqueness of the key used in teh MERGe ON clause.

Cheers
Serge

PS: I think we have given you all the bread crumbs you need and I am on
my way to a beach. So run experimenets. Look at the optimizer plan, ...

--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab
Sep 18 '08 #23
On Sep 18, 9:02*am, Serge Rielau <srie...@ca.ibm .comwrote:
HenryJ. wrote:
Note that our app will never send dupe values to be inserted or
merged.

DB2 cannot know that. The only way for DB2 to know is GROUP BY the PK.
Note that UNION actually (I thought more about it) does NOT work.
UNION assures uniqueness of the row. That is not enough. You need
uniqueness of the key used in teh MERGe ON clause.

Cheers
Serge

PS: I think we have given you all the bread crumbs you need and I am on
my way to a beach. So run experimenets. Look at the optimizer plan, ...

--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab
Thanks Serge. Really appreciated. I got all I need to know. Have
you written any books? A true expert and kind soul. Wish you great
time on beach.
Sep 18 '08 #24
Henry J. wrote:
Thanks Serge. Really appreciated. I got all I need to know. Have
you written any books? A true expert and kind soul. Wish you great
time on beach.
Hah! It takes a patient soul to write a book. I am many things, but not
that.

Cheers
Serge

--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab
Sep 18 '08 #25

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

Similar topics

3
13231
by: jason | last post by:
How does one loop through the contents of a form complicated by dynamic construction of checkboxes which are assigned a 'model' and 'listingID' to the NAME field on the fly in this syntax: Hunter_69. Here is what the form looks like. I have the difficulty of inserting the multiple items selected by the user the first time he visits and uses the screen and then using an UPDATE when he visits later. Model | Original Price | Reduced Price...
0
2235
by: Michael Schoen | last post by:
Hi there, i=B4m actually developing a performance criticial system where I need to index a huge amount of protocoll data. These data are already in a chronological order, so if I push the data one by one in the database I will never need an ORDER BY syntax, `cause mysql already stores the data the right way. That=B4s tweaking, isn=B4t it ;)
2
1957
by: Sashi | last post by:
Hi, I'm trying to insert multiple rows with the following statment and am getting a syntax error on line 3. The datatypes are varchars except for status which is numeric. insert S (S#, SNAME, STATUS, CITY) values ('S2', 'Jones', 10, 'Paris'), ('S3', 'Blake', 30, 'Paris'), ('S4', 'Clark', 20, 'London'), ('S5', 'Adams', 30, 'Athens');
2
1543
by: Asha | last post by:
greetings, i'm doing a multiple insert into the db through a store proc. i'm very sure the sp has nothing wrong. below are my implementation code and error msg. thanks in advance for the help objCmd.Connection = objConn objCmd.CommandType = CommandType.StoredProcedure objCmd.CommandText = "pr_AUDIT_UPLOAD_UpdateStkCntAud" With objds.Tables(0) For intx = 0 To .Rows.Count - 1
2
1461
by: tpafr | last post by:
Hi, I would like to insert multiple rows in an Access DB from a datagrid (or a txt file) It is very slow for large number of insertions(5000 -> >30 seconds !). Is there another way to do this myOleDbConnection.Open(); string sqlst="INSERT INTO TUsers (IDUser,Pwd,...) VALUES (@IDUser, @Pwd, ...)"; OleDbCommand myCommand = new OleDbCommand(sqlst, myConn);
1
1191
by: Bauer | last post by:
I have already implemented merge of 2 datasets.Would like to clarify if this merge does not break. I have 2 datasets each having tables. ds1 has dt1 and ds2 has dt2. dt1 and dt2 differ in only 1 column. Both the datatables do not have any primary keys.when i performed a merge on both the datasets everything works fine. I get a single table dt1 with the extra column from dt2. Is that a correct way of doing it?
1
2674
by: ckirby | last post by:
I've inherited an application for creating mail merges from an Access 2003 ADP that needs an overhaul. To generate the mail merge , the app uses a shell command: result = Shell(str, vbHide) where str = "C:\Program Files\Microsoft Office\Office\Winword.exe " & letter & " /mmergeletter"
1
4834
by: clayalphonso | last post by:
Here is the code: <% dim testArray, testArray2 dim Conn, rs dim sSQL, sConnString 'response.write request.form("sel1") 'testArray = split(request.form("sel1"),",") 'for each gidstuff In testArray 'Response.Write gidstuff & "<br />"
0
9933
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9786
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
11125
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...
1
10836
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,...
1
7962
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
7114
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5794
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...
1
4607
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3230
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.