#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
giorgio79’s picture

Version: 7.x-dev » 11.0.x-dev

Maybe D11?

quietone’s picture

Status: Active » Postponed (maintainer needs more info)

@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

quietone’s picture

Version: 11.0.x-dev » 11.x-dev
quietone’s picture

On 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.

smustgrave’s picture

Should this one be closed?

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.