PERF: much faster Badge.ensure_consistency

This commit is contained in:
Régis Hanol 2017-04-24 22:36:07 +02:00
parent e6f22725c8
commit 3d406b047c
1 changed files with 16 additions and 4 deletions

View File

@ -132,12 +132,24 @@ class Badge < ActiveRecord::Base
exec_sql <<-SQL.squish
DELETE FROM user_badges
USING user_badges ub
LEFT JOIN users u ON u.id = ub.user_id
WHERE u.id IS NULL
AND user_badges.id = ub.id
LEFT JOIN users u ON u.id = ub.user_id
WHERE u.id IS NULL
AND user_badges.id = ub.id
SQL
Badge.find_each(&:reset_grant_count!)
exec_sql <<-SQL.squish
WITH X AS (
SELECT badge_id
, COUNT(user_id) users
FROM user_badges
GROUP BY badge_id
)
UPDATE badges
SET grant_count = X.users
FROM X
WHERE id = X.badge_id
AND grant_count <> X.users
SQL
end
def awarded_for_trust_level?