I am using Django 1.4, Python 2.7, Ubuntu 14.04 and PostgreSQL 9.3.
I have two models that are linked together by a many-to-many relationship (M2M). I get major filtering performance issues in relation to M2M relationships.
class Meat(models.Model):
name1 = models.CharField(max_length=200)
name2 = models.CharField(max_length=200)
class Potato(models.Model):
bad_meats = models.ManyToManyField(
Meat, null=True, blank=True, related_name="bad_potatoes")
I connect filters in a set of requests for meat.
potato = Potato.objects.get(pk=12345)
qs = Meat.objects.all()
qs = qs.filter(name1='foo')
qs = qs.filter(name2='bar')
qs = qs.exclude(id__in=potato.bad_meats.all())
Using __insignificantly slows down this filtering process. Is there any other approach anyone can suggest to speed this up?
I cannot use the query .raw()because I need to bind filters during this process depending on certain conditions.
, Meat 150 000 , potato.bad_meats.all() - 40 000 . 8-10 . , 1 .