Django admin在mysql中进行了大量的重复查询

时间:2016-03-11 14:02:02

标签: python mysql django django-admin

我遇到了一个问题。

当我尝试打开更改页面以查看元素中的参数(如djangosite.com/admin/djangoapp/someelement/1/change/)时,它加载速度非常慢(10-15秒)。

我发现Django正在制作大量重复的查询:

更新 我认为我在admin.py中犯了错误。

   inlines = [PhoneInline,FlatInline,NeedInline]

如果我删除上面的行,一切都很好。

mysql log

2161 Query  set autocommit=0
         2161 Query SELECT `ha_phone`.`id`, `ha_phone`.`phone_number`, `ha_phone`.`phone_owner_id` FROM `ha_phone` WHERE `ha_phone`.`id` = 262
         2161 Query SELECT `django_content_type`.`id`, `django_content_type`.`app_label`, `django_content_type`.`model` FROM `django_content_type` WHERE (`django_content_type`.`model` = 'phone' AND `django_content_type`.`app_label` = 'MYAPP')
         2161 Query commit
         2161 Query set autocommit=1
         2161 Query SELECT `ha_owner`.`id`, `ha_owner`.`owner_pub_date`, `ha_owner`.`owner_name`, `ha_owner`.`owner_verify`, `ha_owner`.`owner_company_id` FROM `ha_owner` WHERE `ha_owner`.`id` = 236
         2161 Query SELECT `ha_company`.`id`, `ha_company`.`company_name` FROM `ha_company` WHERE `ha_company`.`id` = 1
         2161 Query SELECT `ha_owner`.`id`, `ha_owner`.`owner_pub_date`, `ha_owner`.`owner_name`, `ha_owner`.`owner_verify`, `ha_owner`.`owner_company_id` FROM `ha_owner`
         2161 Query SELECT `ha_company`.`id`, `ha_company`.`company_name` FROM `ha_company` WHERE `ha_company`.`id` = 1
         2161 Query SELECT `ha_company`.`id`, `ha_company`.`company_name` FROM `ha_company` WHERE `ha_company`.`id` = 1
         2161 Query SELECT `ha_company`.`id`, `ha_company`.`company_name` FROM `ha_company` WHERE `ha_company`.`id` = 1
         2161 Query SELECT `ha_company`.`id`, `ha_company`.`company_name` FROM `ha_company` WHERE `ha_company`.`id` = 1
         2161 Query SELECT `ha_company`.`id`, `ha_company`.`company_name` FROM `ha_company` WHERE `ha_company`.`id` = 1
         2161 Query SELECT `ha_company`.`id`, `ha_company`.`company_name` FROM `ha_company` WHERE `ha_company`.`id` = 1
         2161 Query SELECT `ha_company`.`id`, `ha_company`.`company_name` FROM `ha_company` WHERE `ha_company`.`id` = 1
         2161 Query SELECT `ha_company`.`id`, `ha_company`.`company_name` FROM `ha_company` WHERE `ha_company`.`id` = 1
         2161 Query SELECT `ha_company`.`id`, `ha_company`.`company_name` FROM `ha_company` WHERE `ha_company`.`id` = 1

...

models.py

class House(models.Model):
    house_number = models.CharField(max_length=10, verbose_name="Номер дома")
    house_floors = models.PositiveSmallIntegerField(verbose_name="Число этажей", null=True)
    house_walls = models.ForeignKey(Walls, verbose_name="Материал стен", null=True)
    house_street = models.ForeignKey(Street, verbose_name="Улица")
    house_metro = models.ForeignKey(Metro, verbose_name="Станция Метро", null=True)
    house_block = models.ForeignKey(Block, verbose_name="Микрорайон", null=True)


    class Meta:
        verbose_name = 'Дом'
        verbose_name_plural = 'Дома'
        ordering = ['house_street','house_number']


    def __unicode__(self):
        return unicode(self.house_street.street_name) + " " + unicode(self.house_number)

