From 09890487cd9f5d625f1d63425c44d435f8574400 Mon Sep 17 00:00:00 2001 From: plegall Date: Fri, 6 Oct 2023 15:03:22 +0200 Subject: [PATCH] fixes #1830 faster way to calculate number of orphans * store count result in config table, as a cache * resets this cache on invalidate_user_cache * instead of a costly SQL query with a LEFT JOIN, use 2 simple COUNT in the 2 tables, and compare --- admin.php | 2 +- admin/include/functions.php | 28 ++++++++++++++++++++++++++++ admin/intro.php | 2 +- 3 files changed, 30 insertions(+), 2 deletions(-) diff --git a/admin.php b/admin.php index 0994606ab..0c552e376 100644 --- a/admin.php +++ b/admin.php @@ -301,7 +301,7 @@ $page['nb_orphans'] = 0; list($page['nb_photos_total']) = pwg_db_fetch_row(pwg_query('SELECT COUNT(*) FROM '.IMAGES_TABLE)); if ($page['nb_photos_total'] < 100000) // 100k is already a big gallery { - $page['nb_orphans'] = count(get_orphans()); + $page['nb_orphans'] = count_orphans(); } $template->assign( diff --git a/admin/include/functions.php b/admin/include/functions.php index deb78c1f2..25be5ab4d 100644 --- a/admin/include/functions.php +++ b/admin/include/functions.php @@ -2206,6 +2206,7 @@ UPDATE '.USER_CACHE_TABLE.' SET need_update = \'true\';'; pwg_query($query); } + conf_delete_param('count_orphans'); trigger_notify('invalidate_user_cache', $full); } @@ -3269,6 +3270,33 @@ SELECT path return count($ids); } +function count_orphans() +{ + if (is_null(conf_get_param('count_orphans'))) + { + // we don't care about the list of image_ids, we only care about the number + // of orphans, so let's use a faster method than calling count(get_orphans()) + $query = ' +SELECT + COUNT(*) + FROM '.IMAGES_TABLE.' +;'; + list($image_counter_all) = pwg_db_fetch_row(pwg_query($query)); + + $query = ' +SELECT + COUNT(DISTINCT(image_id)) + FROM '.IMAGE_CATEGORY_TABLE.' +;'; + list($image_counter_in_categories) = pwg_db_fetch_row(pwg_query($query)); + + $counter = $image_counter_all - $image_counter_in_categories; + conf_update_param('count_orphans', $counter, true); + } + + return conf_get_param('count_orphans'); +} + /** * Return the list of image ids associated to no album * diff --git a/admin/intro.php b/admin/intro.php index cb0c73093..44827b95f 100644 --- a/admin/intro.php +++ b/admin/intro.php @@ -59,7 +59,7 @@ $nb_orphans = $page['nb_orphans']; // already calculated in admin.php if ($page['nb_photos_total'] >= 100000) // but has not been calculated on a big gallery, so force it now { - $nb_orphans = count(get_orphans()); + $nb_orphans = count_orphans(); } if ($nb_orphans > 0)