Skip to content

Instantly share code, notes, and snippets.

@iAugur
Created February 13, 2013 21:42
Show Gist options
  • Select an option

  • Save iAugur/4948609 to your computer and use it in GitHub Desktop.

Select an option

Save iAugur/4948609 to your computer and use it in GitHub Desktop.
Drupal Rebuild node_comment_statistics from http://drupal.org/node/137458#comment-5072066
/**
* Rebuild the node_comment_statistics
* turn off when importing
* $commentvar = variable_get('comment_maintain_node_statistics', TRUE);
* variable_set('comment_maintain_node_statistics', FALSE);
* ref: http://drupal.org/node/137458#comment-5072066
*/
TRUNCATE TABLE node_comment_statistics;
INSERT INTO
node_comment_statistics
(
nid,
last_comment_timestamp,
last_comment_name,
last_comment_uid,
comment_count
)
SELECT
n.nid,
IFNULL(last_comment.created,n.changed) AS last_comment_timestamp,
IFNULL(last_comment.name,null) AS last_comment_name,
IFNULL(last_comment.uid,n.uid) AS last_comment_uid,
IFNULL(comment_count.comment_count,0) AS comment_count
FROM
node AS n
LEFT OUTER JOIN (SELECT nid, COUNT(*) AS comment_count FROM comment WHERE status=1 GROUP BY nid) AS comment_count ON comment_count.nid=n.nid
LEFT OUTER JOIN (SELECT nid, MAX(cid) AS max_cid FROM comment WHERE status=1 GROUP by nid) AS max_node_comment ON max_node_comment.nid=n.nid
LEFT OUTER JOIN (SELECT cid,uid,name,created FROM comment ORDER BY cid DESC LIMIT 1) AS last_comment ON last_comment.cid=max_node_comment.max_cid
WHERE
n.status=1
ORDER BY
n.nid;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment