Opened 2 years ago
Closed 2 years ago
#34011 closed New feature (duplicate)
Implement bulk_create auto-incrementing pk assignment on MySQL when possible
Reported by: | Simon Charette | Owned by: | nobody |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | 4.0 |
Severity: | Normal | Keywords: | |
Cc: | Triage Stage: | Unreviewed | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
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)
.
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 it already partitions between objects with and without a primary key.
Don't use
innodb_autoinc_lock_mode=2
(interleaved) withbinlog_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 as explained in the docs and thus allows for this feature to be used in setups using default InnoDB configurations.
Thanks, it's worth trying. I think we can close it as a duplicate of #19527.