Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

SQL Server 2008 r2 join on half a billion records

Posted on 2013-12-19
3
Medium Priority
?
440 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

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…
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 extract information from SQL Server on Database, Connection and Server properties
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

662 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