The pid field of the comment table is not indexed. Yet we have this query in comment.module:

/**
 * Get replies count for a comment.
 *
 * @param $pid
 *   The comment id.
 * @return
 *   The replies count.
 */
function comment_num_replies($pid) {
  static $cache;

  if (!isset($cache[$pid])) {
    $cache[$pid] = db_result(db_query('SELECT COUNT(cid) FROM {comments} WHERE pid = %d AND status = %d', $pid, COMMENT_PUBLISHED));
  }

  return $cache[$pid];
}

The cache helps, but the query does a full table scan. On my site, adding the index reduces the query from looking at, e.g., 15,000+ rows to just a few rows.

Patch to comment.install adds the index and provides an update.

CommentFileSizeAuthor
pidindex.patch894 bytesjvandyk

Comments

moshe weitzman’s picture

Status: Needs review » Reviewed & tested by the community
dries’s picture

Status: Reviewed & tested by the community » Fixed

This looks like a no-brainer. Commited.

bjaspan’s picture

Gentlemen, you just added mysql-specific ALTER TABLE SQL instead of using the Schema API for its intended purpose. Tsk tsk. See http://drupal.org/node/208938.

jvandyk’s picture

Thanks, Barry. I am appropriately chastened.

moshe weitzman’s picture

woops - bad flashback. sorry.

Anonymous’s picture

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for two weeks with no activity.