From 2078bf58ff474db4d99f1fe66c4e67bea7c135da Mon Sep 17 00:00:00 2001 From: guillermo Date: Tue, 9 Jul 2024 10:09:00 +0200 Subject: [PATCH 1/7] 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 ; From 042ea840558403519b68a8ff4b09d4289be124aa Mon Sep 17 00:00:00 2001 From: guillermo Date: Tue, 9 Jul 2024 10:14:55 +0200 Subject: [PATCH 2/7] feat: refs #7681 Changes --- db/routines/vn/procedures/route_updateM3.sql | 2 -- 1 file changed, 2 deletions(-) diff --git a/db/routines/vn/procedures/route_updateM3.sql b/db/routines/vn/procedures/route_updateM3.sql index a2adf5b14..07ceaaf5c 100644 --- a/db/routines/vn/procedures/route_updateM3.sql +++ b/db/routines/vn/procedures/route_updateM3.sql @@ -22,8 +22,6 @@ BEGIN START TRANSACTION; - SELECT id FROM `route` WHERE id = vSelf FOR UPDATE; - UPDATE `route` SET m3 = (SELECT volume FROM tRouteVolume) WHERE id = vSelf; From a725cd2840f4af816833b3a29277d26d3fb9721c Mon Sep 17 00:00:00 2001 From: guillermo Date: Tue, 9 Jul 2024 10:19:04 +0200 Subject: [PATCH 3/7] feat: refs #7681 Changes --- db/routines/vn/procedures/route_updateM3.sql | 3 +++ 1 file changed, 3 insertions(+) diff --git a/db/routines/vn/procedures/route_updateM3.sql b/db/routines/vn/procedures/route_updateM3.sql index 07ceaaf5c..c2049ca26 100644 --- a/db/routines/vn/procedures/route_updateM3.sql +++ b/db/routines/vn/procedures/route_updateM3.sql @@ -22,6 +22,9 @@ BEGIN START TRANSACTION; + -- Hago el into para que no de error en los triggers + SELECT id INTO vSelf FROM `route` WHERE id = vSelf FOR UPDATE; + UPDATE `route` SET m3 = (SELECT volume FROM tRouteVolume) WHERE id = vSelf; From 376adebe9d93a7dbe7562fc47d15a94c33baa916 Mon Sep 17 00:00:00 2001 From: guillermo Date: Tue, 9 Jul 2024 10:19:18 +0200 Subject: [PATCH 4/7] feat: refs #7681 Changes --- db/routines/vn/procedures/route_updateM3.sql | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/db/routines/vn/procedures/route_updateM3.sql b/db/routines/vn/procedures/route_updateM3.sql index c2049ca26..c5c85544c 100644 --- a/db/routines/vn/procedures/route_updateM3.sql +++ b/db/routines/vn/procedures/route_updateM3.sql @@ -22,7 +22,7 @@ BEGIN START TRANSACTION; - -- Hago el into para que no de error en los triggers + -- Hago el INTO para que no de error en los triggers SELECT id INTO vSelf FROM `route` WHERE id = vSelf FOR UPDATE; UPDATE `route` From 96d2bacb3dc2317e96aa679a7f82a62e9d9fa0c2 Mon Sep 17 00:00:00 2001 From: guillermo Date: Tue, 9 Jul 2024 10:22:42 +0200 Subject: [PATCH 5/7] feat: refs #7681 Changes --- db/routines/vn/procedures/route_updateM3.sql | 9 --------- 1 file changed, 9 deletions(-) diff --git a/db/routines/vn/procedures/route_updateM3.sql b/db/routines/vn/procedures/route_updateM3.sql index c5c85544c..6b73b3858 100644 --- a/db/routines/vn/procedures/route_updateM3.sql +++ b/db/routines/vn/procedures/route_updateM3.sql @@ -8,11 +8,6 @@ BEGIN * * @param vSelf Id ruta */ - DECLARE EXIT HANDLER FOR SQLEXCEPTION - BEGIN - ROLLBACK; - RESIGNAL; - END; CREATE OR REPLACE TEMPORARY TABLE tRouteVolume ENGINE = MEMORY @@ -20,8 +15,6 @@ BEGIN FROM saleVolume WHERE routeFk = vSelf; - START TRANSACTION; - -- Hago el INTO para que no de error en los triggers SELECT id INTO vSelf FROM `route` WHERE id = vSelf FOR UPDATE; @@ -29,8 +22,6 @@ BEGIN SET m3 = (SELECT volume FROM tRouteVolume) WHERE id = vSelf; - COMMIT; - DROP TEMPORARY TABLE tRouteVolume; END$$ DELIMITER ; From a2f2ed743d7cb516f15d47073b00378d88334aec Mon Sep 17 00:00:00 2001 From: guillermo Date: Wed, 10 Jul 2024 13:31:48 +0200 Subject: [PATCH 6/7] refactor: refs #7681 Changes --- db/routines/vn/procedures/route_updateM3.sql | 4 ---- 1 file changed, 4 deletions(-) diff --git a/db/routines/vn/procedures/route_updateM3.sql b/db/routines/vn/procedures/route_updateM3.sql index 6b73b3858..1df37ec8e 100644 --- a/db/routines/vn/procedures/route_updateM3.sql +++ b/db/routines/vn/procedures/route_updateM3.sql @@ -8,16 +8,12 @@ BEGIN * * @param vSelf Id ruta */ - CREATE OR REPLACE TEMPORARY TABLE tRouteVolume ENGINE = MEMORY SELECT IFNULL(SUM(volume), 0) volume FROM saleVolume WHERE routeFk = vSelf; - -- Hago el INTO para que no de error en los triggers - SELECT id INTO vSelf FROM `route` WHERE id = vSelf FOR UPDATE; - UPDATE `route` SET m3 = (SELECT volume FROM tRouteVolume) WHERE id = vSelf; From f687994a7f297019a5870cd86a7e2ec4a0b17042 Mon Sep 17 00:00:00 2001 From: guillermo Date: Thu, 18 Jul 2024 14:27:10 +0200 Subject: [PATCH 7/7] refactor: refs #7681 Requested changes --- db/routines/vn/procedures/route_updateM3.sql | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/db/routines/vn/procedures/route_updateM3.sql b/db/routines/vn/procedures/route_updateM3.sql index 1df37ec8e..98fdae5dd 100644 --- a/db/routines/vn/procedures/route_updateM3.sql +++ b/db/routines/vn/procedures/route_updateM3.sql @@ -8,16 +8,11 @@ BEGIN * * @param vSelf Id ruta */ - CREATE OR REPLACE TEMPORARY TABLE tRouteVolume - ENGINE = MEMORY - SELECT IFNULL(SUM(volume), 0) volume - FROM saleVolume - WHERE routeFk = vSelf; + DECLARE vVolume DECIMAL(10,1) + DEFAULT (SELECT SUM(volume) FROM saleVolume WHERE routeFk = vSelf); UPDATE `route` - SET m3 = (SELECT volume FROM tRouteVolume) + SET m3 = IFNULL(vVolume, 0) WHERE id = vSelf; - - DROP TEMPORARY TABLE tRouteVolume; END$$ DELIMITER ;