My site has been experiencing sporadic spikes in mysql connections, and in the course of investigating, we've discovered a slow query in CommentRSS that is taking typically 45 seconds to execute against a comments table of about 750k rows. A consultant I'm working with has rewritten the query (line 168, commentrss.pages.inc), from this:
$SQL = 'SELECT '. $nidselector .', c.cid, c.subject, c.comment, c.timestamp, c.uid, c.name, c.format, u.name username, n.title FROM {node} n '. $joins .' INNER JOIN {comments} c ON c.nid = n.nid INNER JOIN {users} u ON c.uid = u.uid WHERE '. $where .' n.status = 1 AND c.status = %d ORDER BY c.timestamp DESC';
to this:
$SQL = 'SELECT STRAIGHT_JOIN '. $nidselector .', c.cid, c.subject, c.comment, c.timestamp, c.uid, c.name, c.format, u.name username, n.title FROM {comments} c '. $joins .' INNER JOIN {node} n ON c.nid = n.nid INNER JOIN {users} u ON c.uid = u.uid WHERE '. $where .' n.status = 1 AND c.status = %d ORDER BY c.timestamp DESC';
and then added an index for timestamp and uid. It's reduced the query time to 6 seconds. There may be a better way, but it's improved things on the site considerably for now. Thought I'd report it in case anyone else runs up against such a thing.
Sorry I don't have a patch - still kind of clueless in that area.
Comment | File | Size | Author |
---|---|---|---|
#2 | 561672-commentrss-indexes-D6.patch | 1.77 KB | Dave Reid |
Comments
Comment #1
Dave ReidMarked #567668: Page load is extremely slow with many comments as a duplicate of this issue. This needs an alternate solution because STRAIGHT_JOIN is a MySQL-specific command. It will fail on PostgreSQL.
Comment #2
Dave ReidMaybe just adding the indexes would be a big help? We can implement hook_schema_alter() (with install, uninstall and update) and add our own indexes to the {comments} table. Could you please review this patch to see if it adds the indexes properly and helps speed the query up?
Comment #3
kcoop CreditAttribution: kcoop commentedChris Clark tried this first, but found mysql didn't use the indexes.
Comment #4
fuscata CreditAttribution: fuscata commentedAdding an index on status and timestamp (i.e. a single index on both fields) fixed the problem. I did not test with the original query, but it makes the modified query instantaneous.