From 36a725ff1ff4ce1df89b8787ef965011700d7c10 Mon Sep 17 00:00:00 2001 From: Dan Poltawski Date: Tue, 10 Feb 2009 20:42:19 +0000 Subject: [PATCH] performance/mymoodle MDL-18241 - Assigments were very expensive Reworked to take queries out of loops and let it scale. Unfortunately I don't think we can get the get_users_by_capability() call out of the loop on assignments. Real query numbers before/after: Teacher: 12,509 / 314 Student: 127 / 72 Admin: 718 / 133 --- mod/assignment/lib.php | 51 ++++++++++++++++++++++++++++++++++++----------- 1 files changed, 39 insertions(+), 12 deletions(-) diff --git a/mod/assignment/lib.php b/mod/assignment/lib.php index e9c062c..7b011f7 100644 --- a/mod/assignment/lib.php +++ b/mod/assignment/lib.php @@ -2847,6 +2847,8 @@ function assignment_print_overview($courses, &$htmlarray) { return; } + $assignmentids = array(); + // Do assignment_base::isopen() here without loading the whole thing for speed foreach ($assignments as $key => $assignment) { $time = time(); @@ -2859,9 +2861,16 @@ function assignment_print_overview($courses, &$htmlarray) { } if (empty($isopen) || empty($assignment->timedue)) { unset($assignments[$key]); + }else{ + $assignmentids[] = $assignment->id; } } + if(empty($assignmentids)){ + // no assigments to look at - we're done + return true; + } + $strduedate = get_string('duedate', 'assignment'); $strduedateno = get_string('duedateno', 'assignment'); $strgraded = get_string('graded', 'assignment'); @@ -2871,6 +2880,29 @@ function assignment_print_overview($courses, &$htmlarray) { $strassignment = get_string('modulename', 'assignment'); $strreviewed = get_string('reviewed','assignment'); + + // NOTE: we do all possible database work here *outside* of the loop to ensure this scales + + // build up and array of unmarked submissions indexed by assigment id/ userid + // for use where the user has grading rights on assigment + $rs = get_recordset_sql("SELECT id, assignment, userid + FROM {$CFG->prefix}assignment_submissions + WHERE teacher = 0 AND timemarked = 0 + AND assignment IN (". implode(',', $assignmentids).")"); + + $unmarkedsubmissions = array(); + while ($ra = rs_fetch_next_record($rs)) { + $unmarkedsubmissions[$ra->assignment][$ra->userid] = $ra->id; + } + rs_close($rs); + + + // get all user submissions, indexed by assigment id + $mysubmissions = get_records_sql("SELECT assignment, timemarked, teacher, grade + FROM {$CFG->prefix}assignment_submissions + WHERE userid = {$USER->id} AND + assignment IN (".implode(',', $assignmentids).")"); + foreach ($assignments as $assignment) { $str = '