Post一覧取得時のN+1問題と解消 #2
Open
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.
📝 概要
User Load
を読んでいることが判明した。 上記の原因は、行ごとにcomment.user.name
を参照してるため、include or preloadを使用しないとコメント数分users
を個別selectしてしまう。そのため、一覧取得に事前に関連情報を
preload
する処理を追加した。tag
に関しても同様です🙇🔍 変更内容
✅ 動作確認
📸 スクリーンショット
User
1クエリでまとめて取得できている

Tag
5件のpost分を一括で取得できている

💬 補足