[FIX] gamification: performance improvement

On a large database, thousands of badges may have been granted. This code used to prefetch all fields of the users and badge users. This was highly inefficient. Replace this code by SQL query.
This commit is contained in:
Martin Trigaux
2014-09-01 17:40:47 +02:00
parent 24c25f506c
commit 40e843761f
+11 -6
View File
@@ -96,12 +96,17 @@ class gamification_badge(osv.Model):
the total number of users this badge was granted to the total number of users this badge was granted to
""" """
result = dict.fromkeys(ids, False) result = dict.fromkeys(ids, False)
for obj in self.browse(cr, uid, ids, context=context): for badge_id in ids:
res = list(set(owner.user_id.id for owner in obj.owner_ids)) cr.execute("""
result[obj.id] = { SELECT user_id
'unique_owner_ids': res, FROM gamification_badge_user
'stat_count': len(obj.owner_ids), WHERE badge_id = %s
'stat_count_distinct': len(res) """, (badge_id,))
res = [user_id[0] for user_id in cr.fetchall()]
result[badge_id] = {
'unique_owner_ids': list(set(res)),
'stat_count': len(res),
'stat_count_distinct': len(list(set(res)))
} }
return result return result