OPTIMIZATION: Add index to `post_timings` and adjust the query

This commit is contained in:
Robin Ward 2014-08-01 13:09:16 -04:00
parent b414a3590a
commit 09d7a697bf
2 changed files with 8 additions and 4 deletions

View File

@ -30,10 +30,9 @@ class UserStat < ActiveRecord::Base
FROM
(SELECT pt.user_id,
COUNT(*) AS c
FROM post_timings AS pt
WHERE pt.user_id IN (
SELECT u1.id FROM users u1 where u1.last_seen_at > :seen_at
)
FROM users AS u
INNER JOIN post_timings AS pt ON pt.user_id = u.id
WHERE u.last_seen_at > :seen_at
GROUP BY pt.user_id) AS X
WHERE X.user_id = user_stats.user_id AND
X.c <> posts_read_count

View File

@ -0,0 +1,5 @@
class CreatePostTimingsUserIndex < ActiveRecord::Migration
def change
add_index :post_timings, :user_id
end
end