From befb3425754fc77761cbfd026dde851b4973adc3 Mon Sep 17 00:00:00 2001 From: Julien DURAND Date: Tue, 1 Dec 2026 08:45:24 +0100 Subject: [PATCH 1/3] Title: Fix(Perf): index user preferences and reduce SQL queries per page (#237) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - [x] I have performed a self-review of my code. - [ ] I have added tests (when available) that prove my fix is effective or that my feature works. - [x] I have updated the CHANGELOG with a short functional description of the fix or new feature. - [ ] This change requires a documentation update. ## Description The fix makes the Lifecycle tab of the user preferences 8 times faster to build (467 ms to 56 ms) and removes 2 SQL queries the plugin ran on every HTTP request. The rendered output does not cha nge: the displayed models and selected locations of the preferences tab were checked against the database. Two configurations were measured on the same instance, with the same data: before (`main` branch) and after (fix), plus the plugin disabled on the pages that exist without it (core cost only). * **Instance:** GLPI 11.0.13-dev (11.0/bugfixes), e2e environment, Uninstall plugin 2.10.6, with and without the fix, debug mode and Xdebug off. * **Data:** 2 000 computers, 2 000 users, 200 locations, 200 uninstall models (100 in the root entity, recursive, 100 spread over 18 entities) and 50 000 user preferences. 20 users, the measured super-admin included, own half of the preferences (1 250 each). * **Measured scenarios:** the home page, a computer form, the Lifecycle tab of a computer with a model selected (`ajax/locations.php`), the Lifecycle tab of the user preferences, the model list and form, and a full cycle: create a computer, then apply the Uninstall model (`front/action.php`). * **Measurement method:** Playwright (Chromium) through the Playwright MCP, 5 measured runs per scenario after one discarded warm-up run; times come from the browser Performance API. * **SQL:** the MariaDB slow log with a zero threshold was enabled during a separate pass, one run per scenario; the count includes every query of the scenario's HTTP requests. ## SQL Queries On the preferences tab, queries on the plugin tables go from 219 to 2, and the SQL time from 413 ms to 27 ms. | Queries | Without plugin | Before | After | Difference | |---|---|---|---|---| | Computer form | 142 | 156 | 152 | -4 (3%) | | Computer, Lifecycle tab + model selected | n/a | 258 | 246 | -12 (5%) | | User preferences, Lifecycle tab | n/a | 770 | 549 | -221 (29%) | | Create a computer then apply the Uninstall model | n/a | 993 | 966 | -27 (3%) | Before the fix, `glpi_plugin_uninstall_preferences` had no index besides its primary key, and the preferences tab ran 2 queries per visible model (109 in the test): a lookup by user, entity and model, then a `getFromDB()`. Each lookup scanned the whole table (50 003 rows), which made 5.45 million rows examined and 382 ms of SQL on that tab. The table now has an index on `(users_id, entities_id, templates_id)`, created at install and added by the migration on existing installs. The preferences tab loads all the preferences of the us er in the active entity with a single query, and still creates the missing default preferences as before. The lookup run when a model is selected (`ajax/locations.php`) and when it is applied (` front/action.php`) now reads 1 row instead of 50 003. `plugin_init_uninstall()` called `Plugin::isActivated()`, which costs 2 queries on `glpi_plugins` per HTTP request while plugins are being initialized. The guard stays, because the core also run s this init while the plugin is installed, activated or uninstalled, but it now reads `Plugin::getPlugins()`, the list of active plugins loaded at boot, with no query. ## Response Times The preferences tab is clearly faster; on the other pages the difference stays within the measurement noise. | Median (min–max) | Before | After | |---|---|---| | User preferences, Lifecycle tab built by the server (ms) | 467.2 (443–550) | 56.4 (48–60) | | User preferences, page fully displayed (ms) | 742.1 (704–798) | 319.3 (300–338) | | Computer form, page fully displayed (ms) | 248.7 (226–294) | 236.3 (227–279) | | `ajax/locations.php` server time (ms) | 41.2 (33–50) | 33.1 (25–39) | The test database runs on the same machine, so each query saved costs less than 0.1 ms, which is why the 2 queries removed per request do not show in the page times. With a remote database each query adds a network round trip, so the gain per request grows with that latency. This is an estimate, not a measurement. The preferences tab still renders one location dropdown per model (728 KB for 109 models), which keeps about 140 ms of main-thread work in the browser; reducing it would need a change to the UI, which this PR does not include. --------- Co-authored-by: Julien Durand <300495670+jdurand-teclib@users.noreply.github.com> (cherry picked from commit 4f27882d090af53e23c0ad23615dd96350e7ece3) --- CHANGELOG.md | 1 + inc/preference.class.php | 42 ++++++++++++++++++++++++++++++++-------- setup.php | 5 +++-- 3 files changed, 38 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 89321bc..58c4d3e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ### Fixed - CI: fix Psalm cache directory, drop the stray `glpi-project/tools` composer dependency, declare a unique composer autoloader suffix +- Speed up the Lifecycle tab of the user preferences and the location lookup when applying a model, and remove the SQL queries the plugin ran on every page to check its own activation ## [2.10.6] - 2026-09-18 diff --git a/inc/preference.class.php b/inc/preference.class.php index beb9319..957aef5 100644 --- a/inc/preference.class.php +++ b/inc/preference.class.php @@ -58,6 +58,9 @@ public function handleLocationPref(array $input): array public function showFormUserPreferences() { + /** @var DBmysql $DB */ + global $DB; + $entity = $_SESSION['glpiactive_entity']; $userID = Session::getLoginUserID(); $templates = PluginUninstallUninstall::getAllTemplatesByEntity( @@ -78,21 +81,40 @@ public function showFormUserPreferences() echo "" . __s("Item's location after uninstall", "uninstall") . ""; echo ""; - foreach ($templates as $ID => $name) { - $pref_ID = self::checkIfPreferenceExistsByEntity($userID, $ID, $entity); + // One query for all the user preferences in this entity instead of two per model + $prefs = []; + foreach ( + $DB->request([ + 'SELECT' => ['id', 'templates_id', 'locations_action', 'locations_id'], + 'FROM' => self::getTable(), + 'WHERE' => [ + 'users_id' => $userID, + 'entities_id' => $entity, + ], + ]) as $row + ) { + $prefs[$row['templates_id']] ??= $row; + } - if (!$pref_ID) { - $pref_ID = self::addDefaultPreference($userID, $ID, $entity); + foreach ($templates as $ID => $name) { + if (!isset($prefs[$ID])) { + // Default preference: keep the previous location (see handleLocationPref()) + $prefs[$ID] = [ + 'id' => self::addDefaultPreference($userID, $ID, $entity), + 'locations_action' => 'old', + 'locations_id' => 0, + ]; } - $this->getFromDB($pref_ID); + $pref = $prefs[$ID]; + $pref_ID = $pref['id']; echo "" . $name . ""; echo ""; - $value = ($this->fields["locations_id"] ?? 0); + $value = ($pref["locations_id"] ?? 0); // Handle "old" special value, dropdown expect "-1" in this case - if ($this->fields['locations_action'] == "old" && $value == 0) { + if ($pref['locations_action'] == "old" && $value == 0) { $value = -1; } @@ -321,6 +343,9 @@ public static function install(Migration $migration) ), ); } + + // Preferences are always searched by user, entity and model + $migration->addKey($table, ['users_id', 'entities_id', 'templates_id'], 'users_entities_templates'); } else { // plugin nevers installed $query = "CREATE TABLE `" . $table . "` ( @@ -330,7 +355,8 @@ public static function install(Migration $migration) `templates_id` int {$default_key_sign} DEFAULT '0', `locations_action` varchar(10) NOT NULL DEFAULT 'set', `locations_id` int {$default_key_sign} DEFAULT '0', - PRIMARY KEY (`id`) + PRIMARY KEY (`id`), + KEY `users_entities_templates` (`users_id`, `entities_id`, `templates_id`) ) ENGINE=InnoDB DEFAULT CHARSET={$default_charset} COLLATE={$default_collation} ROW_FORMAT=DYNAMIC;"; $DB->doQuery($query); } diff --git a/setup.php b/setup.php index 03d8105..b66d474 100644 --- a/setup.php +++ b/setup.php @@ -53,8 +53,9 @@ function plugin_init_uninstall() Plugin::registerClass(PluginUninstallPreference::class, ['addtabon' => [Preference::class]]); Plugin::registerClass(PluginUninstallProfile::class, ['addtabon' => [Profile::class]]); - $plugin = new Plugin(); - if ($plugin->isActivated('uninstall')) { + // Core also runs this init while installing or activating the plugin: keep the guard, + // but read the list of active plugins filled at boot instead of querying glpi_plugins. + if (in_array('uninstall', Plugin::getPlugins(), true)) { $UNINSTALL_TYPES = [Computer::class, Monitor::class, NetworkEquipment::class, Peripheral::class, Phone::class, Printer::class]; $UNINSTALL_DIRECT_CONNECTIONS_TYPE = [Monitor::class, Peripheral::class, Phone::class, Printer::class]; From 7e46d19b57d5f8294453873cb06746753095344d Mon Sep 17 00:00:00 2001 From: Julien Durand <300495670+jdurand-teclib@users.noreply.github.com> Date: Wed, 2 Dec 2026 10:46:58 +0100 Subject: [PATCH 2/3] fix: Rector --- inc/preference.class.php | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/inc/preference.class.php b/inc/preference.class.php index 957aef5..a7a1038 100644 --- a/inc/preference.class.php +++ b/inc/preference.class.php @@ -97,14 +97,12 @@ public function showFormUserPreferences() } foreach ($templates as $ID => $name) { - if (!isset($prefs[$ID])) { - // Default preference: keep the previous location (see handleLocationPref()) - $prefs[$ID] = [ - 'id' => self::addDefaultPreference($userID, $ID, $entity), - 'locations_action' => 'old', - 'locations_id' => 0, - ]; - } + // Default preference: keep the previous location (see handleLocationPref()) + $prefs[$ID] ??= [ + 'id' => self::addDefaultPreference($userID, $ID, $entity), + 'locations_action' => 'old', + 'locations_id' => 0, + ]; $pref = $prefs[$ID]; $pref_ID = $pref['id']; From afe7448bb636fea16317fbca7dcf8fa694eb0a0a Mon Sep 17 00:00:00 2001 From: Julien DURAND Date: Fri, 4 Dec 2026 12:05:44 +0100 Subject: [PATCH 3/3] Enhance performance of Lifecycle tab and location lookupfix changelog entry in the wrong section --- CHANGELOG.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 58c4d3e..04fd772 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ## [Unreleased] +### Fixed + +- Speed up the Lifecycle tab of the user preferences and the location lookup when applying a model, and remove the SQL queries the plugin ran on every page to check its own activation + ### Added - Add links to apply the uninstall models from the item form footer (when the status dropdown is not replaced) @@ -21,7 +25,6 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ### Fixed - CI: fix Psalm cache directory, drop the stray `glpi-project/tools` composer dependency, declare a unique composer autoloader suffix -- Speed up the Lifecycle tab of the user preferences and the location lookup when applying a model, and remove the SQL queries the plugin ran on every page to check its own activation ## [2.10.6] - 2026-09-18