#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.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/010701833cbcb6ac-d5806c9f-d7f9-4cf3-b199-25f54d6bdce4-000000%40eu-central-1.amazonses.com.