From 03f63d970bb683e0e33097d39d8ae78d786b0309 Mon Sep 17 00:00:00 2001
From: Sam <sam.saffron@gmail.com>
Date: Tue, 1 Jul 2014 22:00:57 +1000
Subject: [PATCH] PERF: fix slow query

---
 lib/search.rb | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/lib/search.rb b/lib/search.rb
index de7b07684..cc7001f20 100644
--- a/lib/search.rb
+++ b/lib/search.rb
@@ -43,9 +43,10 @@ class Search
 
   def self.rebuild_problem_posts(limit = 10000)
     posts = Post.joins(:topic)
-            .where('posts.id NOT IN (
-               SELECT post_id from post_search_data
-                WHERE locale = ?
+            .where('posts.id IN (
+               SELECT p2.id FROM posts p2
+               LEFT JOIN post_search_data pd ON locale = ? AND p2.id = pd.post_id
+               WHERE pd.post_id IS NULL
               )', SiteSetting.default_locale).limit(10000)
 
     posts.each do |post|