您是否知道如何优化(时间)此查询?即使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
models.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")
答案 0 :(得分:1)
您正在获取m2m相关对象,因此您应该在查询中使用prefetch_related
来优化它。