django - 这个查询集循环是最优的吗?

标签 django postgresql django-queryset

如果不是,我可以做些什么来改进它?目前,仅使用我们的开发数据进行评估就需要 13 秒。原始 SQL 的想法?

    suppliers = models.Supplier.objects.all().order_by('company')
    for supplier in suppliers :
        sup = {}
        sup['company'] = supplier.company
        sup['supplies'] = get_supplies(1, supplier.uuid)
        sup['category'] = 'Supplier'
        if isocode == None :
            addresses = models.Address.objects.filter(company = supplier.company).iterator()
        else :
            addresses = models.Address.objects.filter(company = supplier.company, country_iso = isocode).iterator()
        sup['contacts'] = list(models.Contact.objects.filter(address__in=addresses))
        company_list.append(sup)

------------------------------------------------------------ -------------------------------------------

class SupplierManager(models.Manager):
    def suppliers_for_panel(self, bought_in_control_panel_id):
        return self.filter(supplies__bought_in_control_panel__id = bought_in_control_panel_id).filter(company__hidden=0).order_by('company__name')

class Supplier(models.Model):
    uuid = UUIDField(primary_key=True)
    company  = models.ForeignKey(Company, db_column='company_uuid',null=True, blank=True)
    sector = models.ForeignKey(CustomerSector, db_column='sector_uuid',null=True, blank=True,verbose_name=_('Sector'))
    account_number = models.CharField(null=True, blank=True,max_length=255,verbose_name=_('Account No'))
    reference  = models.CharField(null=True, blank=True,max_length=255)
    notes = models.TextField(null=True, blank=True) 
    date_created = models.DateTimeField(null=True, blank=True, auto_now_add=True)
    date_modified = models.DateTimeField(null=True, blank=True, auto_now=True)
    user = UserField(null=True, blank=True)
    jms_code = models.CharField(null=True, blank=True,max_length=255)

    objects = SupplierManager()

    def __unicode__(self):
        return self.company.name

    class Meta:
        db_table = 'supplier'

------------------------------------------------------------ -------------------------------------------

class Contact(models.Model):

    uuid = UUIDField(primary_key=True)
    address = models.ForeignKey(Address, db_column='address_uuid',null=True,blank=True,verbose_name=_('Address'))
    title = models.ForeignKey(Title,db_column='title_uuid',null=True, blank=True)
    forename = models.CharField(null=True, blank=True,max_length=255)
    surname = models.CharField(null=True, blank=True,max_length=255)
    position = models.CharField(null=True, blank=True,max_length=255)
    mobile = models.CharField(null=True, blank=True,max_length=255)
    direct_line = models.CharField(null=True, blank=True,max_length=255)
    email = models.CharField(null=True, blank=True,max_length=255)
    origin = models.IntegerField(null=True, blank=True)
    lead_source = models.IntegerField(null=True, blank=True)
    notes = models.TextField(null=True, blank=True)
    contact_status = models.ForeignKey(ContactStatus, db_column='contact_status_uuid',verbose_name=_('Contact Status'))
    contact_method = models.ForeignKey(ContactMethod, db_column='contact_method_uuid',verbose_name=_('Contact Method'))
    date_created = models.DateTimeField(null=True, blank=True, auto_now_add=True)
    date_modified = models.DateTimeField(null=True, blank=True, auto_now=True)
    user = UserField(null=True, blank=True)
    jms_code = models.CharField(null=True, blank=True,max_length=255)
    allow_download = models.NullBooleanField(serialize=False)
    is_modified = ModifiedField(serialize=False)

    def __unicode__(self):
        return self.get_full_name()

    def get_full_name(self):
        return self.forename + " " + self.surname

    class Meta:
        db_table = 'contact'

------------------------------------------------------------ -------------------------------------------

class Address(models.Model):
    uuid = UUIDField(primary_key=True)
    company = models.ForeignKey(Company, db_column='company_uuid',null=True,blank=True,verbose_name=_('Address'))
    group_name = models.CharField(null=True, blank=False,max_length=255,verbose_name=_('Corporate Group'))
    line1 = models.CharField(null=True, blank=False,max_length=255,verbose_name=_('Address Line 1'))
    line2 = models.CharField(null=True, blank=True,max_length=255,verbose_name=_('Address Line 2'))
    line3 = models.CharField(null=True, blank=True,max_length=255,verbose_name=_('Address Line 3'))
    town = models.CharField(null=True, blank=True,max_length=255)
    county = models.CharField(null=True, blank=True,max_length=255)
    postcode = models.CharField(null=True, blank=True,max_length=255)
    country_iso = models.CharField(null=True, blank=True,max_length=255)
    telephone = models.CharField(null=True, blank=True,max_length=255)
    fax = models.CharField(null=True, blank=True,max_length=255)
    email = models.CharField(null=True, blank=True,max_length=255)
    website = models.CharField(null=True, blank=True,max_length=255)
    description = models.CharField(null=True, blank=True,max_length=255)
    date_created = models.DateTimeField(null=True, blank=True, auto_now_add=True)
    date_modified = models.DateTimeField(null=True, blank=True, auto_now=True)
    user = UserField(null=True, blank=True)
    jms_code = models.CharField(null=True, blank=True,max_length=255)
    allow_download = models.NullBooleanField(serialize=False)
    notes = models.CharField(null=True, blank=True,max_length=255)    
    is_modified = ModifiedField(serialize=False)

    def __unicode__(self):
        if self.description in [ '',  None ] :
            if self.line1 not in [ '', None ] :
                return self.line1
            return self.uuid
        return self.description

    def asList (self) :
        return [ b for b in self.line1, self.line2, self.line3, self.town, self.county if b not in ('', None) ]

    class Meta:
        db_table = 'address'
        verbose_name_plural = ('Addresses')

------------------------------------------------------------ -------------------------------------------

最佳答案

首先,将 .select_lated('company') 添加到您的初始供应商查询中。现在,每次在循环中访问 supplier.company 时,您都会发出额外的查询。

查询膨胀的其余部分来自于单独选择地址联系人。这是每次循环时都必须发出的另外两个查询。如果您使用的是 Django 1.4+,您可以尝试使用 prefetch_related把它们剪掉。如果您运行的是较低版本,可以尝试django-batch-select这为您提供了类似的功能。可能需要重新思考您的方法,找出一种一次性选择所有内容的方法。

关于django - 这个查询集循环是最优的吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11393545/

相关文章:

mysql - 缩放自动增量

postgresql - 在哪里可以下载用于Postgresql的Northwind数据库?

django - 如何在 django View 中包含多个查询集?

python - 如何从 Django 项目中删除南方

django - 将 OSQA 数据库恢复到初始状态

python - Django 调试工具栏仅适用于管理部分

regexp_replace 负前瞻清理电子邮件地址

django - 注释 Mptt 模型的下降总数

django-admin - Django ModelAdmin 查询集覆盖不起作用

Django 查询集过滤器空 JSONField