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

Fix N+1 query issue in bot_user method by using find_by #2164

Merged
merged 2 commits into from
Dec 20, 2024

Conversation

jayjay-w
Copy link
Contributor

Description

The bot_user method in the TeamBotInstallation model was previously using the where(...).last approach to fetch the BotUser, which could result in unnecessary database queries when called multiple times. This fix optimizes the method by replacing where(...).last with find_by(id: ...), ensuring only a single query is executed per call.

Changes:

  • Replaced where(...).last with find_by(id: ...) in the bot_user method.
  • Added a test to verify that no extra queries are triggered when calling bot_user.

References: CV2-5848

How has this been tested?

A test has been added to verify that no additional queries are triggered when accessing the bot_user method multiple times on the same instance, ensuring that the optimized code works as expected and prevents potential N+1 query issues.

Checklist

  • I have performed a self-review of my own code
  • I have added unit and feature tests, if the PR implements a new feature or otherwise would benefit from additional testing
  • I have added regression tests, if the PR fixes a bug
  • I have added logging, exception reporting, and custom tracing with any additional information required for debugging
  • I considered secure coding practices when writing this code. Any security concerns are noted above.
  • I have commented my code in hard-to-understand areas, if any
  • I have made needed changes to the README
  • My changes generate no new warnings
  • If I added a third party module, I included a rationale for doing so and followed our current guidelines

The bot_user method in the TeamBotInstallation model was previously using the
`where(...).last` approach to fetch the BotUser, which could result in unnecessary
database queries when called multiple times. This fix optimizes the method by replacing
`where(...).last` with `find_by(id: ...)`, ensuring only a single query is executed per call.

Additionally, a test has been added to verify that no additional queries are triggered when
accessing the `bot_user` method multiple times on the same instance, ensuring that the optimized
code works as expected and prevents potential N+1 query issues.

Changes:
- Replaced `where(...).last` with `find_by(id: ...)` in the `bot_user` method.
- Added a test to verify that no extra queries are triggered when calling `bot_user`.
Copy link

codeclimate bot commented Dec 20, 2024

Code Climate has analyzed commit 1509bd8 and detected 0 issues on this pull request.

The test coverage on the diff in this pull request is 100.0% (100% is the threshold).

This pull request will bring the total coverage in the repository to 100.0%.

View more on Code Climate.

@jayjay-w jayjay-w marked this pull request as ready for review December 20, 2024 19:02
@jayjay-w jayjay-w merged commit eb1d6d4 into develop Dec 20, 2024
16 checks passed
@jayjay-w jayjay-w deleted the CV2-5848-fix-n-plus-one-query-in-team-bot branch December 20, 2024 20:52
caiosba pushed a commit that referenced this pull request Dec 21, 2024
* Fix N+1 query issue in bot_user method by using find_by

The bot_user method in the TeamBotInstallation model was previously using the
`where(...).last` approach to fetch the BotUser, which could result in unnecessary
database queries when called multiple times. This fix optimizes the method by replacing
`where(...).last` with `find_by(id: ...)`, ensuring only a single query is executed per call.

Additionally, a test has been added to verify that no additional queries are triggered when
accessing the `bot_user` method multiple times on the same instance, ensuring that the optimized
code works as expected and prevents potential N+1 query issues.

Changes:
- Replaced `where(...).last` with `find_by(id: ...)` in the `bot_user` method.
- Added a test to verify that no extra queries are triggered when calling `bot_user`.

* Fix failing test
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.

2 participants