I need to use some aggregate
data in my django
application that changes frequently and if I do the calculations on the fly some performance issues may happen. Because of that I need to save the aggregate
results in a table and, when data changes, update them. Because I use django
some options may be exist and some maybe not. For example I can use django signals
and a table that, when post_save
signal is emitted, updates the results. Another option is materialized views
in postgresql
or indexed views
in MSSQL Server
, that I do not know how to use in django
or if django
supports them or not. What is the best way to do this in django
for improving performance and accuracy of results.
You can use Materialized view with postgres. It's very simple.
CREATE MATERIALIZED VIEW
my_view as select * from my_table;
Create a model with two
option managed=false
and db_name=my_view
in the model Meta
like
this
MyModel(models.Model):
class Meta:
managed = False
db_table='my_view'
Simply use powers of ORM and treat MyModel as a regular model. e.g. MyModel.objects.count()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With