Skip to:
Content
Pages
Categories
Search
Top
Bottom

trick to reduce queries on topic pages


  • _ck_
    Participant

    @_ck_

    I’ve noticed that depending on the site configuration, many times plugins or template designs will require information about the first poster in a topic or the last poster.

    The problem is that bbPress unfortunately (even in 1.1) only caches the post authors that are on the physical page you are on. So it causes two “out of flow” queries (one for user, one for usermeta) for each of those authors (first, last).

    This means up to four needless queries are added depending on the topic page number (not first or last is the worst hit).

    Even worse there is no direct filter/action on that process so it cannot be affected directly.

    However I’ve come up with a workaround using a trick,

    by catching the actions before and after the process.

    I’ve only tested this on 0.9, but in theory should work in 1.1

    if (is_topic && !is_bb_feed()) {
    add_action('bb_topic.php_pre_db','usercachefix_load');
    add_action('bb_topic.php','usercachefix_unload');
    function usercachefix_load() {add_action('get_forum_where','usercachefix');}
    function usercachefix_unload() {remove_action('get_forum_where','usercachefix'); global $posts; unset($posts['first'],$posts['last']);}
    function usercachefix($x) {global $topic,$posts; $posts['first']->poster_id=$topic->topic_poster; $posts['last']->poster_id=$topic->topic_last_poster; return $x;}
    }

    Make it into a mini-plugin and give it a try.

    You really only need it if you are showing info about the first and/or last poster on every page of the topic.

    You should see the query count go down by two to four queries on the page.

    Or use bb-benchmark to more closely examine the queries and look for multiple calls to the user-table and usermeta-table.

Viewing 1 replies (of 1 total)

  • kevinjohngallagher
    Member

    @kevinjohngallagher

    _ck_,

    More truly excellent work.

    I offered a different solution though, it’s a geneology plugin thats currently with a few folks for testing. It is far from as elegant as this (though it’s scope is wider).

    One of the things that I’ve “diskiked” about bbPress is lack of support for heirarchies. I know you’ve written some excellent code for finding the last poster for forums with sub forums before, but like the code above, they’re all workarounds.

    It also makes it difficult for theme developers to get on board when wanting to call basic API functions for “last poster” for a forum or topic etc. At some stage, I’d love a discussion on small things like this where we could improve bbP0.9

Viewing 1 replies (of 1 total)

You must be logged in to reply to this topic.