class Flat(models.Model):
    flat_number = models.CharField(max_length=7, verbose_name="Номер квартиры", null=True)
    flat_rooms = models.PositiveSmallIntegerField(verbose_name="Число комнат")
    flat_total_sq = models.PositiveSmallIntegerField(verbose_name="Общая площадь")
    flat_life_sq = models.PositiveSmallIntegerField(verbose_name="Жилая площадь")
    flat_kitchen_sq = models.PositiveSmallIntegerField(verbose_name="Кухонная площадь")
    flat_floors = models.PositiveSmallIntegerField(verbose_name="Этаж")
    flat_house = models.ForeignKey(House, verbose_name="Дом")
    flat_owner = models.ForeignKey(Owner, verbose_name="Владелец")
    flat_comment = models.TextField(verbose_name="Комметарий", null=True)
    flat_price = models.PositiveIntegerField(verbose_name='Цена квартиры')
...    
class Owner(models.Model):
        owner_pub_date = models.DateField(default=None,null=True,verbose_name="Дата публикации")
        owner_name = models.CharField(max_length=150, verbose_name="ФИО владельца")
        owner_verify = models.BooleanField(verbose_name="Пользователь верифицирован?")
        owner_company = models.ForeignKey(Company, verbose_name="Компания")

        class Meta:
            verbose_name = 'Владельца'
            verbose_name_plural = 'Владельцы'

        def __unicode__(self):
            return unicode(self.id) + " | "+ self.owner_name + " " + unicode(self.owner_company)


    class Phone(models.Model):
        phone_number = models.CharField(max_length=25, verbose_name="Номер телефона")
        phone_owner = models.ForeignKey(Owner, verbose_name="Владелец телефона")

        class Meta:
            verbose_name = 'Телефон'
            verbose_name_plural = 'Телефоны'

        def __unicode__(self):
            return unicode(self.phone_owner) + " " + self.phone_number

admin.py

...        
class PhoneInline(admin.StackedInline):
            model = Phone
            extra = 2

        class FlatInline(admin.TabularInline):
            model = Flat
            extra = 0

        class NeedInline(admin.TabularInline):
            model = Need
            extra = 1

        class OwnerAdmin(admin.ModelAdmin):
            field=['owner_name']
            inlines = [PhoneInline,FlatInline,NeedInline]

    admin.site.register(Owner,OwnerAdmin)

我该怎么办? 非常感谢你。

更新 @Alasdair,谢谢。使用ha_company的查询减少了。但我和ha_street有另外一个重复。看起来像这样:

 2601 Query SELECT `ha_company`.`id`, `ha_company`.`company_name` FROM `ha_company`
         2601 Query SELECT `ha_house`.`id`, `ha_house`.`house_number`, `ha_house`.`house_floors`, `ha_house`.`house_walls_id`, `ha_house`.`house_street_id`, `ha_house`.`house_metro_id`, `ha_house`.`house_block_id`, `ha_house`.`house_age`, `ha_house`.`house_flat`, `ha_house`.`house_quality` FROM `ha_house` INNER JOIN `ha_street` ON (`ha_house`.`house_street_id` = `ha_street`.`id`) ORDER BY `ha_street`.`street_name` ASC, `ha_house`.`house_number` ASC
         2601 Query SELECT `ha_street`.`id`, `ha_street`.`street_name` FROM `ha_street` WHERE `ha_street`.`id` = 74
         2601 Query SELECT `ha_street`.`id`, `ha_street`.`street_name` FROM `ha_street` WHERE `ha_street`.`id` = 74
         2601 Query SELECT `ha_street`.`id`, `ha_street`.`street_name` FROM `ha_street` WHERE `ha_street`.`id` = 36
         2601 Query SELECT `ha_street`.`id`, `ha_street`.`street_name` FROM `ha_street` WHERE `ha_street`.`id` = 582
         2601 Query SELECT `ha_street`.`id`, `ha_street`.`street_name` FROM `ha_street` WHERE `ha_street`.`id` = 582

1 个答案:

答案 0 :(得分:6)

sudo ionic platforms 模型的__unicode__方法包含Phone,后者又访问unicode(self.phone_owner)外键。这意味着Django必须在内联中为每个电话对象查找公司。

您通过删除内联来解决问题,但如果您想保留内联,则可以:

  1. 更改owner_company__unicode__的{​​{1}}方法,以便不包含该公司
  2. 覆盖内联的Phone方法,以便您可以使用Owner

    get_queryset