OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Optimizing Django app for displaying aggregated data from multiple related objects efficiently

  • Thread starter Thread starter bravesheeptribe
  • Start date Start date
B

bravesheeptribe

Guest
I'm developing a Django app where users can create and manage hierarchical data structures with multiple levels (in my case only 4 levels). The app needs to efficiently display aggregated data (e.g., averages, sums, and other more complicated metrics) for each level based on the aggregated data of the level below it.

Here's a simplified version of my data models:

Code:
from django.db import models

class Level1(models.Model):
    name = models.CharField(max_length=100)

class Level2(models.Model):
    level1 = models.ForeignKey(Level1, on_delete=models.CASCADE)
    name = models.CharField(max_length=100)

class Level3(models.Model):
    level2 = models.ForeignKey(Level2, on_delete=models.CASCADE)
    name = models.CharField(max_length=100)

class DataPoint(models.Model):
    level3 = models.ForeignKey(Level3, on_delete=models.CASCADE)
    value = models.FloatField()

For example, aggregated data for Level3 objects will be based on DataPoints, aggregated data for Level2 models will be based on the aggregated data for its related Level3 objects, and aggregated data for Level1 models will be based on the aggregated data of its related Level2 objects.

This means that I really only have to re-compute the aggregated data of a level if data from a level under it changes.

Currently, I calculate the aggregated data every time they get requested, but this probably isn't good for performance (e.g. I'm displaying the aggregated data of multiple Level1 objects at once, meaning I have to make many expensive calculations).

One solution that I'm thinking about is to use signals to only re-compute the aggregated data when necessary, and store the computed data somewhere in the database. However, the trade-off to this is that write operations would be much slower as I would have to wait for all the computations to finish.
<p>I'm developing a Django app where users can create and manage hierarchical data structures with multiple levels (in my case only 4 levels). The app needs to efficiently display aggregated data (e.g., averages, sums, and other more complicated metrics) for each level based on the aggregated data of the level below it.</p>
<p>Here's a simplified version of my data models:</p>
<pre><code>from django.db import models

class Level1(models.Model):
name = models.CharField(max_length=100)

class Level2(models.Model):
level1 = models.ForeignKey(Level1, on_delete=models.CASCADE)
name = models.CharField(max_length=100)

class Level3(models.Model):
level2 = models.ForeignKey(Level2, on_delete=models.CASCADE)
name = models.CharField(max_length=100)

class DataPoint(models.Model):
level3 = models.ForeignKey(Level3, on_delete=models.CASCADE)
value = models.FloatField()
</code></pre>
<p>For example, aggregated data for <code>Level3</code> objects will be based on <code>DataPoints</code>, aggregated data for <code>Level2</code> models will be based on the aggregated data for its related <code>Level3</code> objects, and aggregated data for <code>Level1</code> models will be based on the aggregated data of its related <code>Level2</code> objects.</p>
<p>This means that I really only have to re-compute the aggregated data of a level if data from a level under it changes.</p>
<p>Currently, I calculate the aggregated data every time they get requested, but this probably isn't good for performance (e.g. I'm displaying the aggregated data of multiple <code>Level1</code> objects at once, meaning I have to make many expensive calculations).</p>
<p>One solution that I'm thinking about is to use signals to only re-compute the aggregated data when necessary, and store the computed data somewhere in the database. However, the trade-off to this is that write operations would be much slower as I would have to wait for all the computations to finish.</p>
 

Latest posts

Online statistics

Members online
0
Guests online
4
Total visitors
4
Top