-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1066 from France-ioi/extractInsertsFromTrigger
Fix slow trigger before_delete_items_items
- Loading branch information
Showing
4 changed files
with
137 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
db/migrations/2405071119_results_propagate_mark_item_id.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
-- +migrate Up | ||
CREATE TABLE `results_propagate_items` ( | ||
`item_id` BIGINT(19) NOT NULL, | ||
PRIMARY KEY (`item_id`), | ||
CONSTRAINT `fk_results_propagate_items_to_items` FOREIGN KEY (`item_id`) REFERENCES `items` (`id`) ON DELETE CASCADE | ||
) | ||
COMMENT='Used by the algorithm that computes results. All results for the item_id have to be recomputed when the item_id is in this table.' | ||
COLLATE='utf8_general_ci' | ||
ENGINE=InnoDB | ||
; | ||
|
||
-- +migrate Down | ||
DROP TABLE `results_propagate_items`; |
42 changes: 42 additions & 0 deletions
42
db/migrations/2405071129_results_propagate_insert_item_id_in_table_instead_of_trigger.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
-- +migrate Up | ||
|
||
DROP TRIGGER `before_delete_items_items`; | ||
-- +migrate StatementBegin | ||
CREATE TRIGGER `before_delete_items_items` BEFORE DELETE ON `items_items` FOR EACH ROW BEGIN | ||
INSERT IGNORE INTO `items_propagate` (`id`, `ancestors_computation_state`) | ||
VALUES (OLD.child_item_id, 'todo') ON DUPLICATE KEY UPDATE `ancestors_computation_state` = 'todo'; | ||
|
||
INSERT IGNORE INTO `permissions_propagate` (`group_id`, `item_id`, `propagate_to`) | ||
SELECT `permissions_generated`.`group_id`, `permissions_generated`.`item_id`, 'children' as `propagate_to` | ||
FROM `permissions_generated` | ||
WHERE `permissions_generated`.`item_id` = OLD.`parent_item_id`; | ||
|
||
-- Some results' ancestors should probably be removed | ||
-- DELETE FROM `results` WHERE ... | ||
|
||
INSERT IGNORE INTO `results_propagate_items` (`item_id`) VALUES (OLD.`parent_item_id`); | ||
END | ||
-- +migrate StatementEnd | ||
|
||
-- +migrate Down | ||
|
||
DROP TRIGGER `before_delete_items_items`; | ||
-- +migrate StatementBegin | ||
CREATE TRIGGER `before_delete_items_items` BEFORE DELETE ON `items_items` FOR EACH ROW BEGIN | ||
INSERT IGNORE INTO `items_propagate` (`id`, `ancestors_computation_state`) | ||
VALUES (OLD.child_item_id, 'todo') ON DUPLICATE KEY UPDATE `ancestors_computation_state` = 'todo'; | ||
|
||
INSERT IGNORE INTO `permissions_propagate` (`group_id`, `item_id`, `propagate_to`) | ||
SELECT `permissions_generated`.`group_id`, `permissions_generated`.`item_id`, 'children' as `propagate_to` | ||
FROM `permissions_generated` | ||
WHERE `permissions_generated`.`item_id` = OLD.`parent_item_id`; | ||
-- Some results' ancestors should probably be removed | ||
-- DELETE FROM `results` WHERE ... | ||
|
||
INSERT INTO `results_propagate` | ||
SELECT `participant_id`, `attempt_id`, `item_id`, 'to_be_propagated' AS `state` | ||
FROM `results` | ||
WHERE `item_id` = OLD.`parent_item_id` | ||
ON DUPLICATE KEY UPDATE `state` = 'to_be_recomputed'; | ||
END | ||
-- +migrate StatementEnd |