Skip to content

Commit

Permalink
fix: use subquery when fetching questions without user votes
Browse files Browse the repository at this point in the history
fixes #1147.
We now use a subquery instead of an outer join, which is much faster.
  • Loading branch information
raphael0202 committed Aug 13, 2024
1 parent 677cd62 commit 15e6cfb
Showing 1 changed file with 9 additions and 12 deletions.
21 changes: 9 additions & 12 deletions robotoff/app/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,24 +49,19 @@ 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:
criteria = AnnotationVote.username == exclusion.id
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(
Expand Down Expand Up @@ -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)

Expand Down

0 comments on commit 15e6cfb

Please sign in to comment.