Solved

compare 2 large tables

Posted on 2013-05-21
7
359 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
7 Comments
 
LVL 10

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 48

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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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 12

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 47

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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
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…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

856 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