MDL-66253 calendar: Allow event retrival queries to use indexes

Before this change in most cases the queries generated by the API
would not be able to use an index as the subquery would use
conditions that coulde not be satisfied by a signle index.

By changing it to use UNIONs the database will be able to use an
appropriate index for each query in most cases.
This commit is contained in:
Neill Magill
2020-01-10 08:27:10 +00:00
parent 77d1c41502
commit 28c30ffece
@@ -278,7 +278,12 @@ class raw_event_retrieval_strategy implements raw_event_retrieval_strategy_inter
// Build the WHERE condition for the sub-query.
if (!empty($subqueryconditions)) {
$subquerywhere = 'WHERE ' . implode(" OR ", $subqueryconditions);
$unionstartquery = "SELECT modulename, instance, eventtype, priority
FROM {event} ev
WHERE ";
$subqueryunion = $unionstartquery . implode(" UNION $unionstartquery ", $subqueryconditions);
} else {
$subqueryunion = '{event}';
}
// Merge subquery parameters to the parameters of the main query.
@@ -291,8 +296,7 @@ class raw_event_retrieval_strategy implements raw_event_retrieval_strategy_inter
ev.instance,
ev.eventtype,
MIN(ev.priority) as priority
FROM {event} ev
$subquerywhere
FROM ($subqueryunion) ev
GROUP BY ev.modulename, ev.instance, ev.eventtype";
// Build the main query.