diff -urp drupal-6.x-dev/modules/comment/comment.module drupal-6.x-dev-modified/modules/comment/comment.module --- drupal-6.x-dev/modules/comment/comment.module 2007-05-17 15:57:19.000000000 -0500 +++ drupal-6.x-dev-modified/modules/comment/comment.module 2007-05-19 12:32:11.000000000 -0500 @@ -2017,6 +2017,7 @@ function _comment_update_node_statistics if ($count > 0) { $last_reply = db_fetch_object(db_query_range('SELECT cid, name, timestamp, uid FROM {comments} WHERE nid = %d AND status = %d ORDER BY cid DESC', $nid, COMMENT_PUBLISHED, 0, 1)); db_query("UPDATE {node_comment_statistics} SET comment_count = %d, last_comment_timestamp = %d, last_comment_name = '%s', last_comment_uid = %d WHERE nid = %d", $count, $last_reply->timestamp, $last_reply->uid ? '' : $last_reply->name, $last_reply->uid, $nid); + db_query('UPDATE {comments} SET last_comment_timestamp = %d WHERE nid = %d', $last_reply->timestamp, $nid); } // no comments diff -urp drupal-6.x-dev/modules/tracker/tracker.module drupal-6.x-dev-modified/modules/tracker/tracker.module --- drupal-6.x-dev/modules/tracker/tracker.module 2007-05-16 08:45:16.000000000 -0500 +++ drupal-6.x-dev-modified/modules/tracker/tracker.module 2007-05-19 12:51:16.000000000 -0500 @@ -81,28 +81,84 @@ function tracker_page($uid = 0) { // Add CSS drupal_add_css(drupal_get_path('module', 'tracker') .'/tracker.css', 'module', 'all', FALSE); - // TODO: These queries are very expensive, see http://drupal.org/node/105639 + // Only restrict to a uid if one is provided + $node_uid_condition = '1'; + $comment_uid_condition = '1'; if ($uid) { - $sql = 'SELECT DISTINCT(n.nid), n.title, n.type, n.changed, n.uid, u.name, GREATEST(n.changed, l.last_comment_timestamp) AS last_updated, l.comment_count FROM {node} n INNER JOIN {node_comment_statistics} l ON n.nid = l.nid INNER JOIN {users} u ON n.uid = u.uid LEFT JOIN {comments} c ON n.nid = c.nid AND (c.status = %d OR c.status IS NULL) WHERE n.status = 1 AND (n.uid = %d OR c.uid = %d) ORDER BY last_updated DESC'; - $sql = db_rewrite_sql($sql); - $sql_count = 'SELECT COUNT(DISTINCT(n.nid)) FROM {node} n LEFT JOIN {comments} c ON n.nid = c.nid AND (c.status = %d OR c.status IS NULL) WHERE n.status = 1 AND (n.uid = %d OR c.uid = %d)'; - $sql_count = db_rewrite_sql($sql_count); - $result = pager_query($sql, 25, 0, $sql_count, COMMENT_PUBLISHED, $uid, $uid); + $node_uid_condition = 'n.uid = %d'; + $comment_uid_condition = 'c.uid = %d'; } - else { - $sql = 'SELECT DISTINCT(n.nid), n.title, n.type, n.changed, n.uid, u.name, GREATEST(n.changed, l.last_comment_timestamp) AS last_updated, l.comment_count FROM {node} n INNER JOIN {users} u ON n.uid = u.uid INNER JOIN {node_comment_statistics} l ON n.nid = l.nid WHERE n.status = 1 ORDER BY last_updated DESC'; - $sql = db_rewrite_sql($sql); - $sql_count = 'SELECT COUNT(n.nid) FROM {node} n WHERE n.status = 1'; - $sql_count = db_rewrite_sql($sql_count); - $result = pager_query($sql, 25, 0, $sql_count); + + // Set a limit for the overall result set. Calculating the full, actual size requires creating the full result set, which is too expensive. + $sanity_limit = variable_get('tracker_limit', 1000); + + // Find the latest nodes created by the user + $nodes_sql = 'SELECT n.nid, n.title, n.type, n.changed, n.uid, n.changed AS last_updated FROM {node} n WHERE n.status = 1 AND ' . $node_uid_condition . ' ORDER BY last_updated DESC'; + $nodes_sql = db_rewrite_sql($nodes_sql); + // DISTINCT() causes the query to require a temporary table, and it doesn't matter much if we get duplicates here, anyway + // The process of combining the result sets will remove duplicates + $nodes_sql = str_replace('DISTINCT(n.nid)', 'n.nid', $nodes_sql); + $node_results = db_query_range($nodes_sql, $uid, 0, $sanity_limit); + + // Find the latest comments created by the user + $comments_sql = 'SELECT n.nid, n.title, n.type, n.changed, n.uid, c.last_comment_timestamp AS last_updated FROM {node} n INNER JOIN {comments} c ON n.nid = c.nid WHERE n.status = 1 AND c.status = %d AND ' . $comment_uid_condition . ' ORDER BY last_updated DESC'; + $comments_sql = db_rewrite_sql($comments_sql); + // The same problem with DISTINCT() occurs here + $comments_sql = str_replace('DISTINCT(n.nid)', 'n.nid', $comments_sql); + $comment_results = db_queryd($comments_sql, COMMENT_PUBLISHED, $uid, 0, $sanity_limit); + + // Combine the results while avoiding duplication + $results = array(); + $node = db_fetch_object($node_results); + $commented_node = db_fetch_object($comment_results); + while ($node || $commented_node) { + // Make sure we have the most recent node that's not currently listed + while (isset($results[$node->nid])) { + $node = db_fetch_object($node_results); + } + // Make sure we have the most recent commented node that's not currently listed + while (isset($results[$commented_node->nid])) { + $commented_node = db_fetch_object($comment_results); + } + + // In the case where both exist + if ($node && $commented_node) { + // ...and the node was updated last + if ($node->last_updated > $commented_node->last_updated) { + $results[$node->nid] = $node; + } + + // ...and the comment was updated last + else { + $results[$commented_node->nid] = $commented_node; + } + } + // Case where only $node is set + else if ($node) { + $results[$node->nid] = $node; + } + // Case where only $commented_node is set + else if ($commented_node) { + $results[$commented_node->nid] = $commented_node; + } } + + // Update the pager and get the current page's results + $results = _tracker_pager_update($results, 25); $rows = array(); - while ($node = db_fetch_object($result)) { + $names = array(); + foreach ($results as $node) { + // Load data here (instead of in a JOIN) to only load data for the current page + if (!isset($names[$node->uid])) { + $names[$node->uid] = db_result(db_query('SELECT name FROM {users} WHERE uid = %d', $node->uid)); + } + $node->name = $names[$node->uid]; + // Determine the number of comments: $comments = 0; if (module_exists('comment') && $node->comment_count) { - $comments = $node->comment_count; + $comments = db_result(db_query('SELECT comment_count FROM {node_comment_statistics} WHERE nid = %d', $node->nid)); if ($new = comment_num_new($node->nid)) { $comments .= '
';