SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Why INSER INTO ... UNION ALL?


Why INSER INTO ... UNION ALL?

Author
Message
Florian Reischl
Florian Reischl
SSCrazy Eights
SSCrazy Eights (8.9K reputation)SSCrazy Eights (8.9K reputation)SSCrazy Eights (8.9K reputation)SSCrazy Eights (8.9K reputation)SSCrazy Eights (8.9K reputation)SSCrazy Eights (8.9K reputation)SSCrazy Eights (8.9K reputation)SSCrazy Eights (8.9K reputation)

Group: General Forum Members
Points: 8875 Visits: 3934
Hi everybody

Why using so many people the UNION ALL syntax to insert multiple values into a table instead of simple INSERT statements?

I just tried with 6000 values:


SET NOCOUNT ON
CREATE TABLE #t (id INT, txt VARCHAR(100))

INSERT INTO #t VALUES (1, 'hello')
INSERT INTO #t VALUES (2, 'hello')
-- and so on...
INSERT INTO #t VALUES (6000, 'hello')



versus:


SET NOCOUNT ON
CREATE TABLE #t (id INT, txt VARCHAR(100))

INSERT INTO #t
SELECT 1, 'hello'
UNION ALL SELECT 2, 'hello'
-- and so on...
UNION ALL SELECT 6000, 'hello'



Inserting 6000 values with simple INSERT statements take less than 2 seconds. Inserting 6000 values with UNION ALL syntax took 6 seconds.

I know Microsoft prefers this syntax but why do you? Because the statement will be done in all or nothing (either everything works or nothing will be inserted)?

Thanking you in anticipation!
Flo


The more I learn, the more I know what I do not know
Blog: Things about Software Architecture, .NET development and T-SQL

How to Post Data/Code to get the best Help How to Post Performance Problems
Bruce W Cassidy
Bruce W Cassidy
Hall of Fame
Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)

Group: General Forum Members
Points: 3109 Visits: 1033
I suspect the answer is: "because".

I do know they've got some improved syntax for doing multiple row inserts in SQL Server 2008, so there the "issue" goes away.

I also know if you use the UNION ALL syntax, you can embed the same SELECT statement as part of a CTE or a derived table/in-line view. You can't do that with a whole bunch of INSERT statements.

And I also know if you do the insert with UNION ALLs, it will generate one transaction rather than multiple transactions. Sometimes that's what you want.

But for how you are using it, a whole bunch of inserts is fine.

Florian Reischl
Florian Reischl
SSCrazy Eights
SSCrazy Eights (8.9K reputation)SSCrazy Eights (8.9K reputation)SSCrazy Eights (8.9K reputation)SSCrazy Eights (8.9K reputation)SSCrazy Eights (8.9K reputation)SSCrazy Eights (8.9K reputation)SSCrazy Eights (8.9K reputation)SSCrazy Eights (8.9K reputation)

Group: General Forum Members
Points: 8875 Visits: 3934
Hello Bruce

Thank you for your answer! In my opinion it "because" a absolutely valid reason Smile.

I just found a third possibility in MSDN forums http://social.msdn.microsoft.com/Forums/en-US/transactsql/thread/8d2888ab-9ad6-47f2-a01c-0a487b1dc995/ which I never saw. Example:


DECLARE @t TABLE (id INT, txt VARCHAR(100))

INSERT INTO @t VALUES
(1, 'hello'),
(2, 'world')

SELECT * FROM @t



I do not have a specific business case at the moment I just would like to understand why other DBAs/developer do how they do and if there may be any advantage for my work.

Greets
Flo


The more I learn, the more I know what I do not know
Blog: Things about Software Architecture, .NET development and T-SQL

How to Post Data/Code to get the best Help How to Post Performance Problems
The Dixie Flatline
The Dixie Flatline
SSChampion
SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)

Group: General Forum Members
Points: 12976 Visits: 6903
Flo,

I'm afraid I'm unable to duplicate your results. I'm comparing an insert into/SELECT ... union all against a insert into VALUES for 1000 rows. The UNION ALL technique is running consistently faster on my machine, which I would expect because it's a single INSERT, as Bruce already stated.

Whenever possible you want to handle entire sets of data, instead of individual rows. When you look at the execution plan of the SELECT/UNION ALL approach, it amounts to single insert fed by the scan of an internal table of constants.

Bob

P.S. I also find it much easier to type up quick examples like are used in this forum using the SELECT/UNION ALL format.

__________________________________________________

Against stupidity the gods themselves contend in vain. -- Friedrich Schiller
Stop, children, what's that sound? Everybody look what's going down. -- Stephen Stills
The Dixie Flatline
The Dixie Flatline
SSChampion
SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)

