Opened 3 years ago

Closed 3 years ago

Last modified 3 years ago

#34591 closed Cleanup/optimization (invalid)

Django ORM replace INNER JOIN with STRAIGHT_JOIN

Reported by: dev-c Owned by: nobody
Component: Database layer (models, ORM) Version: 4.2
Severity: Normal Keywords: sql join
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Similar to this post here from 9+ years ago: https://code.djangoproject.com/ticket/22438

We have queries being generated that use "inner join" - which should be fine, but the SQL engine optimiser is not playing nice with it.
When I swap "inner join" for "straight_join" - thus by-passing the optimiser - I get the performance I'd expect.
With "inner join" - 1+ hour
With "straight_join" - 2 seconds.

This isnt a fault of Django, but after a lot of researching and tinkering, it appears sometimes you have to by-pass the optimiser to get the exact query you want.

Is this possible to do in Django? Perhaps Ive missed it in the docs. Even if its something a bit hacky like:

qs = myModel.object.filters(....).otherStuff(...)
qs.raw_sql.replace("inner ", "straight_")

that would be enough for me to test this further.

Any pointers would be greatly appreciated.

Change History (3)

comment:1 by Natalia Bidart, 3 years ago

Resolution: → invalid
Status: new → closed

Hello!

The best place to get answers to your question is using any of the user support options from ​this link.

This issue tracker isn't the right channel for these kind of questions because it is exclusively devoted to serve the development of Django itself.

comment:2 by dev-c, 3 years ago

Thanks for the quick response. Where exactly would you recommend I post this kind of question?

comment:3 by Natalia Bidart, 3 years ago

I would recommend to post to the Django Forum, specifically in the ​Using the ORM category. You'll get answers soon!

Note: See TracTickets for help on using tickets.
Back to Top