views:

1920

answers:

5

I have two tables with the same columns, and I need to copy one table's rows to the other table's rows to create one big table with all the values from both tables. Right now I am doing this query to return the same thing:

SELECT col1, col2, col3 from Table1
union
SELECT col1, col2, col3 from Table2

However, it seems horribly inefficient, and on my system is very slow (returns 1210189 records).

+3  A: 

May it work to just do:

SELECT col1, col2, col3 INTO Table1 FROM Table2

Tommy
+4  A: 

Start with union all:

select col1, col2, col3 from Table1
union all
select col1, col2, col3 from Table2

Your query is trying to deduplicate things, which would slow it down considerably.

Dustin
I'm pretty certain deduplicate is a made-up word :-)
paxdiablo
Every word has to be made up at some point. :)
Dustin
We use the word 'dedupe' (short for deduplicate) at work all the time :)
Joe Philllips
Data depuplication is a standard phrase in data warehousing and storage/backup situations.
K. Brian Kelley
A: 

I think the best option is to create a view in sql server, this will optimize the performance of the query:

SELECT col1, col2, col3 from Table1
union all
SELECT col1, col2, col3 from Table2

(As other users said: "union" is used to select distinct values from two tables where as "union all" is used to select all values including duplicates from the tables.)

At the same time I would restrict the number of rows I get from the database if i am writing them for a web and if this is giving me problems, with the new functions of Sql Server 2005 row_number(), with this I would page results.

netadictos
+1  A: 

You could use this to fill the second table:

Insert into table2 select * from table1;

Or if you want to be more specific:

Insert into table2(col1, col2, col3) select col1, col2, col3 from table1;

(Note: some DBMSs might require putting parenthesis around the SELECT clause.)

Hosam Aly
A: 

This really helped me alot.

Thaks you somuch.

Srikanth Vemulapally