#693362: taxonomy_form_all() is dangerous just got committed (yay!) and while thinking about #556842: taxonomy_get_tree() memory issues I remembered that even if you specify a parent, the function still loads every term in the vocabulary just to check if one of them has that term as a parent, which is just horrible.
Seems like it ought to be possible to reduce memory for sub-trees by querying a slice of the tree each time. Won't be pretty but would hopefully scale better.
Should look something like this:
If $parent !=0 add WHERE parent = :parent to the query.
Then keep going down the tree with WHERE parent IN (:parents). This means you get a query for each level of depth under the term specified (rarely likely to be more than 5-6 queries) and only ever load the terms needed to return the proper tree, instead of always loading everything.
You'd also need to keep an array of all the tids loaded so far, because the final array has to be ordered by weight and name across all the terms - so that's at least two queries and a couple of extra loops, but still ought to balance out.
Comments
Comment #1
andypostArgument $depth could limit
WHERE parent IN (:parents)queriesComment #2
xjmCrossposting #1207326: Refactor taxonomy hierarchy API for performance, consistency, and convenience.
Comment #3
gielfeldt commentedHi
I've created a module called Taxonomy Edge http://drupal.org/project/taxonomy_edge which, amongst other things, reduces memory usage when selecting partial trees.
Comment #4
jibranComment #5
giorgio79 commentedMaybe D11?
Comment #6
quietone commented@giorgio79, There are other issues about taxonomy load and performance, Is this a duplicate of one of those? So far, I have found the following. There could be others.
#106015: [performance] DB caching for \Drupal\taxonomy\TermStorageController::loadTree()
#1241654: Reduce memory usage of taxonomy terms overview page
Comment #7
quietone commentedComment #8
quietone commentedOn further reflection about this I think the best thing to make progress is to open a new issue on 11.x and describe your particular circumstances. Also, I found #2183565: Avoid loading all terms on the taxonomy overview form which was committed to 10.2.x which may help with this.
Comment #9
smustgrave commentedShould this one be closed?