PERF: Don't pluck all the columns just to retrieve a single value.

This commit is contained in:
Guo Xiang Tan
2018-06-27 11:41:35 +08:00
parent 7dce8290ed
commit cb69888758
5 changed files with 42 additions and 25 deletions
+3 -2
View File
@@ -1,6 +1,6 @@
module TimelineLookup
# Given an array of tuples (id, post_number, days_ago), return at most `max_values` worth of a
# Given an array of tuples containing (id, days_ago), return at most `max_values` worth of a
# lookup table to help the front end timeline display dates associated with posts
def self.build(tuples, max_values = 300)
result = []
@@ -9,9 +9,10 @@ module TimelineLookup
last_days_ago = -1
tuples.each_with_index do |t, idx|
return result unless t.is_a?(Array)
next unless (idx % every) === 0
days_ago = t[2]
days_ago = t[1]
if (days_ago != last_days_ago)
result << [idx + 1, days_ago]
+14 -4
View File
@@ -374,14 +374,14 @@ class TopicView
@filtered_posts.by_newest.with_user.first(25)
end
# Returns an array of [id, post_number, days_ago] tuples.
# Returns an array of [id, days_ago] tuples.
# `days_ago` is there for the timeline calculations.
def filtered_post_stream
@filtered_post_stream ||= begin
posts = @filtered_posts
.order(:sort_order)
columns = [:id, :post_number]
columns = [:id]
if !is_mega_topic?
columns << 'EXTRACT(DAYS FROM CURRENT_TIMESTAMP - created_at)::INT AS days_ago'
@@ -392,7 +392,13 @@ class TopicView
end
def filtered_post_ids
@filtered_post_ids ||= filtered_post_stream.map { |tuple| tuple[0] }
@filtered_post_ids ||= filtered_post_stream.map do |tuple|
if is_mega_topic?
tuple
else
tuple[0]
end
end
end
def unfiltered_post_ids
@@ -406,6 +412,10 @@ class TopicView
end
end
def last_read_post_id(post_number)
@filtered_posts.where(post_number: post_number).pluck(:id).first
end
protected
def read_posts_set
@@ -571,6 +581,6 @@ class TopicView
MEGA_TOPIC_POSTS_COUNT = 10000
def is_mega_topic?
@topic.posts_count >= MEGA_TOPIC_POSTS_COUNT
@is_mega_topic ||= (@topic.posts_count >= MEGA_TOPIC_POSTS_COUNT)
end
end