I have a REST service(the Project uses django and DRF) which serves data from a MSSQL database. One of the models stored inside the DB are invcoies which looks like this:
class Invoice(models.Model):
id = models.IntegerField(primary_key=True)
customer = models.ForeignKey('project_models.Customer', on_delete=models.CASCADE,related_name='invoices')
material = models.ForeignKey('project_models.Material', on_delete=models.SET_NULL)
quantity = models.FloatField()
revenue = models.FloatField()
invoice_date = models.DateField()
I want to expose some aggregated data for these invoices per material like:
- Sum of revenue for the current year
- Sum of revenue for the past year
- Sum of revenue for the past year up to todays date (e.g. today is 2022-03-02, Sum would be from 2021-01-01 to 2021-03-02)
To solve this I could execute a query with minimal aggregation(e.g.revenue sum per day) and iterate over the result set to create a list of entries which contains all necessary informations. However as there are lots of materials this solution can lead to performance issues. This problem would normally be solved by paginating the queryset. Seeing as I still fetch and calculate as well as iterate over all the data of the DB it seems to be not the best solution.
Therefore my question is: what would be a the best approach to aggregate multiple data while still mainting pagination.