From 40e843761f56e1d374937152bc0e2d89eb6f2672 Mon Sep 17 00:00:00 2001 From: Martin Trigaux Date: Mon, 1 Sep 2014 17:40:47 +0200 Subject: [PATCH] [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. --- addons/gamification/models/badge.py | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/addons/gamification/models/badge.py b/addons/gamification/models/badge.py index 74a85c1cf02..326e48f77d3 100644 --- a/addons/gamification/models/badge.py +++ b/addons/gamification/models/badge.py @@ -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