Guo Xiang Tan 28148197d6 PERF: Avoid NOT IN (<subquery>> which can get really slow.
```
EXPLAIN ANALYZE SELECT  "topics".*
FROM "topics" LEFT JOIN topic_users tu ON topics.id = tu.topic_id AND
tu.user_id = 13455
WHERE ("topics"."deleted_at" IS NULL)
AND (topics.archetype = 'private_message')
AND (
  topics.id IN (
    SELECT topic_id
    FROM topic_allowed_users
    WHERE user_id = 13455
   )
)
AND (
  topics.id IN (
    SELECT ta.topic_id
    FROM topic_allowed_users ta
    WHERE ta.user_id IN (2,1995,8307,17621,22980,-10)
  )
  OR
  topics.id IN (
    SELECT tg.topic_id
    FROM topic_allowed_groups tg
    WHERE tg.group_id IN (-10)
  )
)
AND (topics.id NOT IN (68559,60069,42145))
AND "topics"."visible" = 't'
ORDER BY topics.bumped_at
DESC LIMIT 5;
```

Planning time: 1.196 ms
Execution time: 21.176 ms

```
EXPLAIN ANALYZE SELECT  "topics".*
FROM "topics"
LEFT JOIN topic_users tu ON topics.id = tu.topic_id AND tu.user_id =
13455
LEFT JOIN topic_allowed_users ta ON topics.id = ta.topic_id AND
ta.user_id = 13455
LEFT JOIN topic_allowed_users ta2 ON topics.id = ta2.topic_id AND
ta2.user_id IN (2,1995,8307,17621,22980,-10)
LEFT JOIN topic_allowed_groups tg ON topics.id = tg.topic_id AND
tg.group_id IN (-10)
WHERE ("topics"."deleted_at" IS NULL)
AND (topics.archetype = 'private_message')
AND (ta.topic_id IS NOT NULL)
AND (ta2.topic_id IS NOT NULL OR tg.topic_id IS NOT NULL)
AND (topics.id NOT IN (68559,60069,42145))
AND "topics"."visible" = 't'
ORDER BY topics.bumped_at DESC
LIMIT 5;
```

Planning time: 1.792 ms
Execution time: 2.546 ms
2017-09-13 22:28:33 +08:00
..
2017-08-10 22:54:52 +01:00
2017-07-28 10:20:09 +09:00
2017-07-29 13:12:45 +02:00
2017-07-28 10:20:09 +09:00
2017-07-28 10:20:09 +09:00
2017-07-28 10:20:09 +09:00
2017-09-08 14:07:24 +08:00
2017-07-28 10:20:09 +09:00
2017-07-28 10:20:09 +09:00
2017-07-28 10:20:09 +09:00
2017-07-28 10:20:09 +09:00
2017-07-03 15:26:57 -04:00
2017-07-28 10:20:09 +09:00
2017-08-25 11:52:59 -04:00
2017-07-28 10:20:09 +09:00
2017-07-28 10:20:09 +09:00
2017-08-31 14:54:44 -04:00
2017-07-28 10:20:09 +09:00