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
cache key calc for loader options w/ reuse has performance issue #4270
Comments
Michael Bayer (@zzzeek) wrote: here's a test script too!
|
Michael Bayer (@zzzeek) wrote: in this use case I wonder if even without the cache key if we are generating redundant Load options that are applied to the query. |
Michael Bayer (@zzzeek) wrote: reviews going through at https://gerrit.sqlalchemy.org/#/q/I955fe2f50186abd8e753ad490fd3eb8f017e26f9 which also include a separate fix for the re-application of loader options issue. |
Michael Bayer (@zzzeek) wrote: Iterate options per path for baked cache key Fixed an issue that was both a performance regression in 1.2 as well as an Change-Id: I955fe2f50186abd8e753ad490fd3eb8f017e26f9 → 006da86 |
Michael Bayer (@zzzeek) wrote: Iterate options per path for baked cache key Fixed an issue that was both a performance regression in 1.2 as well as an Change-Id: I955fe2f50186abd8e753ad490fd3eb8f017e26f9 → 99f1239 |
Changes by Michael Bayer (@zzzeek):
|
Migrated issue, originally created by Michael Bayer (@zzzeek)
copied from the mailing list
We have just upgraded to sqlalchemy 1.2.7 (from 1.1.14), and had a performance issue with a query that uses a lot of joinedloads that was caused by the automatic baking of all relationship queries that was introduced in 1.2.
Say we have a set of tables with relationships Book.pages, Page.font and Font.layout. We have a query of this form:
The important point here is that the pages object defined on line 1 is reused in both option1 and option2. Now suppose we fetch another relationship that wasn't joined-loaded on the returned instances. This will case another query as it is not loaded already, and this query will be baked due to the change in 1.2 to bake all relationship loads.
We found that the construction of the cache key for baking this query becomes very slow as the number of options of this form increases, and is in fact quadratic in the number of such options (we have ~25 such options in our problematic query). This is due to each option containing all of the joinedloads inside its _to_bind attribute, and _UnboundLoad._generate_cache_key has to process everything in the _to_bind list. E.g. in this example:
Therefore, when generating the key for each option we are processing the joinedloads from all of the options, leading to the quadratic performance degradation.
We fixed it by avoiding reusing the joinedload for Book.pages by doing this:
The resulting query is unchanged, but the cache key function is now just linear in the number of joinedloads as each option has only its relationships in its _to_bind attribute. In our case, this completely solved the performance issue.
The text was updated successfully, but these errors were encountered: