Skip to content

Commit dc07785

Browse files
committed
other resource related optimisations
1 parent 166b4d8 commit dc07785

File tree

8 files changed

+117
-20
lines changed

8 files changed

+117
-20
lines changed

local/o365/classes/feature/cohortsync/main.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,13 @@ public function add_mapping(string $groupoid, int $cohortid): bool {
143143
public function get_mappings(): array {
144144
global $DB;
145145

146-
$mappings = $DB->get_records('local_o365_objects', ['type' => 'group', 'subtype' => 'cohort']);
146+
// Use recordset instead of get_records to reduce memory usage.
147+
$mappingrecordset = $DB->get_recordset('local_o365_objects', ['type' => 'group', 'subtype' => 'cohort']);
148+
$mappings = [];
149+
foreach ($mappingrecordset as $mapping) {
150+
$mappings[$mapping->id] = $mapping;
151+
}
152+
$mappingrecordset->close();
147153

148154
return $mappings;
149155
}
@@ -297,7 +303,13 @@ private function sync_cohort_members_by_cohort_id_and_microsoft_user_objects(int
297303
global $DB;
298304

299305
$microsoftuseroids = array_column($microsoftuserobjects, 'id');
300-
$currentmembers = $DB->get_records('cohort_members', ['cohortid' => $cohortid], '', 'userid');
306+
// Use recordset instead of get_records to reduce memory usage.
307+
$currentmembersrecordset = $DB->get_recordset('cohort_members', ['cohortid' => $cohortid], '', 'userid');
308+
$currentmembers = [];
309+
foreach ($currentmembersrecordset as $member) {
310+
$currentmembers[$member->userid] = $member;
311+
}
312+
$currentmembersrecordset->close();
301313
$connectedusers = $this->get_all_potential_user_details($microsoftuseroids, array_keys($currentmembers));
302314

303315
$microsoftuseroidsflipped = array_flip($microsoftuseroids);

local/o365/classes/feature/coursesync/main.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1072,11 +1072,13 @@ public function update_teams_cache(): bool {
10721072

10731073
// Build existing teams records cache.
10741074
$this->mtrace('Building existing teams cache records', 1);
1075-
$existingcacherecords = $DB->get_records('local_o365_teams_cache');
1075+
// Use recordset instead of get_records to reduce memory usage.
1076+
$existingcacherecordset = $DB->get_recordset('local_o365_teams_cache');
10761077
$existingcachebyoid = [];
1077-
foreach ($existingcacherecords as $existingcacherecord) {
1078+
foreach ($existingcacherecordset as $existingcacherecord) {
10781079
$existingcachebyoid[$existingcacherecord->objectid] = $existingcacherecord;
10791080
}
1081+
$existingcacherecordset->close();
10801082

10811083
// Compare, then create, update, or delete cache.
10821084
$this->mtrace('Updating teams cache records', 1);

local/o365/classes/feature/sds/task/sync.php

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,9 @@ public static function runsync(unified $apiclient): bool {
8686
static::mtrace('Only basic SDS profile data required', 2);
8787
}
8888

89-
$oidcusers = $DB->get_records('user', ['auth' => 'oidc', 'deleted' => 0]);
90-
foreach ($oidcusers as $userid => $oidcuser) {
89+
// Use recordset instead of get_records to reduce memory usage.
90+
$oidcusersrecordset = $DB->get_recordset('user', ['auth' => 'oidc', 'deleted' => 0]);
91+
foreach ($oidcusersrecordset as $userid => $oidcuser) {
9192
$completeuser = get_complete_user_data('id', $userid);
9293
if ($completeuser) {
9394
static::mtrace('Processing user ' . $oidcuser->username, 3);
@@ -178,6 +179,7 @@ public static function runsync(unified $apiclient): bool {
178179
}
179180
}
180181
}
182+
$oidcusersrecordset->close();
181183
} else {
182184
static::mtrace('SDS field mapping disabled', 2);
183185
}
@@ -540,8 +542,20 @@ public static function clean_up_sds_sync_records() {
540542
static::mtrace('Running course sync cleanup', 1);
541543

542544
// Get existing synced records.
543-
$syncedsdsschools = $DB->get_records('local_o365_objects', ['type' => 'sdsschool']);
544-
$syncedsdssections = $DB->get_records('local_o365_objects', ['type' => 'sdssection']);
545+
// Use recordset instead of get_records to reduce memory usage.
546+
$syncedsdsschoolsrecordset = $DB->get_recordset('local_o365_objects', ['type' => 'sdsschool']);
547+
$syncedsdsschools = [];
548+
foreach ($syncedsdsschoolsrecordset as $school) {
549+
$syncedsdsschools[$school->id] = $school;
550+
}
551+
$syncedsdsschoolsrecordset->close();
552+
553+
$syncedsdssectionsrecordset = $DB->get_recordset('local_o365_objects', ['type' => 'sdssection']);
554+
$syncedsdssections = [];
555+
foreach ($syncedsdssectionsrecordset as $section) {
556+
$syncedsdssections[$section->id] = $section;
557+
}
558+
$syncedsdssectionsrecordset->close();
545559

546560
// Clean up schools.
547561
$enabledschools = get_config('local_o365', 'sdsschools');

local/o365/classes/observers.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -477,12 +477,14 @@ public static function handle_enrol_instance_updated(enrol_instance_updated $eve
477477
}
478478
}
479479

480-
$userenrolments = $DB->get_records('user_enrolments', ['enrolid' => $event->objectid]);
480+
// Use recordset instead of get_records to reduce memory usage.
481+
$userenrolmentsrecordset = $DB->get_recordset('user_enrolments', ['enrolid' => $event->objectid]);
481482

482-
foreach ($userenrolments as $userenrolment) {
483+
foreach ($userenrolmentsrecordset as $userenrolment) {
483484
\local_o365\feature\coursesync\utils::sync_user_role_in_course_group($userenrolment->userid, $courseid, 0,
484485
$coursegroupobjectrecordid, true);
485486
}
487+
$userenrolmentsrecordset->close();
486488

487489
return true;
488490
}

local/o365/classes/utils.php

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -562,10 +562,12 @@ public static function get_connected_users(): array {
562562

563563
$connectedusers = [];
564564

565-
$userobjectrecords = $DB->get_records('local_o365_objects', ['type' => 'user']);
566-
foreach ($userobjectrecords as $userobjectrecord) {
565+
// Use recordset instead of get_records to reduce memory usage.
566+
$userobjectrecordset = $DB->get_recordset('local_o365_objects', ['type' => 'user']);
567+
foreach ($userobjectrecordset as $userobjectrecord) {
567568
$connectedusers[$userobjectrecord->moodleid] = $userobjectrecord->objectid;
568569
}
570+
$userobjectrecordset->close();
569571

570572
return $connectedusers;
571573
}
@@ -662,17 +664,23 @@ public static function clean_up_not_found_groups(int $baselevel = 1): void {
662664
static::mtrace('Clean up non-existing groups from database', $baselevel);
663665

664666
$cutofftime = strtotime('-5 minutes');
665-
$sql = "SELECT *
667+
$sql = "SELECT objectid
666668
FROM {local_o365_groups_cache}
667669
WHERE not_found_since != 0
668670
AND not_found_since < :cutofftime";
669671
$records = $DB->get_records_sql($sql, ['cutofftime' => $cutofftime]);
670672

671-
foreach ($records as $record) {
672-
$DB->delete_records('local_o365_groups_cache', ['objectid' => $record->objectid]);
673-
$DB->delete_records('local_o365_objects', ['objectid' => $record->objectid]);
674-
$DB->delete_records('local_o365_teams_cache', ['objectid' => $record->objectid]);
675-
static::mtrace('Deleted non-existing group ' . $record->objectid . ' from groups cache.', $baselevel + 1);
673+
if (!empty($records)) {
674+
$objectids = array_keys($records);
675+
676+
// Use bulk delete with IN clause for better performance.
677+
[$insql, $inparams] = $DB->get_in_or_equal($objectids, SQL_PARAMS_NAMED);
678+
$DB->delete_records_select('local_o365_groups_cache', "objectid $insql", $inparams);
679+
$DB->delete_records_select('local_o365_objects', "objectid $insql", $inparams);
680+
$DB->delete_records_select('local_o365_teams_cache', "objectid $insql", $inparams);
681+
682+
$count = count($objectids);
683+
static::mtrace("Deleted $count non-existing groups from database.", $baselevel + 1);
676684
}
677685

678686
static::mtrace('Finished cleaning up non-existing groups from database.', $baselevel);

local/o365/db/install.xml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@
103103
<INDEXES>
104104
<INDEX NAME="moodleid" UNIQUE="false" FIELDS="moodleid"/>
105105
<INDEX NAME="objectid" UNIQUE="false" FIELDS="objectid"/>
106+
<INDEX NAME="type_subtype" UNIQUE="false" FIELDS="type, subtype"/>
106107
</INDEXES>
107108
</TABLE>
108109
<TABLE NAME="local_o365_appassign" COMMENT="Stores information on which accounts are assigned to the app.">
@@ -164,6 +165,9 @@
164165
<KEYS>
165166
<KEY NAME="primary" TYPE="primary" FIELDS="id"/>
166167
</KEYS>
168+
<INDEXES>
169+
<INDEX NAME="objectid" UNIQUE="false" FIELDS="objectid"/>
170+
</INDEXES>
167171
</TABLE>
168172
<TABLE NAME="local_o365_groups_cache" COMMENT="Stores Groups cache. This is primarily used for cohort sync, but is used to clean up groups in local_o365_objects and teams caches too">
169173
<FIELDS>
@@ -176,6 +180,10 @@
176180
<KEYS>
177181
<KEY NAME="primary" TYPE="primary" FIELDS="id"/>
178182
</KEYS>
183+
<INDEXES>
184+
<INDEX NAME="objectid" UNIQUE="false" FIELDS="objectid"/>
185+
<INDEX NAME="not_found_since" UNIQUE="false" FIELDS="not_found_since"/>
186+
</INDEXES>
179187
</TABLE>
180188
<TABLE NAME="local_o365_course_request" COMMENT="Table to store course requests for o365">
181189
<FIELDS>
@@ -190,6 +198,9 @@
190198
<KEYS>
191199
<KEY NAME="primary" TYPE="primary" FIELDS="id"/>
192200
</KEYS>
201+
<INDEXES>
202+
<INDEX NAME="requeststatus" UNIQUE="false" FIELDS="requeststatus"/>
203+
</INDEXES>
193204
</TABLE>
194205
</TABLES>
195206
</XMLDB>

local/o365/db/upgrade.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1323,5 +1323,53 @@ function xmldb_local_o365_upgrade($oldversion) {
13231323
upgrade_plugin_savepoint(true, 2024100711, 'local', 'o365');
13241324
}
13251325

1326+
if ($oldversion < 2025040806) {
1327+
// Add index to local_o365_teams_cache.objectid for better query performance.
1328+
$table = new xmldb_table('local_o365_teams_cache');
1329+
$index = new xmldb_index('objectid', XMLDB_INDEX_NOTUNIQUE, ['objectid']);
1330+
1331+
// Conditionally launch add index objectid.
1332+
if (!$dbman->index_exists($table, $index)) {
1333+
$dbman->add_index($table, $index);
1334+
}
1335+
1336+
// Add indexes to local_o365_groups_cache for better query performance.
1337+
$table = new xmldb_table('local_o365_groups_cache');
1338+
$index = new xmldb_index('objectid', XMLDB_INDEX_NOTUNIQUE, ['objectid']);
1339+
1340+
// Conditionally launch add index objectid.
1341+
if (!$dbman->index_exists($table, $index)) {
1342+
$dbman->add_index($table, $index);
1343+
}
1344+
1345+
$index = new xmldb_index('not_found_since', XMLDB_INDEX_NOTUNIQUE, ['not_found_since']);
1346+
1347+
// Conditionally launch add index not_found_since.
1348+
if (!$dbman->index_exists($table, $index)) {
1349+
$dbman->add_index($table, $index);
1350+
}
1351+
1352+
// Add composite index to local_o365_objects.type and subtype for better query performance.
1353+
$table = new xmldb_table('local_o365_objects');
1354+
$index = new xmldb_index('type_subtype', XMLDB_INDEX_NOTUNIQUE, ['type', 'subtype']);
1355+
1356+
// Conditionally launch add index type_subtype.
1357+
if (!$dbman->index_exists($table, $index)) {
1358+
$dbman->add_index($table, $index);
1359+
}
1360+
1361+
// Add index to local_o365_course_request.requeststatus for better query performance.
1362+
$table = new xmldb_table('local_o365_course_request');
1363+
$index = new xmldb_index('requeststatus', XMLDB_INDEX_NOTUNIQUE, ['requeststatus']);
1364+
1365+
// Conditionally launch add index requeststatus.
1366+
if (!$dbman->index_exists($table, $index)) {
1367+
$dbman->add_index($table, $index);
1368+
}
1369+
1370+
// O365 savepoint reached.
1371+
upgrade_plugin_savepoint(true, 2025040806, 'local', 'o365');
1372+
}
1373+
13261374
return true;
13271375
}

local/o365/version.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@
2626

2727
defined('MOODLE_INTERNAL') || die();
2828

29-
$plugin->version = 2025040805;
29+
$plugin->version = 2025040806;
3030
$plugin->requires = 2025040800;
31-
$plugin->release = '5.0.1';
31+
$plugin->release = '5.0.2';
3232
$plugin->component = 'local_o365';
3333
$plugin->maturity = MATURITY_STABLE;
3434
$plugin->dependencies = [

0 commit comments

Comments
 (0)