【问题标题】:Deduplication of imported records in SQL serverSQL Server 中导入记录的重复数据删除
【发布时间】:2012-10-23 00:14:32
【问题描述】:

我有以下 T_SQL 存储过程,它目前占用了在新导入的记录上运行所有进程所需的总时间的 50% 到我们的后端分析套件中。不幸的是,每次都需要导入这些数据,并且随着我们的数据库大小的增长而导致瓶颈。

基本上,我们正在尝试识别记录中的所有重复项并只保留其中一个。

DECLARE @status INT
SET @status = 3


DECLARE @contactid INT
DECLARE @email VARCHAR (100)


--Contacts
DECLARE email_cursor CURSOR FOR 
SELECT email FROM contacts WHERE  (reference  = @reference AND status = 1 ) GROUP BY email HAVING (COUNT(email) > 1)
OPEN email_cursor

FETCH NEXT FROM email_cursor INTO @email


WHILE @@FETCH_STATUS = 0
    BEGIN
        PRINT @email
        UPDATE contacts SET duplicate  = 1, status = @status  WHERE email = @email and reference = @reference  AND status = 1
        SELECT TOP 1 @contactid = id FROM contacts where  reference = @reference and email = @email AND duplicate = 1
        UPDATE contacts SET duplicate  =0, status = 1 WHERE id = @contactid
        FETCH NEXT FROM email_cursor INTO @email
    END


CLOSE email_cursor
DEALLOCATE email_cursor

我已经添加了我可以从查询执行计划中看到的所有索引,但可能会更新整个 SP 以不同的方式运行,就像我设法对其他人所做的那样。

【问题讨论】:

    标签: sql stored-procedures sql-server-2012 deduplication


    【解决方案1】:

    使用此单一查询进行重复数据删除。

    ;with tmp as (
    select *
          ,rn=row_number() over (partition by email, reference order by id)
          ,c=count(1) over (partition by email, reference)
      from contacts
     where status = 1
    )
    update tmp
       set duplicate = case when rn=1 then 0 else 1 end
          ,status = case when rn=1 then 1 else 3 end
     where c > 1
    ;
    

    它只会在记录 where status=1 中去重复,并将具有相同(电子邮件,参考)组合的行视为重复。

    【讨论】:

    • 上面的小问题(我认为不需要 FROM 声明)。添加了这个,它工作得很好。所有单元测试都变为绿色,所用时间减少了 90%。
    猜你喜欢
    • 1970-01-01
    • 2011-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-16
    • 2021-02-13
    相关资源
    最近更新 更多