user2722667
user2722667

Reputation: 8661

Should I index my mysql foregin keys

I am using mysql as DB for my Laravel app.

In one table I have 5 fields that are foregin keys, those FK's points to 5 other tables primary keys.

Right now I have only marked them as FK, but do I need to put a index on every FK as well? Or does a FK count as a index?

Thanks in advance,

Upvotes: 0

Views: 28

Answers (1)

BK435
BK435

Reputation: 3176

In mysql when making a FK it automatically indexes that column(s).

MySQL requires indexes on foreign keys and referenced keys so that foreign key checks can be fast and not require a table scan. In the referencing table, there must be an index where the foreign key columns are listed as the first columns in the same order. Such an index is created on the referencing table automatically if it does not exist. This index might be silently dropped later, if you create another index that can be used to enforce the foreign key constraint. index_name, if given, is used as described previously.

You can read more here at dev.mysql

Upvotes: 1

Related Questions