Covering index for a column on the right side of equals

kylie :

Please give me advice on what covering index should be created for the following query:

SELECT id
  FROM user
 WHERE email_address = '...' AND
       hashed_password = SHA2(CONCAT(salt, '...'), 256))

Because salt is also a column in the table, I'm unsure which of these is correct:

INDEX (email_address, hashed_password, id)

INDEX (email_address, hashed_password, salt, id)

Then again, since email address returns one row, putting hashed_password in the index seems redundant.

danblack :

You are correct, its a bit redundant.

As long as one of the indexes begins with email that is fine. You'll probably want a unique key on email alone to prevent duplicate email addresses being entered.

An innodb (and probably other engines) index will implicitly end in its primary key (assumed to be id) so there's never a need to explicitly add it.

While adding hashed_password, salt to the index will improve performance allowing the entire query to be served by the index, it does increase the index size slightly and I'm not sure you'll gain much from it, particularly as this just looks like a login.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=194991&siteId=1