如何优化此重复删除查询?

时间:2012-05-10 22:42:50

标签: sql sql-server sql-server-2000

我正在使用SQL 2000.我已经达到了一个点,我可以根据一组复杂的标准删除所有不需要的重复项,但现在查询需要几个小时才完成,因为它过去只用了大约3.5分钟获取包含重复项的数据。

清晰度: 只要rpt.HostName或rpt.SystemSerialNumber字段也不同,我就可以有一个重复的rpt.Name字段。此外,我必须根据四个不同列的时间戳确定要保留哪个条目,因为其中一些列缺少时间戳。

非常感谢任何帮助!

SELECT 
rpt.[Name],
rpt.LastAgentExecution,
rpt.GroupName,
rpt.PackageName,
rpt.PackageVersion,
rpt.ProcedureName,
rpt.HostName,
rpt.SystemSerialNumber,
rpt.JobCreationTime,
rpt.JobActivationTime,
rpt.[Job Completion Time]
FROM DSM_StandardGroupMembersProcedureActivityViewExt rpt
WHERE
(
  (
      rpt.GroupName = 'Adobe Acrobat 7 Deploy'
   OR rpt.GroupName = 'Adobe Acrobat 8 Deploy'
  )
  AND
  (
      (rpt.PackageName = 'Adobe Acrobat 7' AND rpt.PackageVersion = '-1.0')
   OR (rpt.PackageName = 'Adobe Acrobat 8' AND rpt.PackageVersion = '-3.0')
  )
)
AND NOT EXISTS
(
  SELECT *
  FROM   DSM_StandardGroupMembersProcedureActivityViewExt rpt_dupe
  WHERE
  (
    (
     rpt.GroupName = 'Adobe Acrobat 7 Deploy'
      OR rpt.GroupName = 'Adobe Acrobat 8 Deploy'
    )
    AND
    (
     (rpt.PackageName = 'Adobe Acrobat 7' AND rpt.PackageVersion = '-1.0')
      OR (rpt.PackageName = 'Adobe Acrobat 8' AND rpt.PackageVersion = '-3.0')
    )
    AND
    (
      (rpt_dupe.[Name] = rpt.[Name])
      AND
      (
       (rpt_dupe.SystemSerialNumber = rpt.SystemSerialNumber)
    OR (rpt_dupe.HostName = rpt.HostName)
      )
      AND
      (
       (rpt_dupe.LastAgentExecution    < rpt.LastAgentExecution)
    OR (rpt_dupe.JobActivationTime     < rpt.JobActivationTime)
    OR (rpt_dupe.JobCreationTime       < rpt.JobCreationTime)
    OR (rpt_dupe.[Job Completion Time] < rpt.[Job Completion Time])
      )
    )
  )
)

2 个答案:

答案 0 :(得分:0)

尝试这些方法:

SELECT t_main.columns
FROM table as t_main
LEFT JOIN 
(
SELECT name, MAX(lastAgentExecution)..... FROM table GROUP BY name,serialnumber, hostname
)
as t_joinSerial
ON t_main.name=t_joinSerial.name,lastAgentExecution etc.
where (t_main.AdobeStuff and t_joinSerial is NULL)

答案 1 :(得分:0)

原因是not exists子句。

有人建议将其重写为左外连接:

 from <big query> left outer join
      <dups query>
      on <all the fields that constitute a match>
 where <dups query>.<some field> is null

我发现不存在而且通常不能很好地优化。

另一个建议是将此查询更改为更直接的实现:

with t as (
    SELECT rpt.[Name], rpt.LastAgentExecution, rpt.GroupName, rpt.PackageName,
           rpt.PackageVersion, rpt.ProcedureName, rpt.HostName, rpt.SystemSerialNumber, 
            rpt.JobCreationTime, rpt.JobActivationTime, rpt.[Job Completion Time]
    FROM DSM_StandardGroupMembersProcedureActivityViewExt rpt
    WHERE rpt.GroupName in ('Adobe Acrobat 7 Deploy', 'Adobe Acrobat 8 Deploy') AND
          ((rpt.PackageName = 'Adobe Acrobat 7' AND rpt.PackageVersion = '-1.0') OR
           (rpt.PackageName = 'Adobe Acrobat 8' AND rpt.PackageVersion = '-3.0')
          )
 )
 select t.*
 from t join
      (select name, ..., max(id)
       from t
       group by name, ...
      ) tsum
      on t.id = tsum.id

也就是说,通过您想要的列来汇总表格。选择其中一行。在这里,我假设有一个“id”字段来唯一标识每一行。您可能必须使用字段组合,例如名称和日期。没有身份证,这更具挑战性。在更新版本的SQL Server中,您可以使用row_number()。