Skip to content

Commit

Permalink
PERF: add missing bounce_key index to email_logs
Browse files Browse the repository at this point in the history
We perform lookups based off bounce_key when emails bounce, we need the
index.
  • Loading branch information
SamSaffron committed May 2, 2019
1 parent 3dc4ab9 commit bb8cdf9
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions 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

4 comments on commit bb8cdf9

@tgxworld
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need to add the index before running the DELETE query. The delete query scans through bounce_key and without the index it is taking forever for me locally.

@SamSaffron
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll just assume no dupes then, this is simpler and odds of dupes here are pretty much zero.

@tgxworld
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

O right it is an unique index so we can't add it before the delete query.

@SamSaffron
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.