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