Opened 5 years ago

Closed 5 years ago

Last modified 5 years ago

#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 Michael Korobov, 5 years ago

Pull Request
This has been approved by my teamleader, and the Django Test Suite passed successfully.

comment:2 by Mariusz Felisiak, 5 years ago

Resolution: wontfix
Status: newclosed
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/optimizationNew feature
Version: 2.2master

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 Mariusz Felisiak, 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 Michael Korobov, 5 years ago

Thanks for your time and a reply. We will keep these changes as a patch in our app.

Last edited 5 years ago by Michael Korobov (previous) (diff)
Note: See TracTickets for help on using tickets.
Back to Top