New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Optimise owrt checking of double underscore relations #295
base: master
Are you sure you want to change the base?
Conversation
…_to check during save() This favours a smaller overall query count over simpler queries. By adding attributes during evaluation of the queryset, we can store the prior value of ForeignKey relations, for the cost of a join for each entry within order_with_respect_to (nested joins if LOOKUP_SEPARATOR (__) is used). The fetched attibutes are not available during OrderedModel.__init__() so we defer creation of self._original_wrt_map until save(). Since this reads from attributes it no longer causes any additional queries. This seems to balance correctness in the case of modifying the fields you are ordered with respect to, against minimising extra queries during either get() or save() operations, and does not require the full related object to be fetched to detect the change.
a7480d5
to
1b691f8
Compare
Wow! Yes, it does. Now I'm at 9 queries. I made my own aproach #294 , requires two aditional queries on save. I'm wan't to investigate what happens in the admin, and why do you need this. Edit: It's 8 queries, same with 3.6 version. |
I wonder why 9 rather than the 8 queries you had under 3.6.x series? Any chance you can diff them? |
I checked again and now it's 8 queries in both 3.6 version and this pull request. I think sometimes debug toolbar do an aditional query for inspecting or anything. |
I would like to get this into shape for the next release 3.8 as its the final piece. The annotations added here allow us to very cheaply preserve OWRT values (to test for changes) beyond double-underscore relations by fetching all required values in the single initial query rather than walking related objects one by one. We need to support OWRT ending on both a foreign key (adding If the object is constructed by a @rafammpp posted nice code in #294 to build the 'intitial value' owrt map during So the plan is:
This should keep the minimal number of queries in each case. If a large number of instances from the same |
Tentative fix for #293, however it's not straightforward and mandates use of our
ModelManager
subclass.Does this help with your query count @rafammpp ?