E
E
entermix2018-08-03 20:49:59
MySQL
entermix, 2018-08-03 20:49:59

How to optimize a MySQL procedure?

There is MariaDB 10.3 and something like this MySQL procedure:

BEGIN
        DECLARE done INT DEFAULT 0;

        DECLARE v_id, v_user_id, v_created INT;
        DECLARE cur1 CURSOR FOR SELECT `id`, `user_id`, `created` FROM `table` WHERE `update` = 0 ORDER BY `id` DESC;

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

        UPDATE `table` SET `update` = 0 WHERE 1;

        OPEN cur1;

            WHILE done = 0 DO 

            FETCH cur1 INTO v_id, user_id, v_created;

                IF done = 0 THEN

                // Несколько различных SQL заросов (INSERT IGNORE, INSER UPDATE, UPDATE, SELECT)

                UPDATE `table` SET `update` = 1 WHERE `id` = v_id LIMIT 1;

                END IF;

            END WHILE;

        CLOSE cur1;
    END

The table has more than 1 million rows, the procedure takes several days. How can you optimize it? Perhaps you need to add locks and run the procedure multiple times in parallel? But how to properly set locks so that a row in the table is not traversed several times and this does not affect the selection for the cursor of a procedure that works in parallel?
UPD: https://jira.mariadb.org/browse/MDEV-13115

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question