SQL Server 2008 r2 join on half a billion records

Posted on 2013-12-19
Medium Priority
Last Modified: 2013-12-19

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      
              Table A.matchcolumn
            Table A, Table B
               Table A.matchcolumn=Table B.matchcolumn )
Question by:Knowknot
LVL 57

Accepted Solution

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..

into Table3                        
FROM  Table A
JOIN Table B ON Table A.matchcolumn=Table B.matchcolumn
LVL 61

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!

Author Closing Comment

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

Featured Post

A proven path to a career in data science

At Springboard, we know how to get you a job in data science. With Springboard’s Data Science Career Track, you’ll master data science  with a curriculum built by industry experts. You’ll work on real projects, and get 1-on-1 mentorship from a data scientist.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
This shares a stored procedure to retrieve permissions for a given user on the current database or across all databases on a server.
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
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

597 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