Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 performance gain of multi row insert vs individual

Author  Topic 

adlo
Posting Yak Master

108 Posts

Posted - 2010-09-07 : 04:56:29
Hi,

I have an app that generates sql code

Now I can have it in two ways:

insert into MyTable values (1,2,3)
insert into MyTable values (2,2,3)
insert into MyTable values (3,2,3)
insert into MyTable values (4,2,3)
insert into MyTable values (5,2,3)
GO

OR
INSERT INTO MyTable values
(1,2,3),
(2,2,3),
(3,2,3),
(4,2,3),
(5,2,3)
GO

Is sql server intelligent enough to make the former example just as fast as the latter or is there a performance improvement of rewriting the apps code to the latter example.

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-09-07 : 05:05:27
Both should be same however using the second method, you can only insert 1000 set of values

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-09-07 : 05:06:54
Thought this but seems I am wrong
Your second approach will not work. The syntax is wrong/impossible. INSERT ... VALUES ... is only possible for one record at a time.

But this will work:
INSERT MyTable (colname1, colname2,colname3)
SELECT 1,2,3 UNION ALL
SELECT 2,2,3 UNION ALL
SELECT 3,2,3 UNION ALL
SELECT 4,2,3 UNION ALL
SELECT 5,2,3


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-09-07 : 05:09:22
quote:
Originally posted by webfred

Thought this but seems I am wrong
Your second approach will not work. The syntax is wrong/impossible. INSERT ... VALUES ... is only possible for one record at a time.

But this will work:
INSERT MyTable (colname1, colname2,colname3)
SELECT 1,2,3 UNION ALL
SELECT 2,2,3 UNION ALL
SELECT 3,2,3 UNION ALL
SELECT 4,2,3 UNION ALL
SELECT 5,2,3


No, you're never too old to Yak'n'Roll if you're too young to die.


OP uses version 2008 and posted at 2005 forum

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-09-07 : 05:15:29
Honestly, I also did not know that it works in 2008
We are always learning something new, that's fine.


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

Lumbago
Norsk Yak Master

3271 Posts

Posted - 2010-09-07 : 06:20:44
In theory the first example would involve several transactions while the second one only involves one transaction, hence the second one should be faster. I'm not sure if the difference will be noticeable though...

- Lumbago

My blog (yes, I have a blog now! just not that much content yet)
-> www.thefirstsql.com
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-09-07 : 06:33:11
quote:
Originally posted by webfred

Honestly, I also did not know that it works in 2008
We are always learning something new, that's fine.


No, you're never too old to Yak'n'Roll if you're too young to die.


Read more about VALUES clause in SQL Server 2008
http://beyondrelational.com/blogs/madhivanan/archive/2010/08/02/values-clause-in-sql-server-2008.aspx

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2010-09-07 : 08:59:15
quote:
Originally posted by madhivanan

however using the second method, you can only insert 1000 set of values



For that reason, alone, we avoid it in mechanically generated code
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-09-07 : 09:36:18
quote:
Originally posted by Kristen

quote:
Originally posted by madhivanan

however using the second method, you can only insert 1000 set of values



For that reason, alone, we avoid it in mechanically generated code


Thats good. I am not sure why more than 1000 sets are not allowed

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2010-09-07 : 13:50:12
By the by, is there a limit to the number of

SELECT A, B, C
UNION ALL
SELECT ...

UNION statements that are allowed? Gotta run out of memory / parsing space / something at some point. Maybe 1,000 is a reasonable limit. Come to think of it I doubt we would do 1,000 UNIONs in a mechanically generated statement either - we're break them up into batches so that they ran in a reasonable time interval ... so on that basic we might as well do up-to-1,000 INSERT VALUES statements
Go to Top of Page

Lumbago
Norsk Yak Master

3271 Posts

Posted - 2010-09-08 : 02:40:29
quote:
By the by, is there a limit to the number of...
I have inserted at least a few 100k rows like this without a hitch...so I guess the limitation is within a buffer cache or something...

- Lumbago

My blog (yes, I have a blog now! just not that much content yet)
-> www.thefirstsql.com
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2010-09-08 : 02:45:33
100K repeats of SELECT 1, 'AAA' UNION ALL SELECT 2, 'BBB'?

Interesting thanks. I would imagine it takes SQL Server some time to read & parse it ....

I reflected on this yesterday and thought I'd be better off breaking up the batch into "modest" chunks with a "GO" inbetween ...

Although I'm sure I do

INSERT INTO MyStagingTable
SELECT MyValue
FROM dbo.MySplitFunction('1,2,3,...')

using massive numbers of values - typically that I have got from a Spreadsheet someone has sent me (in that actual example the delimiter will be linebreak, rather than comma) - so I'm not sure that I am walking my talk ...
Go to Top of Page

Lumbago
Norsk Yak Master

3271 Posts

Posted - 2010-09-08 : 03:08:00
It was a few years ago but I remember I was amazed at how fast it was actually. And the situation was that I had a poorly formatted .txt-file that I manually edited using UltraEdit It supports multi-line replace so I found it easier to just create a bunch of selects and then cut/paste in to Management Studio instead of using bcp or sqlcmd

- Lumbago

My blog (yes, I have a blog now! just not that much content yet)
-> www.thefirstsql.com
Go to Top of Page
   

- Advertisement -