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 2012 Forums
 Transact-SQL (2012)
 Multiple inserts in one table. Union or not?

Author  Topic 

Luuk123
Yak Posting Veteran

52 Posts

Posted - 2014-02-21 : 04:01:39
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
Master Smack Fu Yak Hacker

3873 Posts

Posted - 2014-02-21 : 15:17:21
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
   

- Advertisement -