A platform for community discussion. Free, open, simple.
PERF: Use an index to look up embeds by URL in `TopicEmbed`
Régis Hanol5606f19ef132
diff --git a/app/models/topic_embed.rb b/app/models/topic_embed.rbindex 74a16023b6d..31e643d133c 100644--- a/app/models/topic_embed.rb+++ b/app/models/topic_embed.rb@@ -120,7 +120,7 @@ class TopicEmbed < ActiveRecord::Basereturn post unless post&.topic- if post.user != user+ if post.user_id != user.idPostOwnerChanger.new(post_ids: [post.id],topic_id: post.topic_id,@@ -350,8 +350,21 @@ class TopicEmbed < ActiveRecord::Baseenddef self.topic_embed_by_url(embed_url)- embed_url = normalize_url(embed_url).sub(%r{\Ahttps?\://}, "")- TopicEmbed.where("embed_url ~* ?", "^https?://#{Regexp.escape(embed_url)}$").first+ with_embed_urls([embed_url]).min_by(&:id)+ end++ def self.with_embed_urls(urls)+ variants =+ urls.flat_map do |url|+ key = embed_url_key(url)+ ["http://#{key}", "https://#{key}"]+ end++ where("lower(embed_url) IN (?)", variants)+ end++ def self.embed_url_key(url)+ normalize_url(url).sub(%r{\Ahttps?\://}, "")enddef self.topic_id_for_embed(embed_url)@@ -441,5 +454,6 @@ end## Indexes#-# index_topic_embeds_on_embed_url (embed_url) UNIQUE+# index_topic_embeds_on_embed_url (embed_url) UNIQUE+# index_topic_embeds_on_lower_embed_url (lower((embed_url)::text))#diff --git a/db/migrate/20260914140746_add_lower_embed_url_index_to_topic_embeds.rb b/db/migrate/20260914140746_add_lower_embed_url_index_to_topic_embeds.rbnew file mode 100644index 00000000000..5db92267f97--- /dev/null+++ b/db/migrate/20260914140746_add_lower_embed_url_index_to_topic_embeds.rb@@ -0,0 +1,15 @@+# frozen_string_literal: true+class AddLowerEmbedUrlIndexToTopicEmbeds < ActiveRecord::Migration[8.0]+ disable_ddl_transaction!++ INDEX_NAME = "index_topic_embeds_on_lower_embed_url"++ def up+ remove_index :topic_embeds, name: INDEX_NAME, algorithm: :concurrently, if_exists: true+ add_index :topic_embeds, "lower(embed_url)", name: INDEX_NAME, algorithm: :concurrently+ end++ def down+ remove_index :topic_embeds, name: INDEX_NAME, algorithm: :concurrently, if_exists: true+ end+enddiff --git a/db/structure.sql b/db/structure.sqlindex 5c7f093265a..6b5e65d11a9 100644--- a/db/structure.sql+++ b/db/structure.sql@@ -23537,6 +23537,13 @@ CREATE UNIQUE INDEX index_topic_custom_fields_on_topic_id_and_slack_thread_id ONCREATE UNIQUE INDEX index_topic_embeds_on_embed_url ON public.topic_embeds USING btree (embed_url);+--+-- Name: index_topic_embeds_on_lower_embed_url; Type: INDEX; Schema: public; Owner: -+--++CREATE INDEX index_topic_embeds_on_lower_embed_url ON public.topic_embeds USING btree (lower((embed_url)::text));++---- Name: index_topic_groups_on_group_id_and_topic_id; Type: INDEX; Schema: public; Owner: ---@@ -25179,6 +25186,7 @@ INSERT INTO "schema_migrations" (version) VALUES('20260914213908'),('20260914172801'),('20260914172757'),+('20260914140746'),('20260910033302'),('20260910030427'),('20260910030404'),diff --git a/spec/models/topic_embed_spec.rb b/spec/models/topic_embed_spec.rbindex d86562da841..d18fbb93a93 100644--- a/spec/models/topic_embed_spec.rb+++ b/spec/models/topic_embed_spec.rb@@ -580,6 +580,23 @@ RSpec.describe TopicEmbed doexpect(TopicEmbed.topic_id_for_embed("http://example.com/post")).to eq(nil)end+ it "finds the topic id when the stored embed_url has mixed case" do+ topic_embed = Fabricate(:topic_embed, embed_url: "https://Example.com/Post/248")++ expect(TopicEmbed.topic_id_for_embed("http://example.com/post/248")).to eq(+ topic_embed.topic_id,+ )+ end++ it "returns the oldest topic id when both http and https embeds exist" do+ http_embed = Fabricate(:topic_embed, embed_url: "http://example.com/post/248")+ Fabricate(:topic_embed, embed_url: "https://example.com/post/248")++ expect(TopicEmbed.topic_id_for_embed("https://example.com/post/248")).to eq(+ http_embed.topic_id,+ )+ end+it "finds the topic id when the embed_url contains a query string" dotopic_embed = Fabricate(:topic_embed, embed_url: "http://example.com/post/248?key=foo")expect(TopicEmbed.topic_id_for_embed("http://example.com/post/248?key=foo")).to eq(