Solved

SQL Server 2008 r2 join on half a billion records

Posted on 2013-12-19
3
425 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 300 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 59

Assisted Solution

by:Kevin Cross
Kevin Cross earned 200 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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Suggested Solutions

When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…

829 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