1

如果第二个语言环境中不存在,我正在尝试将数据从一个语言环境移动到另一个语言环境。我用来执行此操作的语句需要 20 小时才能执行,因此我将不胜感激有关如何更有效地执行此操作的任何想法。

update table 
set localeid = 3 
where localeid = 1 
  and not exists 
          (select id from table as b where localeid = 3 and id = b.id)

我想知道是否有某种方法可以使用约束并让 SQL 跳过违反约束的行,而不是使整个更新失败。在这种情况下,约束将是创建id,localeid主键。

这可以做到吗?

ps 优化此查询所需的索引已经到位,但由于该数据库的庞大规模(包含超过 7000 万个条目),它仍然需要很长时间。

4

3 回答 3

0

这样的事情呢?这可能会做得更好,因为它消除了子查询。

update a
set localeid = 3
from table a
left join table b on b.id = a.id and b.localeid = 3
where a.localeid = 1
and b.id is null
于 2013-04-15T14:42:34.240 回答
0

我通常会这样写:

update a
   set localeid = 3
from
   table a
     left join
   table b
     on
        a.id = b.id and
        b.localeid = 3
where
    a.localeid = 1 and
    b.id is null

但是否会提高性能,我不知道

于 2013-04-15T14:43:27.230 回答
0

按照 Damien 和 Becuzz 的建议,消除子查询可能会有所帮助……如果没有,您可以尝试以下方法。EXISTS 总是比 NOT EXISTS 快……所以从获取你想要更新的 ID 开始,而不是排除你想要的 ID。运行以下直到在临时表上创建索引。即使有 7000 万行,也不会花费太长时间。

create table #IdsToUpdate (Id INT);

insert  #IdsToUpdate (Id)
select  id 
from    table 
group by id
having  max(case when localeid = 3 then 1 else 0 end) = 0;

create index in1 on #IdsToUpdate(Id);

然后根据临时表尝试实际更新:

update  t
set     t.localeid = 3
from    table t
where   exists (
            select  top 1 1
            from    #IdsToUpdate i
            where   i.Id = t.id);

另外,如果可能的话...您可以将数据库置于简单恢复模式吗?记录更新需要更长的时间。

于 2013-04-15T14:55:39.643 回答