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
Speed up Lesson#unplugged_lesson?
#41252
Conversation
Currently, this method is implemented by querying for all ScriptLevels in the Lesson's Script, and manually filtering them down to just the one ScriptLevel we actually care about. We should instead query directly for the ScriptLevel we care about. Right now, calling Script#summarize invokes this method quite a lot. Specifically: - Script#summarize_for_unit_edit invokes LessonGroup#summarize_for_unit_edit for each LessonGroup in the Script - LessonGroup#summarize_for_unit_edit invokes Lesson#summarize_for_unit_edit for each Lesson in the LessonGroup - Lesson#summarize_for_unit_edit invokes Lesson#summarize - Lesson#summarize invokes ScriptLevel#summarize for each ScriptLevel in the Lesson - ScriptLevel#summarize invokes ScriptLevel#level_display_text - ScriptLevel#level_display_text invokes Lesson#unplugged_lesson? for the Lesson associated with the ScriptLevel What this means is that when we call Script#summarize, we are querying for _all_ ScriptLevels in the entire Script not just once, but _once for every ScriptLevel in the entire Script_.
My current guesstimate (based on local testing) is that this should approximately halve the time it takes to render the script edit page for migrated scripts. |
Follow-up to #41252 I don't know whether or not any of these are currently actively causing any significant performance issues, but they're all less performant than they should be.
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 not 100% sure this change jives with the current strategy of caching script objects and their associated models -- in production, none of these queries will be actually hitting the database, just the script cache.
even worse, there is a chance that this will actually be adding queries in production, since this change could take us from hitting the script cache to not hitting the script cache.
can you confirm that this does not cause an increased query count in production before proceeding?
Good call, Dave! Looks like your suspicions were correct, and the original change would result in a performance hit. I refactored this a bit so it now uses whichever query strategy is appropriate based on |
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 checking that, Elijah! This confirms my suspicion that rails is out to trick us 😛 This solution looks good to me.
Currently, this method is implemented by querying for all ScriptLevels in the Lesson's Script, and manually filtering them down to just the one ScriptLevel we actually care about. We should instead query directly for the ScriptLevel we care about.
Right now, calling
Script#summarize_for_unit_edit
invokes this method quite a lot. Specifically:Script#summarize_for_unit_edit
invokesLessonGroup#summarize_for_unit_edit
for each LessonGroup in the ScriptLessonGroup#summarize_for_unit_edit
invokesLesson#summarize_for_unit_edit
for each Lesson in the LessonGroupLesson#summarize_for_unit_edit
invokesLesson#summarize
Lesson#summarize
invokesScriptLevel#summarize
for each ScriptLevel in the LessonScriptLevel#summarize
invokesScriptLevel#level_display_text
ScriptLevel#level_display_text
invokesLesson#unplugged_lesson?
for the Lesson associated with the ScriptLevelWhat this means is that when we call
Script#summarize_for_unit_edit
, we are querying for all ScriptLevels in the entire Script not just once, but once for every ScriptLevel in the entire Script.Also fix
Lesson#spelling_bee?
in the same way, while I'm in the area.PR Checklist: