Solved

compare 2 large tables

Posted on 2013-05-21
7
363 Views
Last Modified: 2013-05-24
i have 2 tables, lets say:

table1(id bigint) and table2(id bigint)


table1          table2
1                    3
2                    76
23                  89
45                  90
76                  92
90                  98



so i want to select all rows on table1 that does not exist on table2, i have this in place:

 SELECT * FROM TABLE1 AS T1
          WHERE (CASE WHEN EXISTS(SELECT TOP 1 ID FROM TABLE2 WHERE ID = T1.ID) THEN 1 ELSE 0 END) = 0

this query takes a lot from the processor and too hard on my server, both of those tables have about 100000 rows. So i need another better solution to this.
0
Comment
Question by:rafaelrgl
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 11

Expert Comment

by:HuaMinChen
ID: 39186590
1. Make sure you have indices on ID for both tables
2. Try
SELECT * FROM TABLE1 AS T1
          WHERE not EXISTS(SELECT TOP 1 ID FROM TABLE2 t2 WHERE t2.ID = T1.ID);
SELECT * FROM TABLE2 AS t2
          WHERE not EXISTS(SELECT TOP 1 ID FROM TABLE1 t1 WHERE t1.ID = t2.ID);
0
 
LVL 9

Accepted Solution

by:
mimran18 earned 500 total points
ID: 39186614
CREATE PROCEDURE CompareTables(@table1 varchar(100),
 @table2 Varchar(100), @T1ColumnList varchar(1000),
 @T2ColumnList varchar(1000) = '')
AS
 

declare @SQL varchar(8000);
 
IF @t2ColumnList = '' SET @T2ColumnList = @T1ColumnList
 
set @SQL = 'SELECT ''' + @table1 + ''' AS TableName, ' + @t1ColumnList +
 ' FROM ' + @Table1 + ' UNION ALL SELECT ''' + @table2 + ''' As TableName, ' +
 @t2ColumnList + ' FROM ' + @Table2
 
set @SQL = 'SELECT Max(TableName) as TableName, ' + @t1ColumnList +
 ' FROM (' + @SQL + ') A GROUP BY ' + @t1ColumnList +
 ' HAVING COUNT(*) = 1'
 
exec ( @SQL)

http://weblogs.sqlteam.com/jeffs/archive/2004/11/10/2737.aspx
0
 
LVL 49

Expert Comment

by:PortletPaul
ID: 39186646
select
*
from table1
inner join (
            select ID
            from table1

            except

            select ID
            from table2
           ) cept on table1.id = cept.id

Open in new window

0
Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

 
LVL 10

Expert Comment

by:Asim Nazir
ID: 39186788
If you want to compare data of two tables then you may use Visual Studio's Data/Schema comparison options or even SqlDelta.
0
 
LVL 13

Expert Comment

by:jitendra patil
ID: 39186965
as per your given example above,

i've tried to create a small example of except command which can get you near to the results.

DECLARE @Temp1 table (col1 int)
insert into @Temp1(col1)values(1)
insert into @Temp1(col1)values(2)
insert into @Temp1(col1)values(3)
insert into @Temp1(col1)values(4)
insert into @Temp1(col1)values(5)
insert into @Temp1(col1)values(6)

DECLARE @Temp2 table (col1 int)
insert into @Temp2(col1)values(1)
insert into @Temp2(col1)values(4)
insert into @Temp2(col1)values(6)
insert into @Temp2(col1)values(7)
insert into @Temp2(col1)values(4)
insert into @Temp2(col1)values(8)

select * from @Temp1
except
select * from @Temp2


please give  a try & hope this helps.
0
 
LVL 48

Expert Comment

by:Dale Fye (Access MVP)
ID: 39187166
SELECT T1.*
FROM Table1 as T1
LEFT JOIN Table2 as T2
ON T1.ID = T2.ID
WHERE T2.ID IS NULL
0
 
LVL 1

Author Closing Comment

by:rafaelrgl
ID: 39190683
this way is really faster compare to the one i was using.
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question