Skip to content

A platform for community discussion. Free, open, simple.

PERF: add missing bounce_key index to email_logs

Sam Saffronbb8cdf9e5ff9
diff --git a/db/migrate/20190502223613_add_bounce_key_index_on_email_logs.rb b/db/migrate/20190502223613_add_bounce_key_index_on_email_logs.rb
new file mode 100644
index 00000000000..f7d8493c94c
--- /dev/null
+++ b/db/migrate/20190502223613_add_bounce_key_index_on_email_logs.rb
@@ -0,0 +1,14 @@
+class AddBounceKeyIndexOnEmailLogs < ActiveRecord::Migration[5.2]
+ def change
+ execute <<~SQL
+ DELETE FROM email_logs l
+ WHERE bounce_key IS NOT NULL
+ AND id > (
+ SELECT MIN(id)
+ FROM email_logs l2
+ WHERE l2.bounce_key = l.bounce_key
+ )
+ SQL
+ add_index :email_logs, [:bounce_key], unique: true, where: 'bounce_key IS NOT NULL'
+ end
+end