简体   繁体   English

检查数据是否已经存在并插入的更好方法

[英]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 . 大家好,我有以下SQL Server 2008脚本,它将检查Table A是否已存在一行,以及是否未从Table B插入数据。

This was working nicely until Table A started to fill up with a lot of data. Table A开始填充大量数据之前,这一直很好。 We currently have 30 million rows in this table and this will continue to grow to a predicated 70 million rows. 我们目前在此表中有3000万行,并且这一数字将继续增长到预定的7000万行。

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. 只需添加一下,所有这些操作都使用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 http://technet.microsoft.com/en-us/library/bb510625.aspx

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)
  1. If it is clustered Index, and the newly added RecordId is not incremental, then lot of page spilt is expected to happen. 如果它是聚簇索引,并且新添加的RecordId不是增量的,则可能发生大量页面溢出。 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? 如果搜索花费时间或插入花费时间?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM