Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

SQL Server 2008 r2 join on half a billion records

Posted on 2013-12-19
3
Medium Priority
?
443 Views
Last Modified: 2013-12-19
Hello,

I need to insert all of the fields from a SQL Server 2008 table (A)  where my match field is contained in another table. (B)

I already know the match results will equal 500K records because that is how many table  (B) contains and I've consistently matched it to other tables.

I have used a join, where in, where exists, and every other option I can think of with code similar to what I am posting below.

My results keep coming back as 1,200 records and I have no idea why.

Please help me meet a very pressing deadline.  All input welcome and appreciated.

Also, the query runs VERY slow, so anything we can put in the code to speed it up will be bonus.

Thanks for helping!


      SELECT   * into Table3                        
      FROM  Table A
       WHERE EXISTS      
      (Select
              Table A.matchcolumn
      FROM
            Table A, Table B
        WHERE
               Table A.matchcolumn=Table B.matchcolumn )
0
Comment
Question by:Knowknot
3 Comments
 
LVL 57

Accepted Solution

by:
Raja Jegan R earned 1200 total points
ID: 39728889
Try this one which is straight forward to insert only matching records in both your tables..

SELECT   *
into Table3                        
FROM  Table A
JOIN Table B ON Table A.matchcolumn=Table B.matchcolumn
0
 
LVL 60

Assisted Solution

by:Kevin Cross
Kevin Cross earned 800 total points
ID: 39728997
One correction to Raja's code above: SELECT A.* ...
Otherwise, you will INSERT every column from both table A and table B.  A better way would be to specify the columns you want from table A explicitly, but that is a different topic. *smile*

If you continue to have a mismatch of the records, check whether or not the table B has duplicates.  In other words, is it possible that there are only 1,200 rows in table A that fit the criteria but table B contains multiple matches per table A row?  Further, make sure you have the correct matching column and that your COLLATION is the same for both.  For example, you can have 'Apple' and 'apple' not match if you have a case sensitive collation.

Just some other thoughts.

Good luck!
0
 
LVL 1

Author Closing Comment

by:Knowknot
ID: 39730817
Thanks for the rapid response!  Works like a charm!
0

Featured Post

[Webinar] Cloud Security

In this webinar you will learn:

-Why existing firewall and DMZ architectures are not suited for securing cloud applications
-How to make your enterprise “Cloud Ready”, and fix your aging DMZ architecture
-How to transform your enterprise and become a Cloud Enabler

Question has a verified solution.

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

In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Windocks is an independent port of Docker's open source to Windows.   This article introduces the use of SQL Server in containers, with integrated support of SQL Server database cloning.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

972 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