#36490: Avoid unnecessary transaction in QuerySet.bulk_create
-------------------------------------+-------------------------------------
Reporter: Simon | Owner: Simon Charette
Charette |
Type: | Status: assigned
Cleanup/optimization |
Component: Database | Version: 5.2
layer (models, ORM) |
Severity: Normal | Keywords:
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
The QuerySet.bulk_create method creates an unnecessary transaction when it
can perform the insert in a singe `INSERT` when the following criteria are
met
1. There's no mix of objects with and without primary key (this could
potentially be optimized to be done in a single query but that's another
can of worm)
2. The objects fit in a single batch with respect to the provided
`batch_size` and `connection.ops.bulk_batch_size`
This is unnecessary overhead we've optimized in other areas of the code
base (e.g. `Model.save`) which seems warranted in this case particularly
because `bulk_create` offers a more featureful interface than `create`
[
https://johnnymetz.com/posts/postgresql-failing-insert/ which can be used
to insert a small number of rows].
--
Ticket URL: <
https://code.djangoproject.com/ticket/36490>
Django <
https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.