-
Notifications
You must be signed in to change notification settings - Fork 239
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
Experiencing slow leaves call on a model with closure tree #370
Comments
We ended up overriding the leaves call on our Does it make sense to look at implementing scoping similar to this in the gem?
|
@garrinmf ^^ |
We are facing the same problem. In case that the query works for us (I am still checking), then I can submit a pull request. Do you @weilandia want me to create the pull request? 😄 (I just want to be sure to not do work, that then no one wants) |
We found a way to work around the problem. So for now, the problem does not longer affect us. |
I have a model
Product
which is using closure tree on an application backed with Postgres. When we attempt to callProduct.leaves
the returning request is EXTREMELY slow on our production environment. Making a call to the sameProduct.self_and_descendants
is very speedy and is much faster to iterate over self_and_descendants and next if leaf? is false.The information below is run on my local development environment with significantly less rows.
Here is the database model with the appropriate indexes listed.
When loading a product and doing a
Product.leaves.explain
this is what is returned:Looking at the SQL it appears the INNER JOIN on the leaves call is not scoped at all. It is doing a
SELECT ancestor_id FROM "product_hierarchies" GROUP BY ancestor_id HAVING MAX("product_hierarchies".generations) = 0
.Which results in searching the entire table in the explain.
-> Seq Scan on product_hierarchies product_hierarchies_1 (cost=0.00..4475.56 rows=281456 width=8)
To compare, the number of rows being evaluated on my production environment are 42,149,108 instead of 281,456.
Is anyone else having any similar issues? My thought is there should be a way to tweak the sub-select to make it a little more performant either by scoping to somewhere else in the tree or potentially able to utilize an index.
Any help would be greatly appreciated.
The text was updated successfully, but these errors were encountered: