PERF: reindex search data without loading large post counts

This commit is contained in:
Sam Saffron 2017-08-16 08:18:59 -04:00
parent d27e81a296
commit 56f7b4e01e
1 changed files with 26 additions and 17 deletions

View File

@ -10,41 +10,46 @@ module Jobs
rebuild_problem_users rebuild_problem_users
end end
def rebuild_problem_categories(limit = 10000) def rebuild_problem_categories(limit = 500)
categories = load_problem_categories(limit) category_ids = load_problem_category_ids(limit)
categories.each do |category| category_ids.each do |id|
SearchIndexer.index(category, force: true) category = Category.find_by(id: id)
SearchIndexer.index(category, force: true) if category
end end
end end
def rebuild_problem_users(limit = 10000) def rebuild_problem_users(limit = 10000)
users = load_problem_users(limit) user_ids = load_problem_user_ids(limit)
users.each do |user| user_ids.each do |id|
SearchIndexer.index(user, force: true) user = User.find_by(id: id)
SearchIndexer.index(user, force: true) if user
end end
end end
def rebuild_problem_topics(limit = 10000) def rebuild_problem_topics(limit = 10000)
topics = load_problem_topics(limit) topic_ids = load_problem_topic_ids(limit)
topics.each do |topic| topic_ids.each do |id|
SearchIndexer.index(topic, force: true) topic = Topic.find_by(id: id)
SearchIndexer.index(topic, force: true) if topic
end end
end end
def rebuild_problem_posts(limit = 10000) def rebuild_problem_posts(limit = 10000)
posts = load_problem_posts(limit) post_ids = load_problem_post_ids(limit)
posts.each do |post| post_ids.each do |id|
SearchIndexer.index(post, force: true) post = Post.find_by(id: id)
# could be deleted while iterating through batch
SearchIndexer.index(post, force: true) if post
end end
end end
private private
def load_problem_posts(limit) def load_problem_post_ids(limit)
Post.joins(:topic) Post.joins(:topic)
.where('posts.id IN ( .where('posts.id IN (
SELECT p2.id FROM posts p2 SELECT p2.id FROM posts p2
@ -52,27 +57,31 @@ module Jobs
WHERE pd.post_id IS NULL WHERE pd.post_id IS NULL
)', SiteSetting.default_locale, Search::INDEX_VERSION) )', SiteSetting.default_locale, Search::INDEX_VERSION)
.limit(limit) .limit(limit)
.pluck(:id)
end end
def load_problem_categories(limit) def load_problem_category_ids(limit)
Category.joins(:category_search_data) Category.joins(:category_search_data)
.where('category_search_data.locale != ? .where('category_search_data.locale != ?
OR category_search_data.version != ?', SiteSetting.default_locale, Search::INDEX_VERSION) OR category_search_data.version != ?', SiteSetting.default_locale, Search::INDEX_VERSION)
.limit(limit) .limit(limit)
.pluck(:id)
end end
def load_problem_topics(limit) def load_problem_topic_ids(limit)
Topic.joins(:topic_search_data) Topic.joins(:topic_search_data)
.where('topic_search_data.locale != ? .where('topic_search_data.locale != ?
OR topic_search_data.version != ?', SiteSetting.default_locale, Search::INDEX_VERSION) OR topic_search_data.version != ?', SiteSetting.default_locale, Search::INDEX_VERSION)
.limit(limit) .limit(limit)
.pluck(:id)
end end
def load_problem_users(limit) def load_problem_user_ids(limit)
User.joins(:user_search_data) User.joins(:user_search_data)
.where('user_search_data.locale != ? .where('user_search_data.locale != ?
OR user_search_data.version != ?', SiteSetting.default_locale, Search::INDEX_VERSION) OR user_search_data.version != ?', SiteSetting.default_locale, Search::INDEX_VERSION)
.limit(limit) .limit(limit)
.pluck(:id)
end end
end end
end end