Opened 11 years ago
Closed 7 years ago
#20880 closed Cleanup/optimization (fixed)
Remove non-cloning logic from Query.clone() and QuerySet._clone()
Reported by: | Anssi Kääriäinen | Owned by: | Tim Graham |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | dev |
Severity: | Normal | Keywords: | |
Cc: | Triage Stage: | Accepted | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description (last modified by )
QuerySet and Query clone() methods are doing more than just cloning, they are also doing set up for next operation, changing the class of the clone if requested, and even throwing away some attribute values.
To make the code a bit more readable the clone() method should be splitted to clone() part and pre_next_op() part. This also allows creating inplace querysets that work precisely like full-clone querysets. A queryset is called inplace if it doesn't do cloning at all between operations. This again allows speeding up certain internal operations like model.save() and prefetch_related(), and also allows users to optimise heavy-to-create querysets by avoiding clone() overhead.
Patch available from https://github.com/akaariai/django/tree/splitted_clone. The inplace queryset used for Model.save_base() speeds up model.save() by about 20% for a simple m = Model.objects.get(); m.save() benchmark. There are other places where similar optimisations could be used. Prefetch speedup is tracked in ticket #20577.
Change History (8)
comment:1 by , 11 years ago
Description: | modified (diff) |
---|---|
Triage Stage: | Unreviewed → Accepted |
comment:2 by , 11 years ago
comment:3 by , 8 years ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
I've been rebasing Anssi's patch and will post a PR when it's polished. If I understand correctly, Josh proposed a similar idea to inplace()
.
comment:4 by , 7 years ago
Has patch: | set |
---|
Here's a PR without the "inplace" stuff. I think we can leave that to a separate ticket/PR.
comment:5 by , 7 years ago
Summary: | Split clone() to clone() and pre_next_op() → Remove non-cloning logic from Query.clone() and QuerySet._clone() |
---|
#28455 is the follow up ticket for adding "inplace" querysets.
comment:8 by , 7 years ago
Resolution: | → fixed |
---|---|
Status: | assigned → closed |
I updated (force-pushed) to https://github.com/akaariai/django/tree/splitted_clone. The patch is pretty good (cleanup needed), and shows promising results for model saving and some other queryset operations:
For qs_filter_chaining (~5 .filter() calls) using inplace results in speedup of:
This is by no means complex query, yet it shows promising results.
So, it is worth considering if .inplace() should be public API. If so, then it would make sense to guard against using stale versions of the queryset. This is doable by cloning the outer QuerySet. Skipping cloning of the internal Query, should give most of the speedup.
Another consideration is that if the ._clone() should be renamed to ._copy() or something. The patched version ._clone() does subtly different thing than the old version, so this might cause some grey hairs for those who use that internal API.