[Django] #34011: Implement bulk_create auto-incrementing pk assignment on MySQL when possible

10 views
Skip to first unread message

Django

unread,
Sep 14, 2022, 12:02:27 PM9/14/22
to django-...@googlegroups.com
#34011: Implement bulk_create auto-incrementing pk assignment on MySQL when
possible
-------------------------------------+-------------------------------------
Reporter: Simon | Owner: nobody
Charette |
Type: New | Status: new
feature |
Component: Database | Version: 4.0
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 |
-------------------------------------+-------------------------------------
[https://www.godaddy.com/engineering/2022/09/12/rails-bulk-insert-mysql/
From this article] it seems that it might be possible to implement
`AutoField` (and friends) assignment on MySQL for `bulk_create` by using a
range of `(last_insert_id(), last_insert_id() + affected_rows)` .

[https://dev.mysql.com/doc/refman/8.0/en/innodb-auto-increment-
handling.html The criteria are the following]

> Use simple-inserts (e.g. no subquery or insert from select) and don't
mix objects with primary keys and ones without.
`bulk_create` doesn't rely on subqueries and
[https://github.com/django/django/blob/6220c445c40a6a7f4d442de8bde2628346153963/django/db/models/query.py#L796-L822
it already partitions between objects with and without a primary key].

> Don't use `innodb_autoinc_lock_mode=2` (interleaved) with
`binlog_format=STATEMENT`
This is detectable by issuing introspection just like we do with other
`connection.features`.

The default on MySQL 5.7 was `(innodb_autoinc_lock_mode=1,
binlog_format=STATEMENT)` and is `(innodb_autoinc_lock_mode=2,
binlog_format=ROW)` on MySQL 8 which both ensures that ''auto-increment
values are assigned in a predictable and repeatable order''
[https://dev.mysql.com/doc/refman/8.0/en/innodb-
parameters.html#sysvar_innodb_autoinc_lock_mode as explained in the docs]
and thus allows for this feature to be used in setups using default InnoDB
configurations.

--
Ticket URL: <https://code.djangoproject.com/ticket/34011>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Sep 15, 2022, 2:15:30 AM9/15/22
to django-...@googlegroups.com
#34011: Implement bulk_create auto-incrementing pk assignment on MySQL when
possible
-------------------------------------+-------------------------------------
Reporter: Simon Charette | Owner: nobody
Type: New feature | Status: closed
Component: Database layer | Version: 4.0
(models, ORM) |
Severity: Normal | Resolution: duplicate
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* status: new => closed
* resolution: => duplicate


Comment:

Thanks, it's worth trying. I think we can close it as a duplicate of
#19527.

--
Ticket URL: <https://code.djangoproject.com/ticket/34011#comment:1>

Reply all
Reply to author
Forward
0 new messages