-
-
Notifications
You must be signed in to change notification settings - Fork 760
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
Tree query improvements #3443
Merged
SchrodingersGat
merged 14 commits into
inventree:master
from
SchrodingersGat:category-query
Aug 1, 2022
Merged
Tree query improvements #3443
SchrodingersGat
merged 14 commits into
inventree:master
from
SchrodingersGat:category-query
Aug 1, 2022
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- Uses subquery to annotate the part-count for sub-categories - Huge reduction in number of queries
SchrodingersGat
added
bug
Identifies a bug which needs to be addressed
api
Relates to the API
labels
Jul 31, 2022
matmair
approved these changes
Jul 31, 2022
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good for now. Prerendering the path might be the best solution.
- No longer a dynamically calculated value - Constructed when the model is saved, and then written to the database - Limited to 250 characters
- Add new annotation to PartLocationDetail view
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
#3438 focused my attention on the
PartCategory
andStockLocation
list API endpoints.Currently they are quite inefficient!
Taking the PartCategory list endpoint as an example:
/api/part/category/?limit=<x>
This is very slow! (The StockLocation endpoint has similar issues)After fixes applied
There are two culprits here causing 1 + N query problems.
Part Count
Each category object has a field "parts" which is a count of the number of parts in the category (or any subcategories)
Solution
This has been tested, and works very well - query count has been cut in half!
Pathstring
The
pathstring
field is calculated "on the fly" for each part category. One DB query is required for each item in the results. For thousands of results, this means thousands of queries!This currently presents a more difficult challenge to fix!
Options to pursue here:
Caching
We could use the "cache" framework to save and load pathstring values, so we can load from memory instead of the database. This works (and has been tested), reducing the problem down to O(1) instead of O(n).
However the default caching framework can only handle a certain number of keys before it runs out. This solution works up to a point, but is always going to be limited by the maximum number of cache entries allowed by the system.
Annotation
We could (maybe) work out a queryset annotation to fetch the required data from the database and reduce the number of DB hits to a constant number.
However this is a harder problem than any existing queryset annotations. Might be not possible as we need to return multiple results per row, which is not generally possible with django's annotation framework.
Pre-Calculate and Store
We could pre-calculate the
pathstring
field and store in the database. Whenever the model is saved, re-compute and save the "pathstring" again. As these don't change very often, this would be not too bad.However we would be limited by the available number of characters in the char field. This might be OK if we decide on a mechanism to handle "very long" path strings, maybe something like:
/some/path/which/is/super/long/....../middle/is/cut/out/and/replaced/with-dots/