I was watching my mysql-slow.log and noticed the following:
# Query_time: 2 Lock_time: 0 Rows_sent: 1 Rows_examined: 364684
SELECT COUNT(cid) FROM comments WHERE pid = 367522 AND status = 0;
# Time: 061204 18:00:17
# User@Host: XXX[XXX] @ XXX []
# Query_time: 4 Lock_time: 0 Rows_sent: 1 Rows_examined: 364721
SELECT COUNT(cid) FROM comments WHERE pid = 367575 AND status = 0;
# Time: 061204 18:00:56
# User@Host: XXX[XXX] @ XXX []
# Query_time: 3 Lock_time: 0 Rows_sent: 1 Rows_examined: 364723
SELECT COUNT(cid) FROM comments WHERE pid = 367633 AND status = 0;
A quick look shows that there's no index on pid in the comments table, so I added one:
mysql> alter table comments add key pid (pid);
Query OK, 364784 rows affected (15.34 sec)
After this for 4/5 hours there were no slow queries any more. I don't remember seeing this with 4.6 but...
PP: There are three places comment.module selects by pid - lines:
/* 549 */ // validated/filtered data to perform such check.
/* 550 */ $duplicate = db_result(db_query("SELECT COUNT(cid) FROM {comments} WHERE pid = %d ..."
/* 551 */ if ($duplicate != 0) {
/*--
/* 1121 */ if (!isset($cache[$pid])) {
/* 1122 */ $cache[$pid] = db_result(db_query('SELECT COUNT(cid) FROM {comments} WHERE pid = %d ...'
/* 1123 */ }
/*--
/* 1727 */function _comment_update_node_statistics($nid) {
/* 1728 */ $count = db_result(db_query('SELECT COUNT(cid) FROM {comments} WHERE nid = %d AND...'
/* 1729 */
(long lines truncated to fit in the page)
Comments
Comment #1
dave reidDuplicate of #243093: missing indices on comment table.