用于删除重复行的SQL

时间:2015-03-08 18:53:19

标签: postgresql-9.3

我编写了一个sql语句,只保留一个实例(最小id),其中有重复的product_codes。问题是该声明效率非常低,并且运行时间非常长,所以我希望有一种更有效的方法来编写它

数据集的结构如下:

id  product_code  cat_desc      product_desc  
1   2352345       423           COCA COLA   
2   8967896       457           FANTA   
3   6456466       435           SPARKLING WATER 
4   3562314       457           STILL WATER 

陈述是:

DELETE
FROM raw_products_inter
WHERE id IN (SELECT id
             FROM raw_products_inter outer_table
             WHERE product_code IN (SELECT product_code
                                    FROM raw_products_inter
                                    GROUP BY 1
                                    HAVING COUNT(id) > 1)
             AND   id NOT IN (SELECT MIN(id)
                              FROM raw_products_inter inner_table
                              WHERE inner_table.product_code = outer_table.product_code))

1 个答案:

答案 0 :(得分:1)

您应该能够使用EXISTS条件提升性能:

DELETE 
  FROM raw_products_inter P
 WHERE EXISTS (
          SELECT *
            FROM raw_products_inter OP
           WHERE OP.product_code = P.product_code
             AND OP.id < P.id
       )