SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2012 Forums
 Transact-SQL (2012)
 Multiple inserts in one table. Union or not?
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Luuk123
Starting Member

45 Posts

Posted - 02/21/2014 :  04:01:39  Show Profile  Reply with Quote
Hi all,

I have a query which inserts data from different sources into one table. Now it's like this:

Insert into tab
select *
from source_tab_a

Insert into tab
select *
from source_tab_b

Insert into tab
select *
from source_tab_c

I'm wondering which is better, the above separate insert query's or the following:

insert into tab
select * from source_tab_a
union all
select * from source_tab_b
union all
select * from source_tab_c

Can somebody explain?

Thanks!

James K
Flowing Fount of Yak Knowledge

3323 Posts

Posted - 02/21/2014 :  15:17:21  Show Profile  Reply with Quote
If it is a small number of rows that gets inserted it wouldn't matter which method you used. If you have a large number of rows, I would use the first approach. The rationale being that each of the three transactions will be smaller than the one combined transaction.

In general it is better to have smaller transactions than one bigger transactions. Of course, I shouldn't generalize - I can already think of one counter example where that is not true.

By the way, and I know you are just showing an example, avoid using select * as much as possible. That can get you (or others) into troubles in the future.
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000