PERF: add missing bounce_key index to email_logs

We perform lookups based off bounce_key when emails bounce, we need the
index.
This commit is contained in:
Sam Saffron 2019-05-03 08:48:01 +10:00
parent 3dc4ab905c
commit bb8cdf9e5f
1 changed files with 14 additions and 0 deletions

View File

@ -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