Deleting duplicates from a large table

前端 未结 5 978
没有蜡笔的小新
没有蜡笔的小新 2021-01-02 02:40

I have quite a large table with 19 000 000 records, and I have problem with duplicate rows. There\'s a lot of similar questions even here in SO, but none of them seems to gi

5条回答
  •  情歌与酒
    2021-01-02 03:01

    UPDATE table SET datetime  = null 
    WHERE location_id IN (
    SELECT location_id 
    FROM table as tableBis
    WHERE tableBis.location_id = table.location_id
    AND table.datetime > tableBis.datetime)
    
    SELECT * INTO tableCopyWithNoDuplicate FROM table WHERE datetime is not null
    
    DROp TABLE table 
    
    RENAME tableCopyWithNoDuplicate to table
    

    So you keep the line with the lower datetime. I'm not sure about perf, it depends on your table column, your server etc...

提交回复
热议问题