0

任何猜测为什么在处理 300 000 行时此语句花费的时间太长。基本上,此查询用于查找重复项。

SELECT DISTINCT 
    a.Id,
    b.Id as sid
FROM 
    csv_temp a
INNER JOIN 
    csv_temp b ON a.firstname = b.firstname AND 
    a.lastname = b.lastname  AND 
   ((a.address = b.address) OR 
    (a.zip = b.zip) OR 
    (a.city = b.city AND a.state = b.state) )
WHERE 
    a.Id <> b.Id AND 
    a.status=2 AND 
    b.status=1 AND 
    a.flag !=1 AND 
    b.flag !=1

在此处输入图像描述

4

2 回答 2

3

OR 的性能似乎通常很差,在 JOIN 条件下,我预计情况会更糟。尝试使用 3 个 SELECT(每个 ORed 条件一个)并将结果合并在一起。如果这样做,则怀疑不需要 DISTINCTS:-

SELECT  
    a.Id,
    b.Id as sid
FROM 
    csv_temp a
INNER JOIN 
    csv_temp b ON a.firstname = b.firstname AND 
    a.lastname = b.lastname  AND 
   a.address = b.address
WHERE 
    a.Id <> b.Id AND 
    a.status=2 AND 
    b.status=1 AND 
    a.flag !=1 AND 
    b.flag !=1
UNION
SELECT  
    a.Id,
    b.Id as sid
FROM 
    csv_temp a
INNER JOIN 
    csv_temp b ON a.firstname = b.firstname AND 
    a.lastname = b.lastname  AND 
    a.zip = b.zip
WHERE 
    a.Id <> b.Id AND 
    a.status=2 AND 
    b.status=1 AND 
    a.flag !=1 AND 
    b.flag !=1
UNION
SELECT  
    a.Id,
    b.Id as sid
FROM 
    csv_temp a
INNER JOIN 
    csv_temp b ON a.firstname = b.firstname AND 
    a.lastname = b.lastname  AND 
    a.city = b.city AND a.state = b.state
WHERE 
    a.Id <> b.Id AND 
    a.status=2 AND 
    b.status=1 AND 
    a.flag !=1 AND 
    b.flag !=1
于 2013-02-14T09:31:39.657 回答
0

现在在比较中使用的列上添加索引后检查使用解释计划

于 2013-02-14T09:29:01.800 回答