[FIX] gamification: performance improvement
authorMartin Trigaux <mat@openerp.com>
Mon, 1 Sep 2014 15:40:47 +0000 (17:40 +0200)
committerMartin Trigaux <mat@openerp.com>
Mon, 1 Sep 2014 15:40:47 +0000 (17:40 +0200)
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.

addons/gamification/models/badge.py

index 74a85c1..326e48f 100644 (file)
@@ -96,12 +96,17 @@ class gamification_badge(osv.Model):
             the total number of users this badge was granted to
         """
         result = dict.fromkeys(ids, False)
-        for obj in self.browse(cr, uid, ids, context=context):
-            res = list(set(owner.user_id.id for owner in obj.owner_ids))
-            result[obj.id] = {
-                'unique_owner_ids': res,
-                'stat_count': len(obj.owner_ids),
-                'stat_count_distinct': len(res)
+        for badge_id in ids:
+            cr.execute("""
+                SELECT user_id
+                FROM gamification_badge_user
+                WHERE badge_id = %s
+                """, (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