use replica db for calculating post counts

This commit is contained in:
r888888888
2016-01-28 18:31:03 -08:00
parent e6b16e8fe5
commit 81684e6421
2 changed files with 13 additions and 4 deletions

View File

@@ -1061,8 +1061,8 @@ class Post < ActiveRecord::Base
end
def fast_count_search(tags, options = {})
count = Post.with_timeout(options[:statement_timeout] || 500, nil, :tags => tags) do
Post.tag_match(tags).count
count = PostReadOnly.with_timeout(3_000, nil) do
PostReadOnly.tag_match(tags).count
end
if count == nil && tags !~ / /
@@ -1083,8 +1083,8 @@ class Post < ActiveRecord::Base
i = Post.maximum(:id)
sum = 0
while i > 0
count = Post.with_timeout(options[:statement_timeout] || 500, nil, :tags => tags) do
sum += Post.tag_match(tags).where("id <= ? and id > ?", i, i - 25_000).count
count = PostReadOnly.with_timeout(1_000, nil) do
sum += PostReadOnly.tag_match(tags).where("id <= ? and id > ?", i, i - 25_000).count
i -= 25_000
end

View File

@@ -1,4 +1,13 @@
class PostReadOnly < Post
establish_connection "ro_#{Rails.env}".to_sym
attr_readonly *column_names
def with_timeout(n, default_value = nil)
connection.execute("SET STATEMENT_TIMEOUT = #{n}") unless Rails.env == "test"
yield
rescue ::ActiveRecord::StatementInvalid => x
return default_value
ensure
connection.execute("SET STATEMENT_TIMEOUT = 0") unless Rails.env == "test"
end
end