在SQL中插入唯一 [英] Insert unique in SQL

查看:51
本文介绍了在SQL中插入唯一的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,



我有SQL服务器数据库表有超过3000万条记录,在这个表中插入唯一值的最佳方法是什么?



任何建议。



我尝试过:



我试过:



如果不存在(从MyTable中选择*,其中Name = @Name and ...)

开始

插入MyTable(名称,...)值(@Name,...)

结束



但这需要花费大量时间来执行



我为每个列创建了一个索引,我检查它是否存在

Hello,

I have SQL server Database table with more than 30 million records, what is the best way to insert unique values to this table

Any suggestions.

What I have tried:

I tried:

if not exists (select * from MyTable where Name = @Name and ... )
begin
insert into MyTable (Name,...) values (@Name,...)
end

But that is taking lots of time to execute

I created an index to every column im checking if its exist

推荐答案

我认为您需要将一个表数据插入另一个表。

I think You need to insert one table data to another table.
INSERT INTO destTable
SELECT Field1,Field2,Field3,... 
FROM srcTable
WHERE NOT EXISTS(SELECT * 
                 FROM destTable 
                 WHERE (srcTable.Field1=destTable.Field1 and
                       SrcTable.Field2=DestTable.Field2...etc.)
                 )













insert into test.t2(name2) 
select distinct name1 from test.t1 t1 where NOT EXISTS(select name2 from test.t2 t2 where t1.name1=t2.name2);


这篇关于在SQL中插入唯一的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