Comments table has nearly 1 million records. The /crss page takes about 45 seconds to load. Upon investigation, I've found a problem with this query:
$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';
I optimized it by changing it to:
$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 adding a multi-column index on status and timestamp to the comments table.
Note, that I'm using an older version -- not certain which one. I see that the query has changed in CVS (see below), although I believe that the query is still poorly optimized as comments is not the first table referenced and there will not be an index on status-timestamp in the comments table by default.
$SQL = 'SELECT '. $nidselector .', c.cid, c.subject, c.comment, c.timestamp, c.uid, c.name, c.format FROM {node} n ' . $joins . ' INNER JOIN {comments} c ON c.nid = n.nid WHERE ' . $where . ' n.status = 1 AND c.status = %d ORDER BY c.timestamp DESC';
Comments
Comment #1
dave reidDuplicate of #561672: Slow Query