PhoebeB
PhoebeB

Reputation: 8570

Django many to many performance issue

I've profiled a piece of code that processes an excel spreadsheet of 350 rows and is about 200 lines long. 75.5% of the time is spent on this many to many call:

332       388    117059833 301700.6     75.5                      if obj.wave not in c.waves.all():

and here is most of the rest:

136        97       341749   3523.2      0.2                      q.waves.add(obj.wave)
137        97     30551176 314960.6     19.7                      q.save()

wave is a model, obj has a foreign key field to wave

wave = models.ForeignKey(Wave)

and q has a many to many field to wave

waves = models.ManyToManyField(Wave)

I've checked the many to many table and there is a key on every field. I've changed it from ISAM to Innodb with no difference.

Is there anything I can do to improve performance - it's currently taking 20 secs per line and using all the CPU on a 2 core linux box - before I rewrite the code to get rid of the many to many structure?

Upvotes: 1

Views: 135

Answers (1)

Aamir Rind
Aamir Rind

Reputation: 39649

The in operation can be costly specially if there are so many waves record, you can simplify your query as:

if not c.waves.filter(id__exact=obj.wave.id).exists(): pass # do something

Upvotes: 3

Related Questions