In a Django App I'm working on I've got this going on:
在我正在研究的Django应用程序中我已经开始了:
class Parent(models.Model):
name = models.CharField(...)
def num_children(self):
return Children.objects.filter(parent=self).count()
def avg_child_rating(self):
return Child.objects.filter(parent=self).aggregate(Avg('rating'))
class Child(models.Model):
name = models.CharField(...)
parent = models.ForeignKey(Parent)
rating = models.IntegerField(default=0)
I plan on accessing avg_child_rating often. Would it be optimizing if I did the following:
我打算经常访问avg_child_rating。如果我执行以下操作,是否会进行优化:
class Parent(models.Model):
...
num_children = models.IntegerField(default=0)
avg_child_rating = models.FloatField(default=0.0)
def update_parent_child_stats(sender, instance, **kwargs):
num_children = Child.objects.filter(parent=instance.parent)
if instance.parent.num_children != num_children:
instance.parent.num_children = num_children
instance.parent.avg_child_rating = Child.objects.filter(instance.parent=self).aggregate(Avg('rating'))
post_save.connect(update_parent_child_stats, sender=Child)
post_delete.connect(update_parent_child_stats, sender=Child)
The difference now is that every time a child is created/rated/deleted, the Parent object is updated. I know that the created/rating will be done often.
现在的不同之处在于,每次创建/评级/删除子项时,都会更新Parent对象。我知道创建/评级会经常完成。
What's more expensive?
什么更贵?
1 个解决方案
#1
Depends on the scale of the problem. If you anticipate a lot of write traffic, this might be an issue. It's much harder to scale writes than reads (replicate, caching etc.) That said, you can probably going a long way without this extra query causing you any problems.
取决于问题的规模。如果您预计会有大量写入流量,这可能是一个问题。缩放写入比读取(复制,缓存等)要困难得多。也就是说,如果没有这个额外的查询可能会导致任何问题,您可能会走很长的路。
Depending on how up-to-date your stats must be you could have some other process (non-web session) come through and update these stats nightly.
根据您的统计数据必须是最新的,您可以通过其他进程(非网络会话)来获取并每晚更新这些统计数据。
#1
Depends on the scale of the problem. If you anticipate a lot of write traffic, this might be an issue. It's much harder to scale writes than reads (replicate, caching etc.) That said, you can probably going a long way without this extra query causing you any problems.
取决于问题的规模。如果您预计会有大量写入流量,这可能是一个问题。缩放写入比读取(复制,缓存等)要困难得多。也就是说,如果没有这个额外的查询可能会导致任何问题,您可能会走很长的路。
Depending on how up-to-date your stats must be you could have some other process (non-web session) come through and update these stats nightly.
根据您的统计数据必须是最新的,您可以通过其他进程(非网络会话)来获取并每晚更新这些统计数据。