From 6f9d8ca34f6887604b82f3f511df7b0ee892a0fd Mon Sep 17 00:00:00 2001 From: Neill Magill Date: Fri, 21 Apr 2023 13:58:38 +0100 Subject: [PATCH] MDL-77965 notes: Improve data export performance 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. --- notes/classes/privacy/provider.php | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/notes/classes/privacy/provider.php b/notes/classes/privacy/provider.php index 1a661beb261..6cbe8ce5714 100644 --- a/notes/classes/privacy/provider.php +++ b/notes/classes/privacy/provider.php @@ -95,9 +95,16 @@ class provider implements FROM {context} c INNER JOIN {post} p ON p.courseid = c.instanceid AND c.contextlevel = :contextcoursewrittenby WHERE p.module = 'notes' - AND p.usermodified = :usermodified - UNION - SELECT c.id + AND p.usermodified = :usermodified"; + + $params = [ + 'contextcoursewrittenby' => CONTEXT_COURSE, + 'usermodified' => $userid, + ]; + + $contextlist->add_from_sql($sql, $params); + + $sql = "SELECT c.id FROM {context} c INNER JOIN {post} p ON p.courseid = c.instanceid AND c.contextlevel = :contextcoursewrittenfor WHERE p.module = 'notes' @@ -105,8 +112,6 @@ class provider implements AND p.publishstate {$publishstatesql}"; $params = [ - 'contextcoursewrittenby' => CONTEXT_COURSE, - 'usermodified' => $userid, 'contextcoursewrittenfor' => CONTEXT_COURSE, 'userid' => $userid ];