MDL-65403 notifications: Create additional database indexes

This adds indexes on the timecreated and timeread columns, which greatly
speeds up the queries used by \core\task\messaging_cleanup_task on large
sites.
This commit is contained in:
Mark Johnson 2023-11-23 13:27:53 +00:00
parent 6374475dc3
commit 1fd2f91fa2
3 changed files with 27 additions and 2 deletions

View File

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" ?>
<XMLDB PATH="lib/db" VERSION="20231004" COMMENT="XMLDB file for core Moodle tables"
<XMLDB PATH="lib/db" VERSION="20231123" COMMENT="XMLDB file for core Moodle tables"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../../lib/xmldb/xmldb.xsd"
>
@ -754,6 +754,8 @@
</KEYS>
<INDEXES>
<INDEX NAME="useridfrom" UNIQUE="false" FIELDS="useridfrom" COMMENT="The useridfrom would normally be a foreign key to the users table. But it can also contain some extra negative values with special semantics (-10 or -20 for no-reply or support user). Therefore we can't make it a foreign key and the index must be created explicitly."/>
<INDEX NAME="timecreated" UNIQUE="false" FIELDS="timecreated" COMMENT="Index on timecreated to speed up deletion of expired notifications."/>
<INDEX NAME="timeread" UNIQUE="false" FIELDS="timeread" COMMENT="Index on timeread to speed up deletion of read notifications."/>
</INDEXES>
</TABLE>
<TABLE NAME="message_contacts" COMMENT="Maintains lists of contacts between users">

View File

@ -856,5 +856,28 @@ function xmldb_main_upgrade($oldversion) {
upgrade_main_savepoint(true, 2023110900.00);
}
if ($oldversion < 2023111600.01) {
// Define index timecreated (not unique) to be added to notifications.
$table = new xmldb_table('notifications');
$createdindex = new xmldb_index('timecreated', XMLDB_INDEX_NOTUNIQUE, ['timecreated']);
// Conditionally launch add index timecreated.
if (!$dbman->index_exists($table, $createdindex)) {
$dbman->add_index($table, $createdindex);
}
// Define index timeread (not unique) to be added to notifications.
$readindex = new xmldb_index('timeread', XMLDB_INDEX_NOTUNIQUE, ['timeread']);
// Conditionally launch add index timeread.
if (!$dbman->index_exists($table, $readindex)) {
$dbman->add_index($table, $readindex);
}
// Main savepoint reached.
upgrade_main_savepoint(true, 2023111600.01);
}
return true;
}

View File

@ -29,7 +29,7 @@
defined('MOODLE_INTERNAL') || die();
$version = 2023112400.00; // YYYYMMDD = weekly release date of this DEV branch.
$version = 2023112400.01; // YYYYMMDD = weekly release date of this DEV branch.
// RR = release increments - 00 in DEV branches.
// .XX = incremental changes.
$release = '4.4dev (Build: 20231124)'; // Human-friendly version name