Does a UNIQUE constraint automatically create an INDEX on the field(s)?

后端 未结 1 861
野趣味
野趣味 2020-12-02 08:51

Should I define a separate index on the email column (for searching purposes), or is the index is \"automatically\" added along with UNIQ

相关标签:
1条回答
  • 2020-12-02 09:40

    A unique key is a special case of index, acting like a regular index with added checking for uniqueness. Using SHOW INDEXES FROM customer you can see your unique keys are in fact B-tree type indexes.

    A composite index on (email, user_id) is enough, you don't need a separate index on email only - MySQL can use leftmost parts of a composite index. There may be some border cases where the size of an index can slow down your queries, but you should not worry about them until you actually run into them.

    As for testing index usage you should first fill your table with some data to make optimizer think it's actually worth to use that index.

    0 讨论(0)
提交回复
热议问题