Skip to content Skip to sidebar Skip to footer

Delete Duplicates In Postgres

I want to delete all but one row for a given duplicate 'external_id'. The query below takes about two minutes to run for my table of 5,000,000 rows, and I feel like there's got to

Solution 1:

DELETE from posts del 
WHERE EXISTS (
        SELECT *
        FROM posts ex
        WHERE ex.external_id = del.external_id
        AND ex.id < del.id -- if you want to keep the lowest id
        -- AND ex.id > del.id -- if you want to keep the highest id
        );

Post a Comment for "Delete Duplicates In Postgres"