Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

Why INSER INTO ... UNION ALL? Expand / Collapse
Author
Message
Posted Tuesday, March 10, 2009 4:43 PM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Sunday, November 4, 2012 12:23 PM
Points: 2,087, Visits: 3,932
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
Post #672861
Posted Tuesday, March 10, 2009 5:24 PM


SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Tuesday, July 22, 2014 4:12 PM
Points: 958, Visits: 1,031
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.
Post #672877
Posted Tuesday, March 10, 2009 6:04 PM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Sunday, November 4, 2012 12:23 PM
Points: 2,087, Visits: 3,932
Hello Bruce

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

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
Post #672892
Posted Tuesday, March 10, 2009 6:07 PM


SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: Today @ 9:36 AM
Points: 4,011, Visits: 6,084
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? -- Stephen Stills
Post #672895
Posted Tuesday, March 10, 2009 6:09 PM


SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: Today @ 9:36 AM
Points: 4,011, Visits: 6,084
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? -- Stephen Stills
Post #672896
Posted Tuesday, March 10, 2009 6:15 PM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Sunday, November 4, 2012 12:23 PM
Points: 2,087, Visits: 3,932
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
Post #672900
Posted Tuesday, March 10, 2009 6:21 PM


SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Tuesday, July 22, 2014 4:12 PM
Points: 958, Visits: 1,031
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.
Post #672903
Posted Tuesday, March 10, 2009 6:23 PM


SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: Today @ 9:36 AM
Points: 4,011, Visits: 6,084
I'm currently investigating SQL Server 2008...


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

SS2k5 == SQL Server 2005.


__________________________________________________

Against stupidity the gods themselves contend in vain. -- Friedrich Schiller
Stop, children, what's that sound? -- Stephen Stills
Post #672904
Posted Tuesday, March 10, 2009 11:39 PM


SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Thursday, July 24, 2014 4:22 PM
Points: 942, Visits: 1,062
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


---

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.


How to ask for help .. Read Best Practices here.
Post #672993
Posted Wednesday, March 11, 2009 3:16 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Sunday, November 4, 2012 12:23 PM
Points: 2,087, Visits: 3,932
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" :) .

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 :)

SS2k5 == SQL Server 2005.


Thanks! I'm only using it to investigate the differences in usage (like the new syntax ;) ). 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
Post #673068
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse