Closed (fixed)
Project:
Drupal core
Version:
6.x-dev
Component:
comment.module
Priority:
Normal
Category:
Bug report
Assigned:
Unassigned
Reporter:
Created:
2 Jan 2008 at 03:52 UTC
Updated:
30 Jan 2008 at 14:01 UTC
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.
| Comment | File | Size | Author |
|---|---|---|---|
| pidindex.patch | 894 bytes | jvandyk |
Comments
Comment #1
moshe weitzman commentedComment #2
dries commentedThis looks like a no-brainer. Commited.
Comment #3
bjaspan commentedGentlemen, 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.
Comment #4
jvandyk commentedThanks, Barry. I am appropriately chastened.
Comment #5
moshe weitzman commentedwoops - bad flashback. sorry.
Comment #6
Anonymous (not verified) commentedAutomatically closed -- issue fixed for two weeks with no activity.