A platform for community discussion. Free, open, simple.
PERF: Prevent N+1 queries when loading theme/component descriptions (#32305)
Osama Sayegh996a3493fe3c
diff --git a/app/models/theme.rb b/app/models/theme.rbindex 57a9b0afa24..94e5ff9ee3a 100644--- a/app/models/theme.rb+++ b/app/models/theme.rb@@ -87,14 +87,21 @@ class Theme < ActiveRecord::Base:child_themes,:theme_settings,:settings_field,- :locale_fields,:color_scheme,- :theme_translation_overrides,theme_fields: %i[upload theme_settings_migration],)end- scope :include_basic_relations, -> { includes(:parent_themes, :remote_theme, :user) }+ scope :include_basic_relations,+ -> do+ includes(+ :remote_theme,+ :user,+ :locale_fields,+ :theme_translation_overrides,+ parent_themes: %i[locale_fields theme_translation_overrides],+ )+ enddelegate :remote_url, to: :remote_theme, private: true, allow_nil: truediff --git a/spec/models/theme_spec.rb b/spec/models/theme_spec.rbindex 95817be009e..8a7451b8a75 100644--- a/spec/models/theme_spec.rb+++ b/spec/models/theme_spec.rb@@ -1596,4 +1596,105 @@ HTMLexpect(ColorScheme.unscoped.exists?(id: scheme.id)).to eq(false)endend++ describe ".include_basic_relations" do+ fab!(:parent_theme_1) do+ Fabricate(+ :theme,+ theme_fields: [+ ThemeField.new(+ name: "en",+ type_id: ThemeField.types[:yaml],+ target_id: Theme.targets[:translations],+ value: <<~YAML,+ en:+ theme_metadata:+ description: "Description of my theme"+ YAML+ ),+ ],+ )+ end++ fab!(:parent_theme_2) do+ Fabricate(+ :theme,+ theme_fields: [+ ThemeField.new(+ name: "en",+ type_id: ThemeField.types[:yaml],+ target_id: Theme.targets[:translations],+ value: <<~YAML,+ en:+ theme_metadata:+ description: "Description of my theme 2"+ YAML+ ),+ ],+ )+ end++ fab!(:component_1) do+ Fabricate(+ :theme,+ component: true,+ parent_themes: [parent_theme_1],+ theme_fields: [+ ThemeField.new(+ name: "en",+ type_id: ThemeField.types[:yaml],+ target_id: Theme.targets[:translations],+ value: <<~YAML,+ en:+ theme_metadata:+ description: "Description of my component"+ YAML+ ),+ ],+ )+ end++ fab!(:component_2) do+ Fabricate(+ :theme,+ component: true,+ parent_themes: [parent_theme_2],+ theme_fields: [+ ThemeField.new(+ name: "en",+ type_id: ThemeField.types[:yaml],+ target_id: Theme.targets[:translations],+ value: <<~YAML,+ en:+ theme_metadata:+ description: "Description of my component 2"+ YAML+ ),+ ],+ )+ end++ it "doesn't result in N+1 queries for descriptions" do+ components = Theme.include_basic_relations.where(component: true, id: component_1.id)++ queries_for_one =+ track_sql_queries do+ components.each do |component|+ ComponentIndexSerializer.new(component, root: false).as_json+ end+ end++ components =+ Theme.include_basic_relations.where(component: true, id: [component_1.id, component_2.id])++ queries_for_two =+ track_sql_queries do+ components.each do |component|+ ComponentIndexSerializer.new(component, root: false).as_json+ end+ end++ expect(queries_for_two.size).to eq(queries_for_one.size)+ end+ endend