optimise view count calculation query
This commit is contained in:
parent
ee286cc270
commit
441010ac65
2 changed files with 13 additions and 5 deletions
|
@ -329,12 +329,13 @@ class User < ActiveRecord::Base
|
|||
(SELECT v.user_id,
|
||||
COUNT(DISTINCT parent_id) AS c
|
||||
FROM views AS v
|
||||
WHERE parent_type = 'Topic'
|
||||
WHERE parent_type = 'Topic' AND v.user_id IN (
|
||||
SELECT u1.id FROM users u1 where u1.last_seen_at > :seen_at
|
||||
)
|
||||
GROUP BY v.user_id) AS X
|
||||
WHERE
|
||||
X.user_id = users.id AND
|
||||
X.c <> topics_entered AND
|
||||
users.last_seen_at > :seen_at
|
||||
X.c <> topics_entered
|
||||
", seen_at: 1.hour.ago
|
||||
|
||||
# Update denormalzied posts_read_count
|
||||
|
@ -343,10 +344,12 @@ class User < ActiveRecord::Base
|
|||
(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
|
||||
)
|
||||
GROUP BY pt.user_id) AS X
|
||||
WHERE X.user_id = users.id AND
|
||||
X.c <> posts_read_count AND
|
||||
users.last_seen_at > :seen_at
|
||||
X.c <> posts_read_count
|
||||
", seen_at: 1.hour.ago
|
||||
end
|
||||
|
||||
|
|
|
@ -0,0 +1,5 @@
|
|||
class AddUserIdParentTypeIndexOnViews < ActiveRecord::Migration
|
||||
def change
|
||||
add_index :views, [:user_id,:parent_type,:parent_id]
|
||||
end
|
||||
end
|
Reference in a new issue