PERF: use post number to create canoncial path in mega topics.

PERF: use post number to create canoncial path in mega topics.

We don’t need page number accuracy in mega topics since it can be expensive.

06d426bd87d5bf87e36dd543efd298aaef94156e

diff --git a/lib/topic_view.rb b/lib/topic_view.rb
index 17e1d89..bea37c1 100644
--- a/lib/topic_view.rb
+++ b/lib/topic_view.rb
@@ -122,7 +122,7 @@ class TopicView
       if @page > 1
         "?page=#{@page}"
       else
-        posts_count = unfiltered_posts.where("post_number <= ?", @post_number).count
+        posts_count = is_mega_topic? ? @post_number : unfiltered_posts.where("post_number <= ?", @post_number).count
         page = ((posts_count - 1) / @limit) + 1
         page > 1 ? "?page=#{page}" : ""
       end

GitHub sha: 6d177659

1 Like

Can you also add a test?

Also I wonder if we can avoid this query in more cases, we have all the post numbers already for the post stream, maybe we could that

2 Likes

It looks like we only have post numbers for the current page’s filtered posts. But here I’m looking for a total visible post count on the topic (less than the current post number post_number <= ?).

Follow-up: SPEC: use post number to create canoncial path in mega topics. · discourse/discourse@f3f30ed · GitHub

1 Like