declaring the FK will create an index automatically (it didn't used to) but only if a suitable index does not already exist
Code:
CREATE TABLE junction
( key1 INTEGER NOT NULL
, key2 INTEGER NOT NULL
, PRIMARY KEY (key1,key2)
);
that;s the way you normally do it
a query which supplies a value for key1 would ustilize the index, and if you were to declare a FK on key1, it could use the PK index and wouldn't create another one
i typically also add a second alternate index on ( key2,key1 ), and thus the FK on key2 could use that one
why the two-column alternate index instead of just an index on key2 itself? because then it's a covering index (which you could google to see why this is a good idea -- queries don't actually have to read the table rows at all!)
in any case, you would see the FK indexes that mysql creates by default if it needs to, they are not "secret" indexes or anything, so you should be able to see if the index is redundant
Bookmarks