Fix #3934: Post#fast_count has very slow worst case behavior.

This commit is contained in:
evazion
2018-10-01 12:55:57 -05:00
parent 215591403e
commit ccb57e802c

View File

@@ -1207,10 +1207,6 @@ class Post < ApplicationRecord
PostReadOnly.tag_match(tags).count
end
if count.nil?
count = fast_count_search_batched(tags, options)
end
if count.nil?
# give up
if options[:raise_on_timeout]
@@ -1227,26 +1223,6 @@ class Post < ApplicationRecord
return nil
end
def fast_count_search_batched(tags, options)
# this is slower but less likely to timeout
i = Post.maximum(:id)
sum = 0
while i > 0
count = PostReadOnly.with_timeout(1_000, nil, {:tags => tags}) do
sum += PostReadOnly.tag_match(tags).where("id <= ? and id > ?", i, i - 25_000).count
i -= 25_000
end
if count.nil?
return nil
end
end
sum
rescue PG::ConnectionBad
return nil
end
def fix_post_counts(post)
post.set_tag_counts(false)
if post.changes_saved?