#30991 closed New feature (wontfix)
Optional removal of transaction and extra get() in get_or_create().
Reported by: | Michael Korobov | Owned by: | nobody |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | dev |
Severity: | Normal | Keywords: | get_or_create |
Cc: | Triage Stage: | Unreviewed | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
Our app's subject is accounting and it's processing a huge amount of different data by a large amount of users. Hence the perfomance takes crucial part in this app. We've been optimizing our code recently and discovered that get_or_create
method of QuerySet
is generating an atomic block with a savepoint, and then makes another query to a database in case of IntegrityError
exception. While this is really great on it's own, it slows down the app.
The solution in attached PR is aimed to allow us to make a choice - whether to use get_or_created
as it currently is, or skip the creation of an atomic block and avoid another possible hit to a database. New kwarg to get_or_create
raw=False
if set to True
ignores both of mentioned components, making the method run faster. And if needed we can handle IntegrityError
or make an atomic block where we indeed need it by ourselves.
Change History (4)
comment:1 by , 5 years ago
comment:2 by , 5 years ago
Resolution: | → wontfix |
---|---|
Status: | new → closed |
Summary: | Optional removal of transaction and extra `get()` in `get_or_create` → Optional removal of transaction and extra `get()` in `get_or_create`. |
Type: | Cleanup/optimization → New feature |
Version: | 2.2 → master |
Thanks for this ticket, however this use case is quite niche, IMO. I don't think that we want to encourage users for such "unsafe" behavior. get_or_create()
is a sensitive method with few warnings (see documentation) we shouldn't make it more complicated. Moreover you should be able to easily add such hook to your app.
You can start a discussion on the DevelopersMailingList if you don't agree.
comment:3 by , 5 years ago
Summary: | Optional removal of transaction and extra `get()` in `get_or_create`. → Optional removal of transaction and extra get() in get_or_create(). |
---|
comment:4 by , 5 years ago
Thanks for your time and a reply. We will be keeping these changes as a patch in our app.
Pull Request
This has been approved by my teamleader, and the Django Test Suite passed successfully.