Solved

SQL Server 2008 r2 join on half a billion records

Posted on 2013-12-19
3
416 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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

758 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now