> Den 12/08/2015 kl. 20.00 skrev [email protected]:
> 
> In the actually code I create and preload all the DataPoints and Sensors 
> outside the loop.  I found a dict was too slow for DataPoints.

That's suspicious. Compared to loading data from the database, Python dicts are 
not slow, for any reasonable value of slow.

> Can you explain what you mean by “If you need better bulk-insert performance 
> than this, you can convert the m2m relation between Sensor and DataPoint to 
> an explicit m2m model. You can then bulk-insert all m2m relations in one go 
> instead of per-object”?   I’m not sure how to implement this.

Create an intermediate model for the m2m relation as described in 
https://docs.djangoproject.com/en/1.8/topics/db/models/#extra-fields-on-many-to-many-relationships

You can then bulk_create() on this model instead of using add(). Something like:


  class Datapoint(models.Model):
      sensors = models.ManyToManyField(Sensor, through='DatapointSensorRel')


  class DatapointSensorRel(models.Model):
      datapoint = models.ForeignKey(Datapoint)
      sensor = models.ForeignKey(Sensor)


Used like this:

  relations = [DatapointSensorRel(datapoint=d, sensor=s) for d, s in 
my_collected_relations]
  DatapointSensorRel.objects.bulk_create(relations)


Erik

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/E9071BDE-2AD3-4D77-8D9F-0ABD1791CDDB%40cederstrand.dk.
For more options, visit https://groups.google.com/d/optout.

Reply via email to