-
Notifications
You must be signed in to change notification settings - Fork 242
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
Some issues with prefetching and related queries #106
Comments
Yes, exactly.
Thanks for the reporting, these extra queries were caused by a bug. Roughly speaking, after the retrieving of just primary key column Pony does not believed that this was the entire object and tries to fetch it again. Now this behavior should be fixed, and no object should be fetched more then once.
When the It is possible to optimize logic of
It is because the prefetching goes recursively. When you call Anyway, now after the major bug in query logic was fixed the number of queries should be much lower then you initially experienced even without using of prefetching. Tell me what do you think, and thanks again for the reporting! |
Thanks! The main bug appears fixed. I created a couple of patches to reduce the queries further in my specific situation. I don't expect you to merge these necessarily, but here they are in any case (tests are passing, added a couple):
Example output, given the same script as before:
Without patches:
With
With
(These patches only affect queries that use |
Thanks, the patches looks interesting, I'll think a bit and most likely merge them |
(Using pony-0.6, Python 3.3)
I'm seeing some extra queries I don't understand during prefetching and getting related entities. Setup is a simple Forum entity with possible related Topics:
I see the following queries
I assume there are 2 extra
Topic
queries to get-related because you don't know the first time if I am going to ask for related topics from otherForum
s, but when pony hits the secondforum.topics
, it goes ahead and loads the rest (for[2, 3]
)?If so, I figured I could add a
.prefetch()
then to tell Pony to just loadTopic
s for all 3Forum
s one query:and expected to see just one extra query, like
but instead see all of these queries now
At least some of those are logically duplicates, so I'm not sure what's happening there. After further testing, if I add a single field into the
Forum
model (without even using it), like this:The queries go back to the first example (loading
[1]
, then[2, 3]
). Adding the field toTopic
doesn't fix the issue. In any cases I couldn't get it to prefetch all related with a single query.Further, I have a second (and possibly related) issue. Setup is similar, except instead of
Forum
->Topic
, I have nested forums, soForum
->Forum
:And I see these queries
Again 2 extra queries (without using prefetch), but in
[3, 2, 5]
: why is pony fetching sub-forums for2
whenForum[2]
is not in the initial results (sinceForum[2].parent is not None
)? I added.prefetch()
to see what would happenand get:
So now not only
Forum[2]
butForum[4]
children is being pre-fetched, when I expect a single extra query with justThe text was updated successfully, but these errors were encountered: