A discussion on a forum bug reveals that bookmarking a post in a large topic (e.g., with 4,500+ posts) causes extremely slow loading times, sometimes resulting in a 502 error. Macaw first identified the issue, noting that removing bookmarks resolved the problem. codinghorror confirmed it was previously reported, and martin acknowledged the oversight and investigated. martin reproduced the issue on the Roblox forum but not locally, suspecting a database query issue. sam pinpointed the cause: a poor query plan due to a LIMIT 1 in topic.posts.with_deleted.where(post_number: 1).first, which prevents the use of an index. martin confirmed this and promised a fix. The issue is now closed as a duplicate of a related thread.
This thread in particular had been bugging me for a very long time. I asked, why is it taking from 1-3 minutes to load, if it even decides to load?
After seeing someone second this issue in this post, I decided to look into it: I removed all bookmarks from the thread with 4,500 posts.
It loaded instantly like any other post would.
I then asked myself, what other ways could I test this?
Irregardless of whether or not I bookmarked the first post or the latest post, it still took a while to load, albeit not minutes but around 10 seconds.
The effect was exacerbated on threads with more posts; I feel like there’s a certain threshold around 4000 posts that makes loading times this slow.
I tested this on other threads with more than 4000 posts.
The more bookmarks a topic has, the longer it takes to load.
I theorize that the more bookmarks you have on a topic with many posts, the longer it takes to load.
To repro: Open up a topic with many, many posts and bookmark one. It’s too bad that the referenced thread needs you to sign in to bookmark a post, and that the thread with the most amount of replies on this website is 925.
If I need to provide any more information, go ahead and ask.
However, I made a topic with 5000 posts locally and made a bookmark, and it loads in the same time whether I have a bookmark in the topic or not . I will have to keep looking…the code that loads the bookmarks for a user in a topic is here:
And the code that finds a bookmark for each post is here:
To me this does not look crazy and performance intensive. user_post_bookmarks will only query the database once because it is memoized, so no N1 issue here. Also here is the topic view code to see if there is a bookmark at the topic level:
I don’t think so, the Roblox forum is on the version Version bump to v2.6.0.beta3 · discourse/discourse@2a268bd · GitHub. Since then there have only been two bookmark-related commits which I did, neither of which do anything with the topic bookmark loading or serialization.
The SQL generated contains a LIMIT 1 which makes it so the index_posts_on_topic_id_and_post_number index is never used, causing a huge delay. I am unable to reproduce this locally because I have barely any posts in the local database; basically the more posts in your entire Discourse forum the worse this issue gets. I will have a fix up shortly.