Opened 17 years ago
Closed 13 years ago
#10399 closed Cleanup/optimization (fixed)
Optimise cross-table update for one-to-one fields
| Reported by: | Malcolm Tredinnick | Owned by: | Simon Charette |
|---|---|---|---|
| Component: | Database layer (models, ORM) | Version: | 1.0 |
| Severity: | Normal | Keywords: | |
| Cc: | Triage Stage: | Ready for checkin | |
| Has patch: | yes | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description
When we do an update involving a OneToOneField (model inheritance cases being an obvious example), the update queryset does something akin to filter(pk__in=inner_query) where inner_query describes the child model. However, the field in this particular case is known to be one-to-one, not many-to-one. So the reverse relation will only match one item. Thus, we might as well do filter(pk=some_value), which is much faster.
Detecting this case can probably be handled in UpdateQuery.add_update_fields() in some fashion, I suspect.
(This is another one of those long-term SQL optimisation projects that I'll get to at some point. Making a tracking bug for it, since it's been floating around for a while.)
Attachments (3)
Change History (16)
comment:1 by , 17 years ago
| Triage Stage: | Unreviewed → Accepted |
|---|
comment:2 by , 15 years ago
| Severity: | → Normal |
|---|---|
| Type: | → Cleanup/optimization |
comment:5 by , 13 years ago
| Easy pickings: | set |
|---|---|
| Needs tests: | set |
I believe this one is now fixed for normal querying. The changes done to join path generation likely fix this one for update queries, too.
Anybody willing to write a test case for this ticket?
by , 13 years ago
| Attachment: | 0001-Fixed-10399-Tested-that-o2o-field-updates-are-not-co.patch added |
|---|
Testcase
comment:7 by , 13 years ago
Added a patch with a testcase that passes on (py273, py323) X (SQLite3, Postgresql).
comment:8 by , 13 years ago
CaptureQueriesContext is a nice addition. From the bikeshedding department:
- separate commit for adding
CaptureQueriesContext - specific tests for
CaptureQueriesContextin test_utils - when
__getitem__is applied to a list, I'd rather use an argument namediorindex
I'd suggest you go ahead with the commits when above points are addressed (unless someone else has other concerns, of course).
comment:9 by , 13 years ago
| Easy pickings: | unset |
|---|---|
| Has patch: | set |
| Needs tests: | unset |
| Patch needs improvement: | set |
Thanks for your review, was wondering if I should commit CaptureQueriesContext separately.
by , 13 years ago
| Attachment: | 0001-Added-a-context-manager-to-capture-queries-while-tes.patch added |
|---|
Added CaptureQueriesContext with tests
by , 13 years ago
| Attachment: | 0002-Fixed-10399-Tested-that-o2o-field-updates-are-not-co.patch added |
|---|
Testcase for this ticket using CaptureQueriesContext
comment:10 by , 13 years ago
| Patch needs improvement: | unset |
|---|
Added tests for CaptureQueriesContext, included claudep's suggestions and split the patch in two.
comment:11 by , 13 years ago
| Triage Stage: | Accepted → Ready for checkin |
|---|
comment:13 by , 13 years ago
| Resolution: | → fixed |
|---|---|
| Status: | assigned → closed |
This is a pretty old ticket... is it still just as relevant today with the current state of the ORM?