#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

andypost’s picture

Argument $depth could limit WHERE parent IN (:parents) queries

xjm’s picture

gielfeldt’s picture

Hi

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.

jibran’s picture

Title: Reduce memory usage from taxonomy_get_tree() when parent is specified » Reduce memory usage from \Drupal\taxonomy\TermStorageInterface::loadTree() when parent is specified
Issue summary: View changes