From 2078bf58ff474db4d99f1fe66c4e67bea7c135da Mon Sep 17 00:00:00 2001 From: guillermo Date: Tue, 9 Jul 2024 10:09:00 +0200 Subject: [PATCH] feat: refs #7681 Optimization and refactor --- db/routines/vn/procedures/route_updateM3.sql | 39 +++++++++++++++----- 1 file changed, 30 insertions(+), 9 deletions(-) diff --git a/db/routines/vn/procedures/route_updateM3.sql b/db/routines/vn/procedures/route_updateM3.sql index 92d26b753..a2adf5b14 100644 --- a/db/routines/vn/procedures/route_updateM3.sql +++ b/db/routines/vn/procedures/route_updateM3.sql @@ -1,14 +1,35 @@ DELIMITER $$ -CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`route_updateM3`(vRoute INT) +CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`route_updateM3`( + vSelf INT +) BEGIN +/** + * Actualiza el volumen de la ruta. + * + * @param vSelf Id ruta + */ + DECLARE EXIT HANDLER FOR SQLEXCEPTION + BEGIN + ROLLBACK; + RESIGNAL; + END; - UPDATE vn.route r - LEFT JOIN ( - SELECT routeFk, SUM(volume) AS m3 - FROM saleVolume - WHERE routeFk = vRoute - ) v ON v.routeFk = r.id - SET r.m3 = IFNULL(v.m3,0) - WHERE r.id =vRoute; + CREATE OR REPLACE TEMPORARY TABLE tRouteVolume + ENGINE = MEMORY + SELECT IFNULL(SUM(volume), 0) volume + FROM saleVolume + WHERE routeFk = vSelf; + + START TRANSACTION; + + SELECT id FROM `route` WHERE id = vSelf FOR UPDATE; + + UPDATE `route` + SET m3 = (SELECT volume FROM tRouteVolume) + WHERE id = vSelf; + + COMMIT; + + DROP TEMPORARY TABLE tRouteVolume; END$$ DELIMITER ;