Thursday, 4 October 2018

Optimizing a Django query

I have 2 models like these:

class Client(models.Model):
  // some fields

class Transaction(models.Model):
  client = models.ForeignKey(Client)
  created = models.DateTimeField(auto_now_add=True)
  amount = DecimalField(max_digits=9, decimal_places=2)

I want to write a query that add the last created Transaction amount, per clients, only if created is lower than a provided date argument.

For example, if I have a dataset like this one, and that the provided date is 01/20:

Client1:
- Transaction 1, created on 01/15, 5€
- Transaction 2, created on 01/16, 6€
- Transaction 3, created on 01/22, 7€

Client2:
- Transaction 4, created on 01/18, 8€
- Transaction 5, created on 01/19, 9€
- Transaction 6, created on 01/21, 10€

Client3:
- Transaction 7, created on 01/21, 11€

Then, the query should return 15 (6€ + 9€ from transaction 2 and 5).

From a performance view, my purpose is to avoid having N queries for N clients.

Currently, I have trouble selecting the right Transaction objects. Maybe I could start by: Transaction.objects.filter(created__lt=date).select_related('client'). But then, I can't figure out how to select only the latest per client.



from Optimizing a Django query

No comments:

Post a Comment