From bb8cdf9e5ff92242b544e61311054827943d2c47 Mon Sep 17 00:00:00 2001 From: Sam Saffron Date: Fri, 3 May 2019 08:48:01 +1000 Subject: [PATCH] PERF: add missing bounce_key index to email_logs We perform lookups based off bounce_key when emails bounce, we need the index. --- ...502223613_add_bounce_key_index_on_email_logs.rb | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 db/migrate/20190502223613_add_bounce_key_index_on_email_logs.rb 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 0000000000..f7d8493c94 --- /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