Slow deep pages of large topics: per-row hidden-forum meta join
-
Hi,
bbPress 2.6.22, WordPress 6.9, MariaDB 11.8 (also reproduced on MySQL 5.7).
We have a few very large topics, the biggest one has 31,376 replies (~2,100 pages at 15 per page). Deep pages of such a topic take 4–6 seconds to render, while page 2 takes ~0.3 s. Crawlers walking those pages were enough to exhaust our PHP-FPM pool.
The cause is the reply query that
bbp_has_replies()produces for anyone who is not a keymaster:bbp_pre_get_posts_normalize_forum_visibility()adds the hidden/private forum exclusion as a meta query.SELECT SQL_CALC_FOUND_ROWS wp_posts.* FROM wp_posts FORCE INDEX (PRIMARY, post_parent) INNER JOIN wp_postmeta ON ( wp_posts.ID = wp_postmeta.post_id ) WHERE 1=1 AND (wp_posts.ID = 154520 OR wp_posts.post_parent = 154520) AND ( ( wp_postmeta.meta_key = '_bbp_forum_id' AND CAST(wp_postmeta.meta_value AS SIGNED) NOT IN ('154499') ) ) AND ((wp_posts.post_type = 'reply' AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'closed')) OR (wp_posts.post_type = 'topic' AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'closed'))) GROUP BY wp_posts.ID ORDER BY wp_posts.post_date ASC LIMIT 27225, 15The join, the CAST on
meta_valueand the GROUP BY run for every row up to the offset. Inside a single topic’s reply loop the answer is the same for every row: all replies belong to the topic’s forum, and the topic page itself is already a 404 for users who cannot see that forum.Timings of the query above (warm buffer pool, SHOW PROFILES):
- as generated: 0.295 s
- same query without the meta join and GROUP BY: 0.027 s
On production with a cold buffer pool the whole page went from 4–6 s to 0.3–0.5 s.
Our workaround: a
bbp_include_all_forumsfilter that returns true only when the query is a reply/topic loop whosepost_parentis a topic, that topic is publicly visible and not password protected, and neither the topic’s forum nor any of its ancestors is inbbp_exclude_forum_ids( 'array' )orbbp_get_password_protected_forum_ids(). Everything else keeps bbPress’s rules (a topic in a hidden forum still returns no replies for a regular member).Suggestion for core: when the query is the reply loop of one topic, check forum visibility once for the parent topic instead of per row via
_bbp_forum_id. The only behaviour difference I can see: a reply whose_bbp_forum_idmeta is out of sync with its topic’s forum (e.g. after a manual move) would no longer be filtered individually — arguably the topic’s forum is the source of truth there anyway.Happy to provide more details or test a patch.
Thanks!
- You must be logged in to reply to this topic.