Reputation: 1192
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
Upvotes: 2
Views: 1879
Reputation: 368
Upvotes: 0
Reputation: 29619
Not sure if this will be faster, but worth a try:
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
where b.recordID not in
(select recordID from A)
Upvotes: 3
Reputation: 8920
You can check the the Merge command:
http://technet.microsoft.com/en-us/library/bb510625.aspx
Upvotes: 3