Hi all I have the following SQL Server 2008 script that will check if a row already exists in Table A and if it doesn’t insert the data from Table B.
This was working nicely until Table A started to fill up with a lot of data. We currently have 30 million rows in this table and this will continue to grow to a predicated 70 million rows.
The problem if this is taking far too long and is affecting other processes. Just wondering if there is a better way to check if a row already exists in a table. Just to add as well this is all done using an SSIS.
Script:
INSERT INTO TABLE A ([recordID],Field 1, Field2, Field 3, Field 4, Field 5)
SELECT
[TABLE B].[recordID],[TABLE B].[Field 1], [TABLE B].[Field2],
[TABLE B].[Field 3], [TABLE B].[Field 4], [TABLE B].[Field 5]
FROM TABLE B AS TABLE B
LEFT OUTER JOIN TABLE A AS TABLE A ON [TABLE B].[recordID] = [TABLE A].[recordID]
WHERE [TABLE A].[recordID] IS NULL
You can check the the Merge command:
http://technet.microsoft.com/en-us/library/bb510625.aspx