RFC-30643 - Added fifth table to speed up enrolment look ups.

This commit is contained in:
Tyler Bannister 2012-09-04 10:11:16 -04:00
parent c0f00c5fba
commit 63e0fe85b5
2 changed files with 96 additions and 63 deletions

View File

@ -2882,7 +2882,7 @@
<KEY NAME="fk_raterid" TYPE="foreign" FIELDS="raterid" REFTABLE="user" REFFIELDS="id" PREVIOUS="fk_definitionid"/>
</KEYS>
</TABLE>
<TABLE NAME="event_subscriptions" COMMENT="Tracks subscriptions to remote calendars." PREVIOUS="grading_instances" NEXT="temp_log_template">
<TABLE NAME="event_subscriptions" COMMENT="Tracks subscriptions to remote calendars." PREVIOUS="grading_instances" NEXT="temp_enroled_template">
<FIELDS>
<FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="true" NEXT="url"/>
<FIELD NAME="url" TYPE="char" LENGTH="255" NOTNULL="true" SEQUENCE="false" PREVIOUS="id" NEXT="courseid"/>
@ -2897,7 +2897,23 @@
<KEY NAME="primary" TYPE="primary" FIELDS="id"/>
</KEYS>
</TABLE>
<TABLE NAME="temp_log_template" COMMENT="Temporary storage for daily logs" PREVIOUS="event_subscriptions">
<TABLE NAME="temp_enroled_template" COMMENT="Temporary storage for course enrolments" PREVIOUS="event_subscriptions" NEXT="temp_log_template">
<FIELDS>
<FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="true" NEXT="userid"/>
<FIELD NAME="userid" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="id" NEXT="courseid"/>
<FIELD NAME="courseid" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="userid" NEXT="roleid"/>
<FIELD NAME="roleid" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="false" PREVIOUS="courseid"/>
</FIELDS>
<KEYS>
<KEY NAME="primary" TYPE="primary" FIELDS="id"/>
</KEYS>
<INDEXES>
<INDEX NAME="userid" UNIQUE="false" FIELDS="userid" NEXT="courseid"/>
<INDEX NAME="courseid" UNIQUE="false" FIELDS="courseid" PREVIOUS="userid" NEXT="roleid"/>
<INDEX NAME="roleid" UNIQUE="false" FIELDS="roleid" PREVIOUS="courseid"/>
</INDEXES>
</TABLE>
<TABLE NAME="temp_log_template" COMMENT="Temporary storage for daily logs" PREVIOUS="temp_enroled_template">
<FIELDS>
<FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="true" NEXT="userid"/>
<FIELD NAME="userid" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="id" NEXT="course"/>

View File

