2012-03-29 37 views
1

我有一个表与1000万记录与一列上的非聚集索引键,我试图重复表的表。我尝试插入选择使用左连接或不存在;但每次我都会收到违反密钥的错误。以下是我使用的查询;违反主键 - 扣除表

insert into temp(profile,feed,photo,dateadded) 
select distinct profile,feed,photo,dateadded from original as s 
where not exists(select 1 from temp as t where t.profile=s.profile) 

这只是产生违规的关键错误。我尝试使用以下:

insert into temp(profile,feed,photo,dateadded) 
select distinct profile,feed,photo,dateadded from original as s 
left outer join temp t on t.profile=s.profile 
where t.profile is null 

我最终使用批量插入因为日志文件增长过大,但仍然得到主键错误的违规甚至只有1000条记录。

Destination Table :IX_Temp - profileUrl(ASC)--> unique key (non clustered) 
Source Table: IX_PURL - profileUrl(ASC) ---> index (non clustered, not unique 
+0

迁移到http://dba.stackexchange.com/? – 2012-03-29 13:41:15

回答

1

我想象distinct如你期望在这里的时间部分会略有不同的是行不通的。

不同的方法是使用group by并采取最早的dateadded删除任何重复。

也许是这样的:

Select Profile, 
     Feed, 
     Photo, 
     Min(DateAdded) as [DateAdded] 

From Original 
Group By Profile, Feed, Photo 
+0

我想这可能只是工作,因为它现在正在运行,并没有停止与错误味精尚未。完成后会回发。谢谢 – vbNewbie 2012-03-29 14:07:20