Question

I have a mysql InnoDB table with a million rows having an indexed column that does not have uniqueness constraint. I wish to mark the conflicting rows as having conflict and then add the uniqueness constraint to that column. The owners of those rows would then be fixing them offline. Here is the code that I have:

UPDATE users u SET fid = concat("@conflict_", fid, "_", RAND())
WHERE u.fid IN (SELECT fid from (
    SELECT fid
    FROM users
    WHERE fid IS NOT NULL
    GROUP BY fid
    HAVING COUNT(fid) > 1) 
AS TmpUsers);

The above query is very slow. It takes hours to update. What would be a good way to speed it up?

Update

Never use subqueries to update a table with a lot of records. Found a sample join at post 14431772:

UPDATE  users u 
    INNER JOIN
    (
        SELECT  fid
        FROM    users
        where fid IS NOT NULL
        GROUP   BY fid
        HAVING  COUNT(*) > 1
    ) b ON u.fid = b.fid
SET u.fid = concat("@conflict_", u.fid, "_", RAND());

Thanks @Powerlord.

No correct solution

OTHER TIPS

Just try something like this:

create temporary table myCustomTable as (
SELECT fid
FROM users
WHERE fid IS NOT NULL
GROUP BY fid
HAVING COUNT(fid) > 1);

UPDATE users u SET u.fid = concat("@conflict_", u.fid, "_", RAND())
WHERE u.fid IN (select mc.fid from myCustomTable mc);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top