trick to reduce queries on topic pages
-
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.
- You must be logged in to reply to this topic.