Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX: Do batches for backfilling huge embeddings tables #1065

Merged
merged 1 commit into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 17 additions & 7 deletions db/migrate/20250114160417_backfill_topic_embeddings.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
# frozen_string_literal: true
class BackfillTopicEmbeddings < ActiveRecord::Migration[7.2]
def up
not_backfilled = DB.query_single("SELECT COUNT(*) FROM ai_topics_embeddings").first.to_i == 0
disable_ddl_transaction!

if not_backfilled
# Copy data from old tables to new tables
execute <<~SQL
INSERT INTO ai_topics_embeddings (topic_id, model_id, model_version, strategy_id, strategy_version, digest, embeddings, created_at, updated_at)
SELECT * FROM ai_topic_embeddings;
def up
loop do
count = execute(<<~SQL).cmd_tuples
INSERT INTO ai_topics_embeddings (topic_id, model_id, model_version, strategy_id, strategy_version, digest, embeddings, created_at, updated_at)
SELECT source.*
FROM ai_topic_embeddings source
WHERE NOT EXISTS (
SELECT 1
FROM ai_topics_embeddings target
WHERE target.model_id = source.model_id
AND target.strategy_id = source.strategy_id
AND target.topic_id = source.topic_id
)
LIMIT 10000
SQL

break if count == 0
end
end

Expand Down
22 changes: 17 additions & 5 deletions db/migrate/20250114160446_backfill_post_embeddings.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,26 @@
# frozen_string_literal: true
class BackfillPostEmbeddings < ActiveRecord::Migration[7.2]
disable_ddl_transaction!

def up
not_backfilled = DB.query_single("SELECT COUNT(*) FROM ai_posts_embeddings").first.to_i == 0
# Copy data from old tables to new tables in batches.

if not_backfilled
# Copy data from old tables to new tables
execute <<~SQL
loop do
count = execute(<<~SQL).cmd_tuples
INSERT INTO ai_posts_embeddings (post_id, model_id, model_version, strategy_id, strategy_version, digest, embeddings, created_at, updated_at)
SELECT * FROM ai_post_embeddings;
SELECT source.*
FROM ai_post_embeddings source
WHERE NOT EXISTS (
SELECT 1
FROM ai_posts_embeddings target
WHERE target.model_id = source.model_id
AND target.strategy_id = source.strategy_id
AND target.post_id = source.post_id
)
LIMIT 10000
SQL

break if count == 0
end
end

Expand Down
Loading