K
K
Kerm2018-11-13 10:10:00
MySQL
Kerm, 2018-11-13 10:10:00

How to optimize a stored procedure?

When updating the online store, a temporary table is created, after entering the data into which, the stored procedure is executed:

DELIMITER $$
CREATE DEFINER=`root`@`%` PROCEDURE `sync_fl`()
BEGIN
  DROP TABLE IF EXISTS `sh_prod_im__`;
  ALTER TABLE `sh_prod_im` RENAME TO `sh_prod_im__`;
  ALTER TABLE `sh_prod_im_` RENAME TO `sh_prod_im`;
END$$
DELIMITER ;

The online store has a lot of visits per day and it looks like someone catches the moment of renaming the table and an error occurs (Table 'sh_prod_im' doesn't exist.), how can I optimize this process so that such problems do not arise? I read that the process of changing the name of the table takes 0.10 sec and this does not work.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Rsa97, 2018-11-13
@Kerm

Transactions won't help here, because DROP TABLE/ALTER TABLE causes an immediate COMMIT.
It remains only to change the table update procedure itself. For example like this:

BEGIN TRANSACTION;
UPDATE `sh_prod_im` SET `updated` = 0;

Then insert/update rows:
INSERT 
  INTO `sh_prod_im` (`article`, ..., `updated`) 
  VALUES (:article, ..., 1) 
  ON DUPLICATE KEY UPDATE ..., `updated` = 1;

And remove the non-updated ones:
DELETE FROM `sh_prod_im` WHERE `updated` = 0;
COMMIT;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question