From 7b38be01013da2b4c4ba277043d7e31b43089f8c Mon Sep 17 00:00:00 2001 From: TLSM Date: Sat, 25 Jun 2022 20:22:56 -0400 Subject: [PATCH 1/2] Reduce query volume in user.user_awards. The User model class method user_awards previously made one query per type of award. This has been replaced with a new query that retrieves all owned award quantities at once using GROUP BY. --- files/classes/user.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/files/classes/user.py b/files/classes/user.py index 67a5b3c016..f78d75251d 100644 --- a/files/classes/user.py +++ b/files/classes/user.py @@ -20,6 +20,7 @@ from .sub_block import * from .submission import sort_posts from files.__main__ import Base, cache from files.helpers.security import * +from copy import deepcopy import random from os import environ, remove, path @@ -246,12 +247,18 @@ class User(Base): @property @lazy def user_awards(self): - return_value = list(AWARDS2.values()) - user_awards = g.db.query(AwardRelationship).filter_by(user_id=self.id) + awards_owned = g.db.query(AwardRelationship.kind, func.count()) \ + .filter_by(user_id=self.id, submission_id=None, comment_id=None) \ + .group_by(AwardRelationship.kind).all() + awards_owned = dict(awards_owned) - for val in return_value: val['owned'] = user_awards.filter_by(kind=val['kind'], submission_id=None, comment_id=None).count() + for val in return_value: + if val['kind'] in awards_owned: + val['owned'] = awards_owned[val['kind']] + else: + val['owned'] = 0 return return_value From 0f81e6f55137e0fdf9db15b8e856ce10ce40fee1 Mon Sep 17 00:00:00 2001 From: Aevann1 Date: Sat, 25 Jun 2022 21:00:01 +0000 Subject: [PATCH 2/2] sneed --- schema.sql | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/schema.sql b/schema.sql index e2e738c197..0a536e9dc4 100644 --- a/schema.sql +++ b/schema.sql @@ -140,7 +140,8 @@ CREATE TABLE public.award_relationships ( user_id integer NOT NULL, submission_id integer, comment_id integer, - kind character varying(20) NOT NULL + kind character varying(20) NOT NULL, + awarded_utc integer );