Skip to content

Fix N+1 queries in dashboard widget loading#9981

Open
jrafanie wants to merge 1 commit intoManageIQ:masterfrom
jrafanie:fix-n-plus-1-dashboard-widgets
Open

Fix N+1 queries in dashboard widget loading#9981
jrafanie wants to merge 1 commit intoManageIQ:masterfrom
jrafanie:fix-n-plus-1-dashboard-widgets

Conversation

@jrafanie
Copy link
Copy Markdown
Member

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)

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)
@jrafanie jrafanie requested a review from a team as a code owner April 14, 2026 16:38
@jrafanie
Copy link
Copy Markdown
Member Author

WIP: investigating cypress failures locally and seeing it it happens in CI.

@miq-bot
Copy link
Copy Markdown
Member

miq-bot commented Apr 14, 2026

Checked commit jrafanie@d5d8091 with ruby 3.3.10, rubocop 1.86.0, haml-lint 0.72.0, and yamllint 1.37.1
2 files checked, 0 offenses detected
Everything looks fine. ⭐

@jrafanie jrafanie changed the title [WIP] Fix N+1 queries in dashboard widget loading Fix N+1 queries in dashboard widget loading Apr 14, 2026
@jrafanie
Copy link
Copy Markdown
Member Author

WIP: investigating cypress failures locally and seeing it it happens in CI.

ok, all green!

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 }
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants