我有一个返回一些关于电子邮件列表增长的统计数据的视图。涉及的模型有:
模型.py
class Contact(models.Model):
email_list = models.ForeignKey(EmailList, related_name='contacts')
customer = models.ForeignKey('Customer', related_name='contacts')
status = models.CharField(max_length=8)
create_date = models.DateTimeField(auto_now_add=True)
class EmailList(models.Model):
customers = models.ManyToManyField('Customer',
related_name='lists',
through='Contact')
class Customer(models.Model):
is_unsubscribed = models.BooleanField(default=False, db_index=True)
unsubscribe_date = models.DateTimeField(null=True, blank=True, db_index=True)
在视图中,我正在做的是遍历所有 EmailLists 对象并获取一些指标:以下方式:
视图.py
class ListHealthView(View):
def get(self, request, *args, **kwargs):
start_date, end_date = get_dates_from_querystring(request)
data = []
for email_list in EmailList.objects.all():
# historic data up to start_date
past_contacts = email_list.contacts.filter(
status='active',
create_date__lt=start_date).count()
past_unsubscribes = email_list.customers.filter(
is_unsubscribed=True,
unsubscribe_date__lt=start_date,
contacts__status='active').count()
past_deleted = email_list.contacts.filter(
status='deleted',
modify_date__lt=start_date).count()
# data for the given timeframe
new_contacts = email_list.contacts.filter(
status='active',
create_date__range=(start_date, end_date)).count()
})
return Response({'data': data})
现在这工作正常,但随着 My DB 开始增长,此视图的响应时间超过 1 秒,并且偶尔会导致数据库中长时间运行查询。我认为最明显的改进是索引EmailList.customers,但我认为它可能需要一个复合索引?另外,有没有更好的方法来做到这一点?也许使用聚合?
慕姐8265434
相关分类