Index: modules/comment/comment.module
===================================================================
RCS file: /cvs/drupal/drupal/modules/comment/comment.module,v
retrieving revision 1.498
diff -u -p -r1.498 comment.module
--- modules/comment/comment.module	21 Nov 2006 20:14:17 -0000	1.498
+++ modules/comment/comment.module	22 Nov 2006 12:55:17 -0000
@@ -172,8 +172,22 @@ function comment_block($op = 'list', $de
   }
 }
 
+/**
+ * Returns a formatted list of recent comments to be displayed in the comment block.
+ *
+ * @ingroup themeable
+ */
 function theme_comment_block() {
-  $result = db_query_range(db_rewrite_sql('SELECT c.nid, c.subject, c.cid, c.timestamp FROM {comments} c INNER JOIN {node} n ON n.nid = c.nid WHERE n.status = 1 AND c.status = %d ORDER BY c.timestamp DESC', 'c'), COMMENT_PUBLISHED, 0, 10);
+  // Select the 10 nodes (visible to the current user) with the most recent comments.  This is efficient due to the index on last_comment_timestamp.
+  $result = db_query_range(db_rewrite_sql("SELECT n.nid from {node_comment_statistics} n WHERE n.comment_count > 0 ORDER BY n.last_comment_timestamp DESC"), 0, 10);
+
+  $recent_nids = array();
+
+  while ($cnode = db_fetch_object($result)) {
+    $recent_nids[] = $cnode->nid;
+  }
+  // From among the comments on the 10 nodes selected in the first query, find the 10 most recent comments.
+  $result = db_query_range('SELECT c.nid, c.subject, c.cid, c.timestamp FROM {comments} c INNER JOIN {node} n ON n.nid = c.nid WHERE c.nid IN ('. implode(',', $recent_nids) .') AND n.status = 1 AND c.status = %d ORDER BY c.timestamp DESC', COMMENT_PUBLISHED, 0, 10);
   $items = array();
   while ($comment = db_fetch_object($result)) {
     $items[] = l($comment->subject, 'node/'. $comment->nid, NULL, NULL, 'comment-'. $comment->cid) .'<br />'. t('@time ago', array('@time' => format_interval(time() - $comment->timestamp)));
