Forked from #145353: Forums performance improvements.

_forum_update_forum_index() performs the following expensive query on every comment insert, update, publish, or delete, regardless of whether the comment is attached to a forum topic node. In my context, this has a serious impact on migration rollback of comments.

    $last_reply = db_query_range('SELECT cid, name, created, uid FROM {comment} WHERE nid = :nid AND status = :status ORDER BY cid DESC', 0, 1, array(
      ':nid' => $nid,
      ':status' => COMMENT_PUBLISHED,
    ))->fetchObject();

This is to setup a db_update on forum_index, which will do nothing anyway if the parent node wasn't a forum topic. So, we want to avoid this query unless we know the node is already in forum_index. I added an inner join to forum_index on the initial comment query in the function, and in a very rough informal test the average time of forum_comment_delete() goes from 54ms to 1ms (this is with 73k comments in the comment table).

I'll freshen my D8 checkout and submit a patch.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

mikeryan’s picture

Status: Active » Needs review
FileSize
666 bytes
mikeryan’s picture

moshe weitzman’s picture

Status: Needs review » Reviewed & tested by the community

Makes sense, and code looks good.

chx’s picture

*blink* if that query is expensive , is it expensive because it's not indexed? If it is not, why doesn't forum add an index on comment table? If it's indexed how it can be expensive?

mikeryan’s picture

EXPLAIN shows it's using the comment_num_new index - the issue is the sort ("Using where; Using filesort").

At any rate, this query is always going to be more expensive than the join I added to the initial query, and it's an unnecessary query for any non-forum nodes, so this is a big win when dealing with 176k blog comments...

chx’s picture

After chatting with catch about this it seems #5 is a mis-post and I misunderstood what's going on. While the count query is likely to be near-instant as I suspected the point is not the query in the patch but not running the other two controlled by this count query. So yes, good to go.

chx’s picture

Also, further talking -- shouldnt we change the ORDER in the $last_reply query to be ORDER BY created, cid so the comment_num_new index (nid,status,created,cid,thread) can be used fully? Can be a followup or can be done here. Won't that speed up further?

effulgentsia’s picture

Re #7: that might speed up comment updates on long forum threads, but seems like a separate issue. #1 looks good for this issue, which is to prevent an unnecessary db_query_range() when dealing with a non-forum node.

effulgentsia’s picture

Title: Poor performance of _forum_update_forum_index() » Poor performance of _forum_update_forum_index() when passed a non-forum node
webchick’s picture

Status: Reviewed & tested by the community » Fixed

Committed and pushed to 8.x. Thanks! Does this make sense to backport too?

mikeryan’s picture

Version: 8.x-dev » 7.x-dev
Status: Fixed » Patch (to be ported)
Issue tags: +Needs backport to D7

Yes please! Let me generate the D7 patch...

mikeryan’s picture

Status: Patch (to be ported) » Needs review
FileSize
645 bytes

Status: Needs review » Needs work

The last submitted patch, forum-index_update_performance_d7-1715402-12.patch, failed testing.

droplet’s picture

c.nid=i.nid

a little code style problem ? (in both D7 & 8 patch)

mikeryan’s picture

Status: Needs work » Needs review
FileSize
648 bytes

Ahhh, bad habit I have of compressing my SQL...

moshe weitzman’s picture

Status: Needs review » Reviewed & tested by the community
webchick’s picture

Status: Reviewed & tested by the community » Fixed

Committed and pushed to 7.x. I also went back to 8.x and fixed the spacing.

Automatically closed -- issue fixed for 2 weeks with no activity.