Skip to content

Commit

Permalink
Merged from MOODLE_15_STABLE - forums/trackreads: Postgres-optimized …
Browse files Browse the repository at this point in the history
…query - from many minutes to a few milliseconds
  • Loading branch information
martinlanghoff committed Sep 2, 2005
1 parent fc3aaa4 commit a85168b
Showing 1 changed file with 25 additions and 5 deletions.
30 changes: 25 additions & 5 deletions mod/forum/lib.php
Expand Up @@ -3371,11 +3371,31 @@ function forum_tp_count_forum_read_records($userid, $forumid, $groupid=false) {
$groupsel = ' AND (d.groupid = '.$groupid.' OR d.groupid = -1)';
}

$sql = 'SELECT COUNT(DISTINCT p.id) '.
'FROM '.$CFG->prefix.'forum_posts p,'.$CFG->prefix.'forum_read r,'.$CFG->prefix.'forum_discussions d '.
'WHERE d.forum = '.$forumid.$groupsel.' AND p.discussion = d.id AND '.
'((p.id = r.postid AND r.userid = '.$userid.') OR p.modified < '.$cutoffdate.' ) ';

error_log("forumcount");

if ($CFG->dbtype === 'postgres7') {
// this query takes 20ms, vs several minutes for the one below
$sql = " SELECT COUNT (DISTINCT u.id ) "
. " FROM ( "
. " SELECT p.id "
. " FROM {$CFG->prefix}forum_posts p "
. " JOIN {$CFG->prefix}forum_discussions d ON p.discussion = d.id "
. " JOIN {$CFG->prefix}forum_read r ON p.id = r.postid"
. " WHERE d.forum = $forumid $groupsel "
. " AND r.userid= $userid"
. " UNION"
. " SELECT p.id"
. " FROM {$CFG->prefix}forum_posts p "
. " JOIN {$CFG->prefix}forum_discussions d ON p.discussion = d.id "
. " WHERE d.forum = $forumid $groupsel "
. " AND p.modified < $cutoffdate"
. ") u";
} else {
$sql = 'SELECT COUNT(DISTINCT p.id) '.
'FROM '.$CFG->prefix.'forum_posts p,'.$CFG->prefix.'forum_read r,'.$CFG->prefix.'forum_discussions d '.
'WHERE d.forum = '.$forumid.$groupsel.' AND p.discussion = d.id AND '.
'((p.id = r.postid AND r.userid = '.$userid.') OR p.modified < '.$cutoffdate.' ) ';
}
return (count_records_sql($sql));
}

Expand Down

0 comments on commit a85168b

Please sign in to comment.