From 75f4bb70e1a8b71621308be5f7c024516c319656 Mon Sep 17 00:00:00 2001 From: stonebuzz Date: Mon, 6 May 2024 14:13:31 +0200 Subject: [PATCH 1/6] Optimize(Search): use LEFT JOIN filter on SELECT statement --- src/ConsumableItem.php | 2 -- src/Search/Provider/SQLProvider.php | 28 +++++++++++++++++++++++++++- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/src/ConsumableItem.php b/src/ConsumableItem.php index d98780f8168..0b4a5ebbc3b 100644 --- a/src/ConsumableItem.php +++ b/src/ConsumableItem.php @@ -221,7 +221,6 @@ public function rawSearchOptions() 'massiveaction' => false, 'joinparams' => [ 'jointype' => 'child', - 'condition' => ['NOT' => ['NEWTABLE.date_out' => null]] ] ]; @@ -236,7 +235,6 @@ public function rawSearchOptions() 'massiveaction' => false, 'joinparams' => [ 'jointype' => 'child', - 'condition' => ['NEWTABLE.date_out' => null] ] ]; diff --git a/src/Search/Provider/SQLProvider.php b/src/Search/Provider/SQLProvider.php index ca91b136854..86734a29a3d 100644 --- a/src/Search/Provider/SQLProvider.php +++ b/src/Search/Provider/SQLProvider.php @@ -38,6 +38,7 @@ use Change; use CommonDBTM; use CommonITILObject; +use ConsumableItem; use DBmysqlIterator; use Glpi\Application\View\TemplateRenderer; use Glpi\Asset\Asset_PeripheralAsset; @@ -461,7 +462,32 @@ public static function getSelectCriteria(string $itemtype, int $ID, bool $meta = return array_merge($SELECT, $ADDITONALFIELDS); } break; - + case "glpi_consumables.id": + if ( + $itemtype == ConsumableItem::class + && ($ID == 17 || $ID = 19) + && $opt_arrays[$ID]["datatype"] == 'count' + ) { + // forces the LEFT JOIN filter directly into the SELECT statement to optimise data loading + if ($ID == 19) { + $SELECT = [ + new QueryExpression( + QueryFunction::sum(new QueryExpression("CASE WHEN $table$addtable.date_out IS NULL THEN 1 ELSE 0 END")), + alias: $NAME + ) + ]; + return array_merge($SELECT, $ADDITONALFIELDS); + } else { + $SELECT = [ + new QueryExpression( + QueryFunction::sum(new QueryExpression("CASE WHEN $table$addtable.date_out IS NOT NULL THEN 1 ELSE 0 END")), + alias: $NAME + ) + ]; + return array_merge($SELECT, $ADDITONALFIELDS); + } + } + break; default: break; } From 43d8ab75c59f565bb4b9264a84f72240d13bbc47 Mon Sep 17 00:00:00 2001 From: stonebuzz Date: Mon, 6 May 2024 15:44:02 +0200 Subject: [PATCH 2/6] use 'computation' instead of 'workaround' --- src/ConsumableItem.php | 17 +++++++++++++---- src/Search/Provider/SQLProvider.php | 26 -------------------------- 2 files changed, 13 insertions(+), 30 deletions(-) diff --git a/src/ConsumableItem.php b/src/ConsumableItem.php index 0b4a5ebbc3b..d95cfd21ba3 100644 --- a/src/ConsumableItem.php +++ b/src/ConsumableItem.php @@ -154,6 +154,9 @@ public function defineTabs($options = []) public function rawSearchOptions() { + /** @var \DBmysql $DB */ + global $DB; + $tab = parent::rawSearchOptions(); $tab[] = [ @@ -215,13 +218,16 @@ public function rawSearchOptions() 'table' => 'glpi_consumables', 'field' => 'id', 'name' => __('Number of used consumables'), - 'datatype' => 'count', + 'datatype' => 'number', 'forcegroupby' => true, 'usehaving' => true, 'massiveaction' => false, 'joinparams' => [ 'jointype' => 'child', - ] + ], + 'computation' => new QueryExpression( + expression: QueryFunction::sum(new QueryExpression("CASE WHEN " . $DB::quoteName('TABLE.date_out') . " IS NULL THEN 1 ELSE 0 END")) + ) ]; $tab[] = [ @@ -229,13 +235,16 @@ public function rawSearchOptions() 'table' => 'glpi_consumables', 'field' => 'id', 'name' => __('Number of new consumables'), - 'datatype' => 'count', + 'datatype' => 'number', 'forcegroupby' => true, 'usehaving' => true, 'massiveaction' => false, 'joinparams' => [ 'jointype' => 'child', - ] + ], + 'computation' => new QueryExpression( + expression: QueryFunction::sum(new QueryExpression("CASE WHEN " . $DB::quoteName('TABLE.date_out') . " IS NOT NULL THEN 1 ELSE 0 END")) + ) ]; $tab = array_merge($tab, Location::rawSearchOptionsToAdd()); diff --git a/src/Search/Provider/SQLProvider.php b/src/Search/Provider/SQLProvider.php index 86734a29a3d..25cee4a2d0c 100644 --- a/src/Search/Provider/SQLProvider.php +++ b/src/Search/Provider/SQLProvider.php @@ -462,32 +462,6 @@ public static function getSelectCriteria(string $itemtype, int $ID, bool $meta = return array_merge($SELECT, $ADDITONALFIELDS); } break; - case "glpi_consumables.id": - if ( - $itemtype == ConsumableItem::class - && ($ID == 17 || $ID = 19) - && $opt_arrays[$ID]["datatype"] == 'count' - ) { - // forces the LEFT JOIN filter directly into the SELECT statement to optimise data loading - if ($ID == 19) { - $SELECT = [ - new QueryExpression( - QueryFunction::sum(new QueryExpression("CASE WHEN $table$addtable.date_out IS NULL THEN 1 ELSE 0 END")), - alias: $NAME - ) - ]; - return array_merge($SELECT, $ADDITONALFIELDS); - } else { - $SELECT = [ - new QueryExpression( - QueryFunction::sum(new QueryExpression("CASE WHEN $table$addtable.date_out IS NOT NULL THEN 1 ELSE 0 END")), - alias: $NAME - ) - ]; - return array_merge($SELECT, $ADDITONALFIELDS); - } - } - break; default: break; } From 4d71ba92a6af13c315bf5c80bf27429cac403e0f Mon Sep 17 00:00:00 2001 From: stonebuzz Date: Mon, 6 May 2024 15:44:33 +0200 Subject: [PATCH 3/6] clean use statement --- src/Search/Provider/SQLProvider.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Search/Provider/SQLProvider.php b/src/Search/Provider/SQLProvider.php index 25cee4a2d0c..ea5c3dfcca5 100644 --- a/src/Search/Provider/SQLProvider.php +++ b/src/Search/Provider/SQLProvider.php @@ -38,7 +38,6 @@ use Change; use CommonDBTM; use CommonITILObject; -use ConsumableItem; use DBmysqlIterator; use Glpi\Application\View\TemplateRenderer; use Glpi\Asset\Asset_PeripheralAsset; From ac662c65bf92e6eb61e257da33ed3dd15b7a83ad Mon Sep 17 00:00:00 2001 From: stonebuzz Date: Mon, 6 May 2024 15:45:50 +0200 Subject: [PATCH 4/6] revert --- src/Search/Provider/SQLProvider.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Search/Provider/SQLProvider.php b/src/Search/Provider/SQLProvider.php index ea5c3dfcca5..ca91b136854 100644 --- a/src/Search/Provider/SQLProvider.php +++ b/src/Search/Provider/SQLProvider.php @@ -461,6 +461,7 @@ public static function getSelectCriteria(string $itemtype, int $ID, bool $meta = return array_merge($SELECT, $ADDITONALFIELDS); } break; + default: break; } From eff1365fdcebaa5ee520228265fc549c84921312 Mon Sep 17 00:00:00 2001 From: stonebuzz Date: Mon, 6 May 2024 16:43:16 +0200 Subject: [PATCH 5/6] try to fix TU --- src/ConsumableItem.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ConsumableItem.php b/src/ConsumableItem.php index d95cfd21ba3..d414e712f84 100644 --- a/src/ConsumableItem.php +++ b/src/ConsumableItem.php @@ -216,7 +216,7 @@ public function rawSearchOptions() $tab[] = [ 'id' => '17', 'table' => 'glpi_consumables', - 'field' => 'id', + 'field' => 'date_out', 'name' => __('Number of used consumables'), 'datatype' => 'number', 'forcegroupby' => true, @@ -233,7 +233,7 @@ public function rawSearchOptions() $tab[] = [ 'id' => '19', 'table' => 'glpi_consumables', - 'field' => 'id', + 'field' => 'date_out', 'name' => __('Number of new consumables'), 'datatype' => 'number', 'forcegroupby' => true, From b5871c0cd1ef886f6619410fd01e8d5b928b8bbe Mon Sep 17 00:00:00 2001 From: stonebuzz Date: Tue, 7 May 2024 08:54:42 +0200 Subject: [PATCH 6/6] fix TU --- src/ConsumableItem.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/ConsumableItem.php b/src/ConsumableItem.php index d414e712f84..bd0e6d95002 100644 --- a/src/ConsumableItem.php +++ b/src/ConsumableItem.php @@ -222,6 +222,7 @@ public function rawSearchOptions() 'forcegroupby' => true, 'usehaving' => true, 'massiveaction' => false, + 'nometa' => true, 'joinparams' => [ 'jointype' => 'child', ], @@ -239,6 +240,7 @@ public function rawSearchOptions() 'forcegroupby' => true, 'usehaving' => true, 'massiveaction' => false, + 'nometa' => true, 'joinparams' => [ 'jointype' => 'child', ],