Skip to content

Commit

Permalink
Optimize(Search): use LEFT JOIN filter on SELECT statement
Browse files Browse the repository at this point in the history
  • Loading branch information
stonebuzz committed May 6, 2024
1 parent afbfebf commit 985862c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
2 changes: 0 additions & 2 deletions src/ConsumableItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,6 @@ public function rawSearchOptions()
'massiveaction' => false,
'joinparams' => [
'jointype' => 'child',
'condition' => ['NOT' => ['NEWTABLE.date_out' => null]]
]
];

Expand All @@ -236,7 +235,6 @@ public function rawSearchOptions()
'massiveaction' => false,
'joinparams' => [
'jointype' => 'child',
'condition' => ['NEWTABLE.date_out' => null]
]
];

Expand Down
28 changes: 27 additions & 1 deletion src/Search/Provider/SQLProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
use Change;
use CommonDBTM;
use CommonITILObject;
use ConsumableItem;
use DBmysqlIterator;
use Glpi\Application\View\TemplateRenderer;
use Glpi\Asset\Asset_PeripheralAsset;
Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit 985862c

Please sign in to comment.