@ -103,7 +103,7 @@ function stats_progress($ident) {
* @param string $sql The query to run
* @return boolean success
*/
function stats_run_query($sql, $parameters) {
function stats_run_query($sql, $parameters = array()) {
global $DB;
try {
@ -190,7 +190,13 @@ function stats_cron_daily($maxdays=1) {
$days = 1;
$failed = true;
}
mtrace("Temporary tables created");
mtrace('Temporary tables created');
if(!stats_temp_table_setup()) {
$days = 1;
$failed = true;
}
mtrace('Enrolments calculated');
$totalactiveusers = $DB->count_records('user', array('deleted' => '0'));
@ -281,15 +287,12 @@ function stats_cron_daily($maxdays=1) {
$sql = "INSERT INTO {temp_stats_daily} (stattype, timeend, courseid, roleid, stat1, stat2)
SELECT 'enrolments' AS stattype, $nextmidnight AS timeend, e.courseid, ra.roleid,
COUNT(DISTINCT ue.userid) AS stat1, 0 AS stat2
FROM {role_assignments} ra
JOIN {context} c ON (c.id = ra.contextid AND c.contextlevel = :courselevel)
JOIN {enrol} e ON e.courseid = c.instanceid
JOIN {user_enrolments} ue ON (ue.enrolid = e.id AND ue.userid = ra.userid)
SELECT 'enrolments' as stattype, $nextmidnight as timeend, courseid, roleid,
COUNT(DISTINCT userid) as stat1, 0 as stat2
FROM {temp_enroled}
GROUP BY courseid, roleid";
if (!stats_run_query($sql, array('courselevel'=>CONTEXT_COURSE))) {
if (!stats_run_query($sql)) {
$failed = true;
break;
}
@ -300,28 +303,23 @@ function stats_cron_daily($maxdays=1) {
$sql = "UPDATE {temp_stats_daily}
SET stat2 = (
SELECT COUNT(DISTINCT ra.userid)
FROM {role_assignments} ra
JOIN {context} c ON (c.id = ra.contextid AND c.contextlevel = :courselevel)
JOIN {enrol} e ON e.courseid = c.instanceid
JOIN {user_enrolments} ue ON (ue.enrolid = e.id AND ue.userid = ra.userid)
WHERE ra.roleid = {temp_stats_daily}.roleid
AND e.courseid = {temp_stats_daily}.courseid
SELECT COUNT(DISTINCT userid)
FROM {temp_enroled} te
WHERE roleid = {temp_stats_daily}.roleid
AND courseid = {temp_stats_daily}.courseid
AND EXISTS (
SELECT 'x'
FROM {temp_log1} l
WHERE l.course = {temp_stats_daily}.courseid
AND l.userid = ra.userid
AND l.userid = te.userid
)
)
)
WHERE {temp_stats_daily}.stattype = 'enrolments'
AND {temp_stats_daily}.timeend = $nextmidnight
AND {temp_stats_daily}.courseid IN (
WHERE {stats_daily}.stattype = 'enrolments'
AND {stats_daily}.timeend = $nextmidnight
AND {stats_daily}.courseid IN (
SELECT DISTINCT course FROM {temp_log2}
)";
SELECT DISTINCT course FROM {temp_log2})";
if ($logspresent && !stats_run_query($sql, array('courselevel'=>CONTEXT_COURSE))) {
$failed = true;
@ -332,10 +330,9 @@ function stats_cron_daily($maxdays=1) {
// Now get course total enrolments (roleid==0) - except frontpage
$sql = "INSERT INTO {temp_stats_daily} (stattype, timeend, courseid, roleid, stat1, stat2)
SELECT 'enrolments', $nextmidnight AS timeend, e.courseid AS courseid, 0 AS roleid,
SELECT 'enrolments', $nextmidnight AS timeend, te.courseid AS courseid, 0 AS roleid,
COUNT(DISTINCT userid) AS stat1, 0 AS stat2
FROM {enrol} e
JOIN {user_enrolments} ue ON ue.enrolid = e.id
FROM {temp_enroled} te
GROUP BY courseid
HAVING COUNT(DISTINCT userid) > 0";
@ -349,27 +346,26 @@ function stats_cron_daily($maxdays=1) {
$sql = "UPDATE {temp_stats_daily}
SET stat2 = (
SELECT COUNT(DISTINCT ue.userid)
FROM {enrol} e
JOIN {user_enrolments} ue ON ue.enrolid = e.id
WHERE e.courseid = {temp_stats_daily}.courseid
AND EXISTS (
SELECT COUNT(DISTINCT te.userid)
FROM {temp_enroled} te
WHERE te.courseid = {temp_stats_daily}.courseid
AND EXISTS (
SELECT 'x'
FROM {temp_log1} l
WHERE l.course = {temp_stats_daily}.courseid
AND l.userid = ue.userid
)
)
SELECT 'x'
FROM {temp_log1} l
WHERE l.course = {temp_stats_daily}.courseid
AND l.userid = te.userid
)
)
WHERE {temp_stats_daily}.stattype = 'enrolments'
AND {temp_stats_daily}.timeend = $nextmidnight
AND {temp_stats_daily}.roleid = 0
AND {temp_stats_daily}.courseid IN (
SELECT l.course
FROM {temp_log2} l
WHERE l.course <> ".SITEID.")";
SELECT l.course
FROM {temp_log2} l
WHERE l.course <> ".SITEID.")";
if ($logspresent && !stats_run_query($sql, array())) {
$failed = true;
@ -469,14 +465,12 @@ function stats_cron_daily($maxdays=1) {
SELECT pl.courseid, pl.roleid, sud.statsreads, sud.statswrites
FROM {temp_stats_user_daily} sud, (
SELECT DISTINCT ra.userid, ra.roleid, e.courseid
FROM {role_assignments} ra
JOIN {context} c ON (c.id = ra.contextid AND c.contextlevel = :courselevel)
JOIN {enrol} e ON e.courseid = c.instanceid
JOIN {user_enrolments} ue ON (ue.enrolid = e.id AND ue.userid = ra.userid)
WHERE ra.roleid <> $guestrole
AND ra.userid <> $guest
) pl
SELECT DISTINCT te.userid, te.roleid, te.courseid
FROM {temp_enroled} te
WHERE te.roleid <> $guestrole
AND te.userid <> $guest
) pl
WHERE sud.userid = pl.userid
AND sud.courseid = pl.courseid
AND sud.timeend = $nextmidnight
@ -508,10 +502,9 @@ function stats_cron_daily($maxdays=1) {
AND sud.stattype='activity'
AND (sud.userid = $guest OR sud.userid NOT IN (
SELECT ue.userid
FROM {user_enrolments} ue
JOIN {enrol} e ON ue.enrolid = e.id
WHERE e.courseid = sud.courseid
SELECT userid
FROM {temp_enroled} te
WHERE te.courseid = sud.courseid
))
) inline_view
@ -1444,7 +1437,7 @@ function stats_get_report_options($courseid,$mode) {
$reportoptions[STATS_REPORT_PARTICIPATORY_COURSES] = get_string('statsreport'.STATS_REPORT_PARTICIPATORY_COURSES);
$reportoptions[STATS_REPORT_PARTICIPATORY_COURSES_RW] = get_string('statsreport'.STATS_REPORT_PARTICIPATORY_COURSES_RW);
}
break;
break;
}
return $reportoptions;
@ -1562,9 +1555,10 @@ function stats_temp_table_create() {
// Allows for the additional xml files to be used (if necessary)
$files = array(
$xmlfile => array(
'stats_daily' => array('temp_stats_daily'),
'stats_user_daily' => array('temp_stats_user_daily'),
'temp_log_template' => array('temp_log1', 'temp_log2'),
'stats_daily' => array('temp_stats_daily'),
'stats_user_daily' => array('temp_stats_user_daily'),
'temp_enroled_template' => array('temp_enroled'),
'temp_log_template' => array('temp_log1', 'temp_log2'),
),
);
@ -1619,7 +1613,7 @@ function stats_temp_table_drop() {
$dbman = $DB->get_manager();
$tables = array('temp_log1', 'temp_log2', 'temp_stats_daily', 'temp_stats_user_daily');
$tables = array('temp_log1', 'temp_log2', 'temp_stats_daily', 'temp_stats_user_daily', 'temp_enroled');
foreach ($tables as $name) {
@ -1638,6 +1632,31 @@ function stats_temp_table_drop() {
/**
* Fills the temporary stats tables with new data
*
* This function is meant to be called once at the start of stats generation
*
* @param timestart timestamp of the start time of logs view
* @param timeend timestamp of the end time of logs view
* @returns boolen success (true) or failure(false)
*/
function stats_temp_table_setup() {
global $DB;
$sql = "INSERT INTO {temp_enroled} (userid, courseid, roleid)
SELECT ue.userid, e.courseid, ra.roleid
FROM {role_assignments} ra
JOIN {context} c ON (c.id = ra.contextid AND c.contextlevel = :courselevel)
JOIN {enrol} e ON e.courseid = c.instanceid
JOIN {user_enrolments} ue ON (ue.enrolid = e.id AND ue.userid = ra.userid)";
return stats_run_query($sql, array('courselevel' => CONTEXT_COURSE));
}
/**
* Fills the temporary stats tables with new data
*
* This function is meant to be called to get a new day of data
*
* @param timestart timestamp of the start time of logs view
* @param timeend timestamp of the end time of logs view
* @returns boolen success (true) or failure(false)
@ -1683,9 +1702,7 @@ function stats_temp_table_clean() {
FROM {temp_stats_user_daily}';
foreach ($sql as $id => $query) {
try {
$DB->execute($query);
} catch (Exception $e) {
if (! stats_run_query($query)) {
mtrace("Error during table cleanup!");
return false;
}