Skip to content Skip to sidebar Skip to footer

Better Way To Check If The Data Already Exists And Insert

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

Solution 1:

Solution 2:

Not sure if this will be faster, but worth a try:

INSERT INTO TABLEA ([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]FROMTABLEB 
where b.recordID not in 
     (select recordID fromA)

Solution 3:

  1. If it is clustered Index, and the newly added RecordId is not incremental, then lot of page spilt is expected to happen. Make sure to set optimal Fill Factor.
  2. Find what operation takes time, then it will be simple to address. If it Searching is taking time or inserting is taking time?

Post a Comment for "Better Way To Check If The Data Already Exists And Insert"