Fix N+1 queries in dashboard widget loading#9981
Open
jrafanie wants to merge 1 commit intoManageIQ:masterfrom
Open
Fix N+1 queries in dashboard widget loading#9981jrafanie wants to merge 1 commit intoManageIQ:masterfrom
jrafanie wants to merge 1 commit intoManageIQ:masterfrom
Conversation
Eliminates N+1 queries by eager loading widget contents and passing widgets to view via instance variable hash instead of using find_by_id. The view was calling MiqWidget.find_by_id for each widget, bypassing any eager loading done in the controller. This caused individual queries for each widget plus their contents. 1. Eager load widget contents with .includes(:miq_widget_contents) 2. Create widgets_by_id hash for fast lookup in view 3. Update view to use hash instead of find_by_id - Before: 49 queries (9 cached) - After: 39 queries (8 cached) - Eliminated: 8 individual MiqWidget queries + improved content loading View makes individual widget queries: MiqWidget Load (0.8ms) WHERE "miq_widgets"."id" = $1 (widget 4) MiqWidget Load (0.5ms) WHERE "miq_widgets"."id" = $1 (widget 3) MiqWidget Load (0.4ms) WHERE "miq_widgets"."id" = $1 (widget 9) MiqWidget Load (0.3ms) WHERE "miq_widgets"."id" = $1 (widget 24) MiqWidget Load (0.4ms) WHERE "miq_widgets"."id" = $1 (widget 22) MiqWidget Load (0.4ms) WHERE "miq_widgets"."id" = $1 (widget 1) MiqWidget Load (4.5ms) WHERE "miq_widgets"."id" = $1 (widget 6) MiqWidget Load (2.4ms) WHERE "miq_widgets"."id" = $1 (widget 21) MiqWidget Load (0.1ms) WHERE "miq_widgets"."id" = $1 (widget 10) Plus individual MiqWidgetContent queries for each widget. Result: Completed 200 OK in 299ms (49 queries, 9 cached) Single bulk load with eager loading: MiqWidget Load (3.5ms) WHERE "miq_widgets"."id" IN ($1, $2, ..., $27) MiqWidgetContent Load (4.1ms) WHERE "miq_widget_contents"."miq_widget_id" IN ($1, $2, ..., $27) View uses preloaded data - all subsequent queries are CACHE hits (0.0-0.8ms). Result: Completed 200 OK in 311ms (39 queries, 8 cached)
Member
Author
|
WIP: investigating cypress failures locally and seeing it it happens in CI. |
Member
|
Checked commit jrafanie@d5d8091 with ruby 3.3.10, rubocop 1.86.0, haml-lint 0.72.0, and yamllint 1.37.1 |
Member
Author
ok, all green! |
Fryguy
reviewed
Apr 14, 2026
Comment on lines
+215
to
+216
| widgets = MiqWidget.available_for_user(current_user) | ||
| eager_loaded_widgets = MiqWidget.where(:id => widgets.map(&:id)).includes(:miq_widget_contents).sort_by { |a| a.content_type + a.title.downcase } |
Member
There was a problem hiding this comment.
Is MiqWidget.available_for_user a scope? If not, can it be? Asking because it looks like we pull back the widgets, and then we pull them back again. If available_for_user cannot be turned into a scope, then instead of pull back the records a second time, may we can use MiqPreloader here instead.
Suggested change
| widgets = MiqWidget.available_for_user(current_user) | |
| eager_loaded_widgets = MiqWidget.where(:id => widgets.map(&:id)).includes(:miq_widget_contents).sort_by { |a| a.content_type + a.title.downcase } | |
| widgets = MiqWidget.available_for_user(current_user) | |
| eager_loaded_widgets = MiqPreloader.preload(widgets, :miq_widget_contents).sort_by { |a| a.content_type + a.title.downcase } |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Eliminates N+1 queries by eager loading widget contents and passing
widgets to view via instance variable hash instead of using find_by_id.
The view was calling MiqWidget.find_by_id for each widget, bypassing
any eager loading done in the controller. This caused individual queries
for each widget plus their contents.
View makes individual widget queries:
Plus individual MiqWidgetContent queries for each widget.
Result: Completed 200 OK in 299ms (49 queries, 9 cached)
Single bulk load with eager loading:
View uses preloaded data - all subsequent queries are CACHE hits (0.0-0.8ms).
Result: Completed 200 OK in 311ms (39 queries, 8 cached)