MySQL write stored procedure to delete duplicate data

In the previous development process, when using sql to query whether an object exists, an exception is always reported. The exception means that multiple objects cannot be set into one object, and then through group by, it is found that there are hundreds of thousands of duplicate data in the data. Article, so I wrote a stored procedure to delete duplicates. After executing it, the efficiency is still super fast. The next is sql.

CREATE PROCEDURE delete_repeatUerPermission ()

BEGIN DECLARE userPermId VARCHAR (32);

DECLARE accountId VARCHAR (32);

DECLARE permissionId VARCHAR (32);

DECLARE _done INT DEFAULT 0;

DECLARE _Cur CURSOR FOR SELECT Id AS userPermId, account_id AS accountId, permission_id AS permissionId FROM idb_permission GROUP BY account_id, permission_id, `status` HAVING COUNT(*) > 1 AND `status` = "1";

DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET _done = 1;

OPEN _Cur;

FETCH _Cur INTO userPermId, accountId, permissionId;

WHILE (_done <> 1) DO DELETE FROM idb_permission WHERE Id <> userPermId AND account_id = accountId AND permission_id = permissionId AND `status` = "1";

FETCH _Cur INTO userPermId, accountId, permissionId;

END WHILE;

CLOSE _Cur;

END;

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326566261&siteId=291194637