在MySQL的几列了重复的行(Getting duplicate rows by several c

2019-10-29 05:33发布

我想通过一些列的大表(近18个000行)来搜索重复的行。 问题是,查询需要大量的时间,我尝试这样做:

 SELECT * FROM table_name a, table_name b 
 WHERE a.col1 = b.col1 
 AND a.col2 = b.col2 
 AND a.col3 = b.col3
 AND a.col4 = b.col4 
 AND a.id <> b.id

和这个:

SELECT *
FROM table_name 
WHERE col1 IN (
    SELECT col1 
    FROM table_name 
    GROUP BY col1 
    HAVING count(col1) > 1
    )
AND col2 IN (
    SELECT col2
    FROM table_name 
    GROUP BY col2
    HAVING count(col2) > 1
    )
AND col3 IN (
    SELECT col3
    FROM table_name 
    GROUP BY col3
    HAVING count(col3) > 1
    )
AND col4 IN (
    SELECT col4
    FROM table_name 
    GROUP BY col4
    HAVING count(col4) > 1
    )

他们都工作,但速度太慢。 有任何想法吗?

Answer 1:

您可以尝试使用一个联合GROUP BY像语句:

SELECT * FROM table_name
    GROUP BY col1, col2, col3, col4
    HAVING count(*) > 1

最起码,它看起来更清洁。

编辑

要返回所有结果为前一列一个子集:

SELECT *
FROM table_name 
WHERE col4 IN (
  SELECT col4
  FROM table_name 
  WHERE col3 IN (
    SELECT col3
    FROM table_name 
    WHERE col2 IN (
      SELECT col2
      FROM table_name
      WHERE col1 IN (
        SELECT col1
        FROM table_name
        GROUP BY col1
        HAVING count(col1) > 1
        )
      )
    )

这在概念上,应该给你一个更快的执行时间,所有结果。



文章来源: Getting duplicate rows by several columns in MySQL