Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The most efficient way to delete all duplicate rows from table?

I have a table:

| foo | bar |
+-----+-----+
| a   | abc |
| b   | def |
| c   | ghi |
| d   | jkl |
| a   | mno |
| e   | pqr |
| c   | stu |
| f   | vwx |

I want to delete all rows containing duplicates by foo column so that the table should look like this:

| foo | bar |
+-----+-----+
| b   | def |
| d   | jkl |
| e   | pqr |
| f   | vwx |

What is the most efficient way to do this?

like image 773
Andrew Shulgin Avatar asked Apr 07 '13 17:04

Andrew Shulgin


People also ask

How do I remove all duplicates from a table?

Remove duplicate values Select the range of cells that has duplicate values you want to remove. Tip: Remove any outlines or subtotals from your data before trying to remove duplicates. Click Data > Remove Duplicates, and then Under Columns, check or uncheck the columns where you want to remove the duplicates.

What is the fastest method of deleting all the data from table?

TRUNCATE is faster than DELETE , as it doesn't scan every record before removing it. TRUNCATE TABLE locks the whole table to remove data from a table; thus, this command also uses less transaction space than DELETE . Unlike DELETE , TRUNCATE does not return the number of rows deleted from the table.

Which tool will completely remove the duplicate record?

To remove duplicate values, click Data > Data Tools > Remove Duplicates. To highlight unique or duplicate values, use the Conditional Formatting command in the Style group on the Home tab.


1 Answers

You can join a table from a subquery which returns only unique foo using LEFT JOIN. The rows that did not have a match on the subquery will be deleted as you desired, example

DELETE  a
FROM    TableName a
        LEFT JOIN
        (
            SELECT  foo
            FROM    TableName
            GROUP   BY Foo
            HAVING  COUNT(*) = 1
        ) b ON a.Foo = b.Foo
WHERE   b.Foo IS NULL
  • SQLFiddle Demo

For faster performance, add an index on column Foo.

ALTER TABLE tableName ADD INDEX(foo)
like image 191
John Woo Avatar answered Sep 21 '22 02:09

John Woo