Group: General Forum Members
Points: 12976 Visits: 6903
POST PostScript:

Row Constructors [ex: Values (1,'hello'), (2,'hello')] are new to 2008.

Be mindful that you are in a 2005 forum.

__________________________________________________

Against stupidity the gods themselves contend in vain. -- Friedrich Schiller
Stop, children, what's that sound? Everybody look what's going down. -- Stephen Stills
Florian Reischl
Florian Reischl
SSCrazy Eights
SSCrazy Eights (8.9K reputation)SSCrazy Eights (8.9K reputation)SSCrazy Eights (8.9K reputation)SSCrazy Eights (8.9K reputation)SSCrazy Eights (8.9K reputation)SSCrazy Eights (8.9K reputation)SSCrazy Eights (8.9K reputation)SSCrazy Eights (8.9K reputation)

Group: General Forum Members
Points: 8875 Visits: 3934
Hi Bob!

Bob Hovious (3/10/2009)
Flo,

I'm afraid I'm unable to duplicate your results. I'm comparing an insert into/SELECT ... union all against a insert into VALUES for 1000 rows. The UNION ALL technique is running consistently faster on my machine, which I would expect because it's a single INSERT, as Bruce already stated.


Strange...! I tried on SQL Server 2005 in our company and on my SQL Server 2008. The single inserts are always faster than the union syntax..?

Bob Hovious (3/10/2009)
POST PostScript:

Row Constructors [ex: Values ((1,'hello') (2,'hello'))] are new to 2008.


Oups... sorry for that. I'm currently investigating SQL Server 2008...

Thanks for your reply!

Greets
Flo


The more I learn, the more I know what I do not know
Blog: Things about Software Architecture, .NET development and T-SQL

How to Post Data/Code to get the best Help How to Post Performance Problems
Bruce W Cassidy
Bruce W Cassidy
Hall of Fame
Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)

Group: General Forum Members
Points: 3109 Visits: 1033
florian.reischl (3/10/2009)
Strange...! I tried on SQL Server 2005 in our company and on my SQL Server 2008. The single inserts are always faster than the union syntax..?


Probably dependant on the database recovery model.
The Dixie Flatline
The Dixie Flatline
SSChampion
SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)

Group: General Forum Members
Points: 12976 Visits: 6903
I'm currently investigating SQL Server 2008...


No problem. But if you are asking questions about 2008, there is a separate forum for that Smile

SS2k5 == SQL Server 2005.

__________________________________________________

Against stupidity the gods themselves contend in vain. -- Friedrich Schiller
Stop, children, what's that sound? Everybody look what's going down. -- Stephen Stills
Mohit K. Gupta
Mohit K. Gupta
Hall of Fame
Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)

Group: General Forum Members
Points: 3178 Visits: 1089
I have used both formats; I usually don't notice on the servers. But most of my servers are pretty powerful; but I have shown both those syntax to developers.

And most of my developer buddies hate UNION ALL because they find it it makes it hard to read. I liked Union all because I don't have to do INSERT INTO .blah blah again...

Me ((0.02/10)/10) cents o.O there is such thing right Doze

---

Mohit K. Gupta, MCITP: Database Administrator (2005), My Blog, Twitter: @SQLCAN.
Microsoft FTE - SQL Server PFE

* Some time its the search that counts, not the finding...
* I didn't think so, but if I was wrong, I was wrong. I'd rather do something, and make a mistake than be frightened and be doing nothing. Smooooth


How to ask for help .. Read Best Practices here.
Florian Reischl
Florian Reischl
SSCrazy Eights
SSCrazy Eights (8.9K reputation)SSCrazy Eights (8.9K reputation)SSCrazy Eights (8.9K reputation)SSCrazy Eights (8.9K reputation)SSCrazy Eights (8.9K reputation)SSCrazy Eights (8.9K reputation)SSCrazy Eights (8.9K reputation)SSCrazy Eights (8.9K reputation)

Group: General Forum Members
Points: 8875 Visits: 3934
Hi %

Thank you all for your suggestions! It seems to never mind wich syntax is used.

So as Bruce said; the reason for the used syntax is "because" Smile .

Bob Hovious (3/10/2009)
I'm currently investigating SQL Server 2008...


No problem. But if you are asking questions about 2008, there is a separate forum for that Smile

SS2k5 == SQL Server 2005.


Thanks! I'm only using it to investigate the differences in usage (like the new syntax Wink ). Currently we only use SQL Server 2000 and 2005 in development, test and production. So for the next time I stay a SQL Server 2005 user.

Thanks to all!
Flo


The more I learn, the more I know what I do not know
Blog: Things about Software Architecture, .NET development and T-SQL

How to Post Data/Code to get the best Help How to Post Performance Problems
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search