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 ActiveRecord queries in User#next_unpassed_progression_level #18437

Merged
merged 1 commit into from Oct 17, 2017

Conversation

wjordan
Copy link
Contributor

@wjordan wjordan commented Oct 17, 2017

This PR optimizes User#next_unpassed_progression_level in script_levels#show, eliminating 3 DB queries from this high-scale route:

Reusing the user_levels_by_level query (cached by ActiveRecord Query Cache) removes:

SELECT user_levels.* FROM user_levels WHERE user_levels.user_id = [user] AND user_levels.script_id = [script] AND user_levels.level_id IN ([level_ids]) ORDER BY id desc

Using the script cache instead of user_level.level.script_levels.where(script_id: script.id).first removes:

  • SELECT levels.* FROM levels WHERE levels.id = 455174957 LIMIT 1
  • SELECT script_levels.* FROM script_levels INNER JOIN levels_script_levels ON script_levels.id = levels_script_levels.script_level_id WHERE levels_script_levels.level_id = [level] AND script_levels.script_id = [script] ORDER BY script_levels.id ASC LIMIT 1

Part of ActiveRecord query-optimization pass (see #18390). These queries combined account for ~9% of our current DB-query load.

optimize User#next_unpassed_progression_level.
@wjordan wjordan changed the title Optimize ActiveRecord queries in script_levels#show Optimize ActiveRecord queries in User#next_unpassed_progression_level Oct 17, 2017
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

Successfully merging this pull request may close these issues.

None yet

2 participants