Skip to content

Commit

Permalink
[2.2.x] Refs #30656 -- Reorganized bulk methods in the database optim…
Browse files Browse the repository at this point in the history
…ization docs.

Backport of fe33fdc from master
  • Loading branch information
MisterRios authored and felixxm committed Jul 29, 2019
1 parent ea57c8a commit b4139ed
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions docs/topics/db/optimization.txt
Original file line number Diff line number Diff line change
Expand Up @@ -341,8 +341,13 @@ it on a ``QuerySet`` by calling

Adding an index to your database may help to improve ordering performance.

Insert in bulk
==============
Use bulk methods
================

Use bulk methods to reduce the number of SQL statements.

Create in bulk
--------------

When creating objects, where possible, use the
:meth:`~django.db.models.query.QuerySet.bulk_create()` method to reduce the
Expand All @@ -362,8 +367,13 @@ Note that there are a number of :meth:`caveats to this method
<django.db.models.query.QuerySet.bulk_create>`, so make sure it's appropriate
for your use case.

This also applies to :class:`ManyToManyFields
<django.db.models.ManyToManyField>`, so doing::
Insert in bulk
--------------

When inserting objects into :class:`ManyToManyFields
<django.db.models.ManyToManyField>`, use
:meth:`~django.db.models.fields.related.RelatedManager.add` with multiple
objects to reduce the number of SQL queries. For example::

my_band.members.add(me, my_friend)

Expand Down

0 comments on commit b4139ed

Please sign in to comment.