Skip to content
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

Optimize Tree.hash_tree #24

Closed
rewritten opened this issue Sep 28, 2012 · 3 comments
Closed

Optimize Tree.hash_tree #24

rewritten opened this issue Sep 28, 2012 · 3 comments
Assignees

Comments

@rewritten
Copy link

Right now it just executes the :hash_tree method for each root. So it's potentially a O(N) query. It can be done in one swipe, preloading by generation including the roots:

scope = Category.select("categories.*, category_hierarchies.*").
     joins("left join category_hierarchies on ancestor_id = categories.id").
     order("parent_id IS NOT NULL, generations ASC, name")
@ghost ghost assigned mceachen Sep 29, 2012
@mceachen
Copy link
Collaborator

You're correct, the class #hash_tree method does do 1 select for every root, but your scope won't work, as it hides the root nodes, nor does it respect the :limit_depth option.

I'll see what I can do.

@mceachen
Copy link
Collaborator

v3.6.3 uses 1 query now.

@rewritten
Copy link
Author

You are right it does not take into account the :limit_depth option, but the roots are loaded first because of the left join and the parent_id is not null first order key (so roots are listed first). In any case it was just a hint, I didn't mean to provide a comlpete solution... Thanks for citing me in the changelog.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants