From c73d5521030d5f733d93ffc8f20f3be1000c5a5e Mon Sep 17 00:00:00 2001 From: Stanislas Kita <7335054+stonebuzz@users.noreply.github.com> Date: Wed, 25 Nov 2026 14:31:13 +0100 Subject: [PATCH 1/6] Perf: filter block values on itemtype to use the unique index Value rows were looked up by items_id only, so MariaDB scanned the whole value table on every form display, save and purge. Adding itemtype lets the itemtype_item_container unique key be used, as search joins already do. --- inc/container.class.php | 10 +++++----- inc/field.class.php | 1 + 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/inc/container.class.php b/inc/container.class.php index 6b9ec560..4d0fec7d 100644 --- a/inc/container.class.php +++ b/inc/container.class.php @@ -1055,7 +1055,7 @@ public static function preItemPurge($item) } $fields = new $classname(); - $fields->deleteByCriteria(['items_id' => $item->fields['id']], true); + $fields->deleteByCriteria(['itemtype' => $itemtype, 'items_id' => $item->fields['id']], true); } } @@ -1790,7 +1790,7 @@ public function updateFieldsValues($data, $itemtype, $massiveaction = false) $obj = new $classname(); - $exist = $obj->getFromDBByCrit(['items_id' => $items_id]); + $exist = $obj->getFromDBByCrit(['itemtype' => $itemtype, 'items_id' => $items_id]); // Convert "multiple" values into a JSON string $multiple_fields_iterator = $DB->request([ @@ -1835,7 +1835,7 @@ public function updateFieldsValues($data, $itemtype, $massiveaction = false) $obj = $dbu->getItemForItemtype($classname); if ($obj !== false) { - if ($obj->getFromDBByCrit(['items_id' => $items_id]) === false) { + if ($obj->getFromDBByCrit(['itemtype' => $itemtype, 'items_id' => $items_id]) === false) { // add fields data $obj->add($data); } else { @@ -2296,7 +2296,7 @@ private static function getStoredValues(self $container, string $itemtype, int $ } $values_obj = (new DbUtils())->getItemForItemtype(self::getClassname($itemtype, $container->fields['name'])); - if ($values_obj === false || !$values_obj->getFromDBByCrit(['items_id' => $items_id])) { + if ($values_obj === false || !$values_obj->getFromDBByCrit(['itemtype' => $itemtype, 'items_id' => $items_id])) { return []; } @@ -2741,7 +2741,7 @@ private static function checkContainerMandatory(CommonDBTM $item, PluginFieldsCo $classname = self::getClassname($item::getType(), $loc_c->fields['name']); $dbu = new DbUtils(); $obj = $dbu->getItemForItemtype($classname); - if ($obj !== false && $obj->getFromDBByCrit(['items_id' => $item->getID()])) { + if ($obj !== false && $obj->getFromDBByCrit(['itemtype' => $item::getType(), 'items_id' => $item->getID()])) { $data += $obj->fields; } else { $data += self::getPendingValues($item, $c_id); diff --git a/inc/field.class.php b/inc/field.class.php index 82817259..c70fd07f 100644 --- a/inc/field.class.php +++ b/inc/field.class.php @@ -1280,6 +1280,7 @@ public static function prepareHtmlFields( if ($obj instanceof CommonDBTM) { $found_values = $obj->find( [ + 'itemtype' => $item->getType(), 'plugin_fields_containers_id' => $first_field['plugin_fields_containers_id'], 'items_id' => $item->getID(), ], From 444cc07b238aef281ffe987900cd79db3736a1d1 Mon Sep 17 00:00:00 2001 From: Stanislas Kita <7335054+stonebuzz@users.noreply.github.com> Date: Wed, 25 Nov 2026 14:31:13 +0100 Subject: [PATCH 2/6] Perf: load label translations once per request getLabelFor() ran one query per label, and search options call it twice per field on every form, list and block refresh (up to 266 queries on a ticket form). Translations of the session language are now loaded in a single query and kept in memory, reset when a translation is saved or purged. --- inc/labeltranslation.class.php | 51 ++++++++++++++++++++++++++++------ 1 file changed, 43 insertions(+), 8 deletions(-) diff --git a/inc/labeltranslation.class.php b/inc/labeltranslation.class.php index 8b1ff9ac..e8c4fb23 100644 --- a/inc/labeltranslation.class.php +++ b/inc/labeltranslation.class.php @@ -38,6 +38,9 @@ class PluginFieldsLabelTranslation extends CommonDBChild public static $items_id = 'items_id'; + /** @var array>> */ + private static array $labels_by_language = []; + /** * Install or update plugin base data. * @@ -125,6 +128,24 @@ public function prepareInputForAdd($input) return $input; } + public function post_addItem() + { + parent::post_addItem(); + self::$labels_by_language = []; + } + + public function post_updateItem($history = true) + { + parent::post_updateItem($history); + self::$labels_by_language = []; + } + + public function post_purgeItem() + { + parent::post_purgeItem(); + self::$labels_by_language = []; + } + public static function createForItem(CommonDBTM $item) { @@ -374,16 +395,30 @@ public static function getAlreadyTranslatedForItem($itemtype, $items_id) */ public static function getLabelFor(array $item) { - $obj = new self(); - $found = $obj->find(['itemtype' => $item['itemtype'], - 'items_id' => $item['id'], - 'language' => $_SESSION['glpilanguage'], - ]); + $language = $_SESSION['glpilanguage']; + self::$labels_by_language[$language] ??= self::loadLabels($language); - if (count($found) > 0) { - return array_values($found)[0]['label']; + return self::$labels_by_language[$language][$item['itemtype']][(int) $item['id']] ?? $item['label'] ?? ''; + } + + /** + * @return array> + */ + private static function loadLabels(string $language): array + { + /** @var DBmysql $DB */ + global $DB; + + $labels = []; + $iterator = $DB->request([ + 'SELECT' => ['itemtype', 'items_id', 'label'], + 'FROM' => self::getTable(), + 'WHERE' => ['language' => $language], + ]); + foreach ($iterator as $row) { + $labels[$row['itemtype']][(int) $row['items_id']] = (string) $row['label']; } - return $item['label'] ?? ''; + return $labels; } } From db3e5623ef08d21a07f3438cf01c0c5da41a0645 Mon Sep 17 00:00:00 2001 From: Stanislas Kita <7335054+stonebuzz@users.noreply.github.com> Date: Wed, 25 Nov 2026 14:31:13 +0100 Subject: [PATCH 3/6] Perf: read plugin activation state from the boot list at init Plugin::isPluginActive() queries glpi_plugins while plugins are being initialized, so plugin_init_fields() ran extra queries on every HTTP request. The activated plugin list is already built at boot and gives the same answer, including during plugin install and activation. --- setup.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/setup.php b/setup.php index 1ed1bebc..7ed0dd97 100644 --- a/setup.php +++ b/setup.php @@ -101,7 +101,8 @@ function plugin_init_fields() $pluginfields_autoloader = new PluginFieldsAutoloader([PLUGINFIELDS_CLASS_PATH]); $pluginfields_autoloader->register(); - if (!Plugin::isPluginActive('fields')) { + // Activated plugins are listed at boot; isPluginActive() queries the DB until init is over. + if (!in_array('fields', Plugin::getPlugins(), true)) { return; } @@ -202,7 +203,7 @@ function plugin_init_fields() } // Add Fields to Datainjection - if (Plugin::isPluginActive('datainjection')) { + if (in_array('datainjection', Plugin::getPlugins(), true)) { $PLUGIN_HOOKS['plugin_datainjection_populate']['fields'] = 'plugin_datainjection_populate_fields'; } From 2c71eb5a3d0b468ff3f41db0b7a6cd89acd30d38 Mon Sep 17 00:00:00 2001 From: Stanislas Kita <7335054+stonebuzz@users.noreply.github.com> Date: Wed, 25 Nov 2026 14:31:13 +0100 Subject: [PATCH 4/6] Perf: load field definitions once per request in list cells getSpecificValueToDisplay() and getSpecificValueToSelect() loaded the field definition with getFromDB() for every displayed cell, about 80 queries on a 20-row list with 4 plugin columns. Definitions are now kept in memory per field id. --- inc/abstractcontainerinstance.class.php | 49 ++++++++++++++++--------- 1 file changed, 31 insertions(+), 18 deletions(-) diff --git a/inc/abstractcontainerinstance.class.php b/inc/abstractcontainerinstance.class.php index 5d89ce70..34bd8d97 100644 --- a/inc/abstractcontainerinstance.class.php +++ b/inc/abstractcontainerinstance.class.php @@ -103,12 +103,12 @@ public static function getSpecificValueToSelect($field, $name = '', $values = '' $field_id = $options['searchopt']['pfields_fields_id'] ?? null; - $field_specs = new PluginFieldsField(); - if ($field_id !== null && $field_specs->getFromDB($field_id)) { + $field_specs = $field_id !== null ? self::getFieldSpecs((int) $field_id) : null; + if ($field_specs !== null) { $dropdown_matches = []; if ( - preg_match('/^dropdown-(?.+)$/i', (string) $field_specs->fields['type'], $dropdown_matches) === 1 - && $field_specs->fields['multiple'] + preg_match('/^dropdown-(?.+)$/i', (string) $field_specs['type'], $dropdown_matches) === 1 + && $field_specs['multiple'] ) { $itemtype = $dropdown_matches['class']; if (!is_a($itemtype, CommonDBTM::class, true)) { @@ -127,10 +127,10 @@ public static function getSpecificValueToSelect($field, $name = '', $values = '' 'display' => false, ]); } elseif ( - $field_specs->fields['type'] === 'dropdown' - && $field_specs->fields['multiple'] + $field_specs['type'] === 'dropdown' + && $field_specs['multiple'] ) { - $itemtype = PluginFieldsDropdown::getClassname($field_specs->fields['name']); + $itemtype = PluginFieldsDropdown::getClassname($field_specs['name']); return Dropdown::show($itemtype, [ 'name' => $name, @@ -151,20 +151,20 @@ public static function getSpecificValueToDisplay($field, $values, array $options $field_id = $options['searchopt']['pfields_fields_id'] ?? null; - $field_specs = new PluginFieldsField(); - if ($field_id !== null && $field_specs->getFromDB($field_id)) { + $field_specs = $field_id !== null ? self::getFieldSpecs((int) $field_id) : null; + if ($field_specs !== null) { $dropdown_matches = []; if ( - preg_match('/^dropdown-(?.+)$/i', (string) $field_specs->fields['type'], $dropdown_matches) === 1 - && $field_specs->fields['multiple'] + preg_match('/^dropdown-(?.+)$/i', (string) $field_specs['type'], $dropdown_matches) === 1 + && $field_specs['multiple'] ) { $itemtype = $dropdown_matches['class']; if (!is_a($itemtype, CommonDBTM::class, true)) { return ''; // Itemtype not exists (maybe a deactivated plugin) } - if (empty($values[$field]) && !empty($field_specs->fields['default_value'])) { - $values[$field] = $field_specs->fields['default_value']; + if (empty($values[$field]) && !empty($field_specs['default_value'])) { + $values[$field] = $field_specs['default_value']; } if (empty($values[$field])) { @@ -186,13 +186,13 @@ public static function getSpecificValueToDisplay($field, $values, array $options return implode($options['separator'] ?? '
', $names); } elseif ( - $field_specs->fields['type'] === 'dropdown' - && $field_specs->fields['multiple'] + $field_specs['type'] === 'dropdown' + && $field_specs['multiple'] ) { - $itemtype = PluginFieldsDropdown::getClassname($field_specs->fields['name']); + $itemtype = PluginFieldsDropdown::getClassname($field_specs['name']); if (empty($values[$field])) { - if (!empty($field_specs->fields["default_value"])) { - $values[$field] = $field_specs->fields['default_value']; + if (!empty($field_specs["default_value"])) { + $values[$field] = $field_specs['default_value']; } else { return ''; // Value not defined } @@ -213,6 +213,19 @@ public static function getSpecificValueToDisplay($field, $values, array $options return parent::getSpecificValueToDisplay($field, $values, $options); } + private static function getFieldSpecs(int $field_id): ?array + { + /** @var array|null> $specs */ + static $specs = []; + + if (!array_key_exists($field_id, $specs)) { + $field = new PluginFieldsField(); + $specs[$field_id] = $field->getFromDB($field_id) ? $field->fields : null; + } + + return $specs[$field_id]; + } + public static function addField($fieldname, $type, array $options = []) { $migration = new PluginFieldsMigration('0'); From 12fed845b94ccfd861dec23600964d2e1960bb06 Mon Sep 17 00:00:00 2001 From: Stanislas Kita <7335054+stonebuzz@users.noreply.github.com> Date: Wed, 25 Nov 2026 14:31:13 +0100 Subject: [PATCH 5/6] Perf: read blocks once per request during plugin init getUsedItemtypes() and getEntries() each queried the containers table on every HTTP request; they now share one in-memory list, reset when a block is added, updated or purged. hasAvailableFields() counts the fields of all available blocks in one query instead of two queries per block. --- inc/container.class.php | 67 +++++++++++++++++++++----------------- inc/questiontype.class.php | 13 ++++---- 2 files changed, 45 insertions(+), 35 deletions(-) diff --git a/inc/container.class.php b/inc/container.class.php index 4d0fec7d..17392d88 100644 --- a/inc/container.class.php +++ b/inc/container.class.php @@ -43,6 +43,9 @@ class PluginFieldsContainer extends CommonDBTM */ private static ?WeakMap $pending_values = null; + /** @var array>|null */ + private static ?array $containers = null; + public static function canCreate(): bool { return self::canUpdate(); @@ -855,6 +858,8 @@ public function prepareInputForAdd($input) // phpcs:ignore PSR1.Methods.CamelCapsMethodName public function post_addItem() { + self::$containers = null; + if (!isset($this->input['clone']) || !$this->input['clone']) { //create profiles associated to this container PluginFieldsProfile::createForContainer($this); @@ -973,6 +978,20 @@ public static function generateTemplate($fields) return true; } + // phpcs:ignore PSR1.Methods.CamelCapsMethodName + public function post_updateItem($history = true) + { + parent::post_updateItem($history); + self::$containers = null; + } + + // phpcs:ignore PSR1.Methods.CamelCapsMethodName + public function post_purgeItem() + { + parent::post_purgeItem(); + self::$containers = null; + } + // phpcs:ignore PSR1.Methods.CamelCapsMethodName public function pre_deleteItem() { @@ -1570,20 +1589,15 @@ public static function getEntries($type = 'tab', $full = false): array /** @var DBmysql $DB */ global $DB; - $condition = [ - 'is_active' => 1, - ]; - if ($type !== 'all') { - $condition[] = ['type' => $type]; - } - if (!$DB->tableExists(self::getTable())) { return []; } $itemtypes = []; - $container = new self(); - $found = $container->find($condition, 'label'); + $found = array_filter( + self::getContainers(), + fn(array $container): bool => $container['is_active'] == 1 && ($type === 'all' || $container['type'] === $type), + ); if ( $found === [] @@ -1648,28 +1662,15 @@ public static function getEntries($type = 'tab', $full = false): array public static function getUsedItemtypes($type = 'all', $must_be_active = false) { - /** @var DBmysql $DB */ - global $DB; $itemtypes = []; - $where = []; - - if ($type !== 'all') { - $where['type'] = $type; - } - - if ($must_be_active) { - $where['is_active'] = 1; - } - - $iterator = $DB->request([ - 'SELECT' => 'itemtypes', - 'DISTINCT' => true, - 'FROM' => self::getTable(), - 'WHERE' => $where, - ]); + $containers = array_filter( + self::getContainers(), + fn(array $container): bool => ($type === 'all' || $container['type'] === $type) + && (!$must_be_active || $container['is_active'] == 1), + ); - foreach ($iterator as $data) { - $jsonitemtype = PluginFieldsToolbox::decodeJSONItemtypes($data['itemtypes']); + foreach (array_unique(array_column($containers, 'itemtypes')) as $json_itemtypes) { + $jsonitemtype = PluginFieldsToolbox::decodeJSONItemtypes($json_itemtypes); if (is_array($jsonitemtype)) { $itemtypes = array_merge($itemtypes, $jsonitemtype); } @@ -1678,6 +1679,14 @@ public static function getUsedItemtypes($type = 'all', $must_be_active = false) return $itemtypes; } + /** + * @return array> + */ + private static function getContainers(): array + { + return self::$containers ??= (new self())->find([], 'label'); + } + public function getTabNameForItem(CommonGLPI $item, $withtemplate = 0) { if ($withtemplate) { diff --git a/inc/questiontype.class.php b/inc/questiontype.class.php index c983c7ab..73fc743c 100644 --- a/inc/questiontype.class.php +++ b/inc/questiontype.class.php @@ -521,13 +521,14 @@ public static function getFieldsFromBlock(?int $block_id): array public static function hasAvailableFields(): bool { $blocks = (new self())->getAvailableBlocks(); - foreach (array_keys($blocks) as $block_id) { - $fields = (new self())->getFieldsFromBlock($block_id); - if ($fields !== []) { - return true; - } + if ($blocks === []) { + return false; } - return false; + return countElementsInTable(PluginFieldsField::getTable(), [ + 'is_active' => 1, + 'plugin_fields_containers_id' => array_keys($blocks), + 'NOT' => ['type' => 'header'], + ]) > 0; } } From 7f2d5ad617fe4206be64e461d58777ad46d83be0 Mon Sep 17 00:00:00 2001 From: Stanislas Kita <7335054+stonebuzz@users.noreply.github.com> Date: Wed, 25 Nov 2026 14:37:29 +0100 Subject: [PATCH 6/6] Update changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index fdc8bb8e..9c0aa80c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). - Fix search on number fields: `is` / `is not` ignoring values from 1000, `<` / `>` comparing values as text, and `is not` excluding items without value - Fix `Some mandatory fields are empty` error on a filled dropdown field when its block table contains a leftover column with the same name - Fix GenericObject migration error message to name the required 3.0.x version +- Fix slow forms, lists and saves on large databases: block values were read without using their index, and label translations, field definitions and block lists were queried again for every label, list cell and request ## [1.24.7] - 2026-10-28