From 1bcd7533c4fa30d8690a45e2b2296ff7742937b3 Mon Sep 17 00:00:00 2001 From: Neill Magill Date: Thu, 20 Apr 2023 11:56:44 +0100 Subject: [PATCH] MDL-77965 calendar: Improve performance of data extract The UNION caused the query to be run in a way that is very inefficient on MySQL, separating the queries causes each of them to run in a much more efficient form. Any duplicated will be filtered out on the PHP side instead of in the database. On large Moodle sites this is preferable as the extract is likely to be performed on a server dedicated to running the Moodle cron and so there will be less of effect on resources that are used to serve end users. --- calendar/classes/privacy/provider.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/calendar/classes/privacy/provider.php b/calendar/classes/privacy/provider.php index 63f4b72ed04..d15b6b81823 100644 --- a/calendar/classes/privacy/provider.php +++ b/calendar/classes/privacy/provider.php @@ -118,9 +118,9 @@ class provider implements (e.courseid = ctx.instanceid AND e.eventtype = 'course' AND ctx.contextlevel = :coursecontext) OR (e.courseid = ctx.instanceid AND e.eventtype = 'group' AND ctx.contextlevel = :groupcontext) OR (e.userid = ctx.instanceid AND e.eventtype = 'user' AND ctx.contextlevel = :usercontext) - WHERE e.userid = :cuserid - UNION - SELECT ctx.id + WHERE e.userid = :cuserid"; + $contextlist->add_from_sql($sql, $params); + $sql = "SELECT ctx.id FROM {context} ctx JOIN {course_modules} cm ON cm.id = ctx.instanceid AND ctx.contextlevel = :modulecontext JOIN {modules} m ON m.id = cm.module