-
Notifications
You must be signed in to change notification settings - Fork 1
feat: add request_distribution_ids to Bench API response #98
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
C'est effectivement une bonne manière d'ajouter les IDs de nos request_distributions sur nos bench :)
En revanche, on crée ainsi une N+1 ; ce qu'il faut comprendre, c'est qu'en renvoyant une liste de
benches, on va pour chacun d'entre eux exécuter une requête SQL pour récupérer les IDs des request_distribution de notre bench.Si on renvoie 10 benches, on va donc exécuter une requête SQL pour récupérer la liste des 10 benches, et pour chacun d'entre eux, faire une requête SQL pour faire un
SELECT request_distributions.id FROM ..., donc 11 requêtes en tout (d'où le terme N+1).Pour éviter ça, dans notre controller, on pourrait déjà charger la liste des request_distributions de tous nos bench, et ainsi ne faire que 2 requêtes en tout, indépendamment de la taille de notre liste de benches :) Pour ça, on peut utiliser le
includespar exemple.En remplaçant donc la ligne
benches = policy_scope(@greenhouse.benches)dans notre controller parbenches = policy_scope(@greenhouse.benches).includes(:request_distributions), on corrige une N+1 🚀Oh, et pour finir, petit sucre syntaxique sympa avec rails : à la place du
bench.request_distributions.pluck(:id)ici présent, vous pouvez écrirebench.request_distribution_ids👌Uh oh!
There was an error while loading. Please reload this page.
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.
Merci pour ton retour. Effectivement, cela va poser problème lorsqu'il y aura beaucoup de données à gérer.
Je vais suivre tes recommandations et changer cela ce week-end :)