-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Fix N+1 problem on Api::TaxonsController#index #3011
Fix N+1 problem on Api::TaxonsController#index #3011
Conversation
Thanks @stem! I'm trying to analyze the result of this PR with bullet but I see the same results as before without PR. Can you please help me understand what happens and why this should fix the N+1? I'm also trying to see some documentation in |
I understand your concerns about Concerning bullet, I don't know how to use it.
Of course, the other queries are the same, but in this case, the total number came down by 7 queries (8 taxon's ancestors that are loaded in 1 query instead of 8) If you point me to a way you wanna test it, I can write a spec to ensure the N+1 doesn't reappear. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the explanation, now the context is more clear. I left a comment on a possible improvement to avoid declaring a new scope.
Also, I've made some experiment testing the query count to avoid N+1 here: https://gist.github.com/kennyadsl/4443c3bce12e8d2ef2eb0740d61d2344.
Let me know if both make sense!
@stem 👋Hey there! Any update here? |
@kennyadsl, I'll look into it now that I'm back from vacations 🏖 Your experiment seems to do the job so I'll take that road and update this PR to add theses tests |
FYI, I've found and fixed another N+1 with the taxon's children while ensuring that the tests were OK. That helps us moving from 11 queries to 5 for the 2 leafs in this simple example :
|
A last, small thing. Can you rebase and squash some commits (I'm looking at the Hound violation commit in particular, that could go into the |
ea14bc1
to
30e4e55
Compare
@kennyadsl I've rebased and squash some commits. Let me know if I can do something more to improve this PR. |
@kennyadsl is there anything I can do to improve or merge this one ? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm good with this! Thanks!
Thanks for putting this together. I'm interested by the query count spec but am concerned it would be fragile and likely to cause potential failures in the future. What do you think about removing the query count matcher portion of this as I think the rest of this is good to go as is. |
In the core team meeting we talked about this and I agree with @gmacdougall here (sorry, I pushed to add that spec). There could be a lot of things (rails/act_as_nested_set updates or specs execution order) that would make this test fail. It's probably better to remove it now that we are sure we removed the N+1 there. |
When asking Api::TaxonsController#index with multiple IDs, we used to generate a new SQL query to find each taxon's ancestors. This generates a query to find all ancestors for all taxons and use `AwesomeNestedSet::associate_parents` to associate all parents
- use the same "algorithm" than Taxon.permalink - use taxon.parent instead of taxon.ancestors to take advantage of the preloaded parents
This endpoint had another N+1 problem when loading the selected taxon's children. This includes them only if the caller ask for the children.
30e4e55
to
7fbb31d
Compare
@kennyadsl I've removed the query_counter part, but kept the rspec matcher in case we need to investigate other N+1. Let me know if you think I should also remove that. |
@stem yes, I think it's better to remove this module since we don't use it and we could keep code in the repository that could no longer work in the future. |
As pointed out by the core team, this spec might be too hazardous in the future (ex: an update of acts_as_nested_set that cause the code to make 1 additional query). I kept the spec to check that the API is correctly handling multiple IDs as it's an existing feature.
7fbb31d
to
f403266
Compare
@kennyadsl done ! |
Thanks @stem ! |
With the help of
associate_parents
defined by awesome_nested_set, we can associate the parent of a flat collection.That way, we can preload all parents for a defined set of taxons with just 1 SQL query.
Closes #3004