-
Notifications
You must be signed in to change notification settings - Fork 494
Description
Describe the bug
Running bulk_create_with_history on a database that doesn't support returning ids from bulk_create results in an n+1 query due to all the additional one-off .filter() calls it's making
I believe this was originally needed to maintain the order from the original list, but it should be possible to use the same CASE/WHEN trick that Django uses for bulk_update to bulk resolve ids in the same order as our original list
https://github.com/django/django/blob/4.0.4/django/db/models/query.py#L624-L638
https://stackoverflow.com/a/19196522/1960509
To Reproduce
Steps to reproduce the behavior:
- Create a model
- Run a script with
--print-sqlon and bulk create with more than 1 model on a database that lacks support for returning multiple ids frombulk_create - See more queries than expected
Expected behavior
1 query for bulk creation (INSERT INTO normal table) + 1 query for retrieving ids (SELECT WHERE) + 1 query for bulk history creation (INSERT INTO)
Actual behavior
1 query for bulk creation (INSERT INTO normal table) + n query for retrieving ids (SELECT WHERE * n) + 1 query for bulk history creation (INSERT INTO history table)
Environment (please complete the following information):
Not willing to share at this time, I'll file iterate within django-simple-history test environment instead