Breaking change: Improve save() performance by skipping index creation #2702
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Indexes are now only created when a Model is first used, eg the first call to save() or on the first call to _get_collection(), or when the new meta["auto_create_index_on_save"] option is set to True. This is a minor breaking change for some applications. As a workaround apps can explicitly call ensure_indexes() or set meta["auto_create_index_on_save"] to True.
Note that with the default settings (auto_create_index=True + auto_create_index_on_save=False) indexes are still created after on a Document's first use, or first use after Document.drop_collection().
I hope this PR can finally resolve #1446. I reviewed that issue, the previous attempts to fix it (#1457 and #1511), and the original issue that added this behavior (#812) and I believe the solution in this PR is a good compromise between having better default behavior while also offering an escape hatch for the old behavior (via auto_create_index_on_save).
Here are the benchmark results:
4.72/2.78 = a significant 1.7x speed up for save() on a document with one field and one index.
Note: I've made some relatively minor changes to the other benchmarks as well, mainly just to explicitly use w=1 write concern. This change significantly improves the runtime (and the effectiveness) of the benchmarks, otherwise they end up using the server's default writeConcern which is now w:majority. I can move this to a new PR if desired.