From b444617a00eddf8eeb9e208e4178b5b56b71e9d2 Mon Sep 17 00:00:00 2001 From: Michael Date: Sun, 21 Feb 2021 08:55:09 +0000 Subject: [PATCH] Non blocking item-uri cleanup --- src/Worker/CleanItemUri.php | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/src/Worker/CleanItemUri.php b/src/Worker/CleanItemUri.php index 8f74d99fb0..c883083b5f 100644 --- a/src/Worker/CleanItemUri.php +++ b/src/Worker/CleanItemUri.php @@ -41,11 +41,20 @@ class CleanItemUri return; } Logger::notice('Start deleting orphaned URI-ID', ['last-id' => $item['uri-id']]); - $ret = DBA::e("DELETE FROM `item-uri` WHERE `id` < ? - AND NOT EXISTS(SELECT `uri-id` FROM `post-user` WHERE `uri-id` = `item-uri`.`id`) - AND NOT EXISTS(SELECT `parent-uri-id` FROM `post-user` WHERE `parent-uri-id` = `item-uri`.`id`) - AND NOT EXISTS(SELECT `thr-parent-id` FROM `post-user` WHERE `thr-parent-id` = `item-uri`.`id`) - AND NOT EXISTS(SELECT `external-id` FROM `post-user` WHERE `external-id` = `item-uri`.`id`)", $item['uri-id']); - Logger::notice('Orphaned URI-ID entries removed', ['result' => $ret, 'rows' => DBA::affectedRows()]); + $uris = DBA::select('item-uri', ['id'], ["`id` < ? + AND NOT EXISTS(SELECT `uri-id` FROM `post` WHERE `uri-id` = `item-uri`.`id`) + AND NOT EXISTS(SELECT `parent-uri-id` FROM `post` WHERE `parent-uri-id` = `item-uri`.`id`) + AND NOT EXISTS(SELECT `thr-parent-id` FROM `post` WHERE `thr-parent-id` = `item-uri`.`id`) + AND NOT EXISTS(SELECT `external-id` FROM `post` WHERE `external-id` = `item-uri`.`id`)", $item['uri-id']]); + + $affected_count = 0; + while ($rows = DBA::toArray($uris, false, 100)) { + $ids = array_column($rows, 'id'); + DBA::delete('item-uri', ['id' => $ids]); + $affected_count += DBA::affectedRows(); + Logger::info('Updated', ['rows' => $affected_count]); + } + DBA::close($uris); + Logger::notice('Orphaned URI-ID entries removed', ['rows' => $affected_count]); } }