From 15e6cfb0749bafff9de25b6f3328f61d445f84e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Bournhonesque?= Date: Tue, 13 Aug 2024 15:52:04 +0200 Subject: [PATCH] fix: use subquery when fetching questions without user votes fixes #1147. We now use a subquery instead of an outer join, which is much faster. --- robotoff/app/core.py | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/robotoff/app/core.py b/robotoff/app/core.py index 5e8cba3e2c..6db13067fd 100644 --- a/robotoff/app/core.py +++ b/robotoff/app/core.py @@ -49,12 +49,9 @@ class SkipVotedOn(NamedTuple): id: str -def _add_vote_exclusions( - query: peewee.Query, exclusion: Optional[SkipVotedOn] -) -> peewee.Query: - if not exclusion: - return query - +def _add_vote_exclusion_clause(exclusion: SkipVotedOn) -> peewee.Expression: + """Return a peewee expression to exclude insights that have been voted on + by the user.""" if exclusion.by == SkipVotedType.DEVICE_ID: criteria = AnnotationVote.device_id == exclusion.id elif exclusion.by == SkipVotedType.USERNAME: @@ -62,11 +59,9 @@ def _add_vote_exclusions( else: raise ValueError(f"Unknown SkipVoteType: {exclusion.by}") - return query.join( - AnnotationVote, - join_type=peewee.JOIN.LEFT_OUTER, - on=((AnnotationVote.insight_id == ProductInsight.id) & (criteria)), - ).where(AnnotationVote.id.is_null()) + return ProductInsight.id.not_in( + AnnotationVote.select(AnnotationVote.insight_id).where(criteria) + ) def get_insights( @@ -178,8 +173,10 @@ def get_insights( if predictor is not None: where_clauses.append(ProductInsight.predictor == predictor) - query = _add_vote_exclusions(ProductInsight.select(), avoid_voted_on) + if avoid_voted_on: + where_clauses.append(_add_vote_exclusion_clause(avoid_voted_on)) + query = ProductInsight.select() if where_clauses: query = query.where(*where_clauses)