mysql存储过程在“检查权限”上花费太多时间

时间:2019-05-20 11:56:43

标签: mysql stored-procedures triggers

我有一个正在运行的触发器,它调用存储过程。一切正常,但有时会卡在“检查权限”上。

任何帮助都将受到高度赞赏。

我的代码:

CALL `insert_tags_new (NEW.id_users, new.user_tags);`

存储过程:

创建

    PROCEDURE `insert_tags_new`(uid int(11), fullstr TEXT)
    BEGIN

        DECLARE a INT DEFAULT 0 ;
        DECLARE str VARCHAR(255);
        DECLARE len INT DEFAULT 0;

        SET @delete_trigger_disabled =  1;
        DELETE FROM users_tags_mapping WHERE tag_id NOT IN (fullstr) AND user_id=uid;

        SET len = LENGTH(fullstr) - LENGTH(REPLACE(fullstr, ',', '')) + 1;
    simple_loop: LOOP
            SET a=a+1;
            SET str=SPLIT_STR(fullstr,',',a);

            # Exit the loop
            IF a > len THEN
                LEAVE simple_loop;
            END IF;

            IF str != '' THEN
                IF NOT EXISTS (SELECT * FROM users_tags_mapping WHERE user_id=uid AND tag_id=str) THEN
                    INSERT INTO users_tags_mapping(user_id, tag_id) VALUES (uid, str);
                END IF;
            END IF;

    END LOOP simple_loop;

    SET @delete_trigger_disabled = NULL;
    END

0 个答案:

没有答案