1

你知道如何优化(时间)这个查询吗?即使 wordpress 人口在 1000 人左右,它也很慢。

询问:

al = Wordpress.objects.all().order_by('?')
zaplecza = Wordpress.objects.none()
for l in labels.split(","):
    zaplecza = zaplecza | al.filter(label = l)    
zaplecza = zaplecza.exclude(wordpress__url = project_name) <--very slow

模型.py

class Wordpress(models.Model):
    url = models.CharField(max_length = 1000)
    login = models.CharField(max_length = 1000, default = "admin")
    password = models.CharField(max_length = 1000, default = "perkoz")
    label = models.CharField(max_length = 1000)
    cms = models.CharField(max_length = 1000)


class Project(models.Model):
    url = models.CharField(max_length = 1000)
    links = models.TextField(blank = True, null = True, verbose_name = "Linki do wpisów")
    wordpress = models.ManyToManyField(Wordpress, related_name = "wordpress", verbose_name = "Dodane do zaplecz")

    main_link = models.CharField(max_length = 1000)
    dir_links = models.TextField(blank = True, null = True, verbose_name="Anchory")
    blog_links = models.TextField(blank = True, null = True, verbose_name="Lista linków i anchorów z ;;;")
    category = models.ForeignKey(Category, related_name = "projekt")
    kategorie = models.CharField(max_length = 1000)
    labels = models.CharField(max_length = 1000)
    tagi = models.CharField(max_length = 1000)
    auto_tekst = models.BooleanField(verbose_name="Auto tekst")
    arts = models.TextField(blank = True, null = True, verbose_name="Artykuły")
    title = models.TextField(blank = True, null = True, verbose_name="Tytuł")
    no = models.IntegerField(blank = True, null = True, verbose_name="Liczba wpisów")
    last_added = models.DateTimeField(blank = True, null = True, verbose_name="Ostatnio dodane")
4

1 回答 1

1

您正在获取 m2m 相关对象,因此您应该prefetch_related在查询中使用它来优化它。

https://docs.djangoproject.com/en/dev/ref/models/querysets/#django.db.models.query.QuerySet.prefetch_related

于 2013-09-07T09:27:46.553 回答