0

I have a model like this:

class Class(models.TextChoices):
    STANDARD = "Standard", _("Standard")
    JWW = "JWW", _("JWW")

class Competition(models.Model):
    cls = models.CharField(choices=Class.choices)
    date = models.DateField()

I would like to get all of the dates where a Standard Competition occurred AND a JWW Competition occurred. My current solution takes a long time for a low number of rows (~300), and relies on an intersection query.

I have a large amount of data upon which this calculation will need to be performed, so the faster the query, the better. Memory is not a concern.

Current Solution:

base_runs = (
    Competition.objects
    .filter(cls__in={Class.JWW, Class.STANDARD})
    .values("date", "cls")
)

JWW_runs = base_runs.filter(cls=Class.JWW)
standard_runs = base_runs.filter(cls=Class.STANDARD)
return JWW_runs.intersection(standard_runs).values("date").distinct().count()
Optionwiz
  • 326
  • 1
  • 4
  • 13
Quontas
  • 400
  • 1
  • 3
  • 19

0 Answers0