How to properly index a linking table for many-to-many connection in MySQL?

后端 未结 3 1435
孤独总比滥情好
孤独总比滥情好 2020-12-08 00:53

Lets say I have a simple many-to-many table between tables \"table1\" and \"table2\" that consists from two int fields: \"table1-id\" and \"table2-id\". How should I index t

3条回答
  •  一生所求
    2020-12-08 01:26

    As long as you are specifying both keys in the query, it doesn't matter what order they have in the query, nor does it matter what order you specify them in the index.

    However, it's not unlikely that you will sometimes have only one or the other of the keys. If you sometimes have id_1 only, then that should be the first (but you still only need one index).

    If you sometimes have one, sometimes the other, sometimes both, you'll need one index with both keys, and a second (non-unique) index with one field - the more selective of the two keys - and the primary composite index should start with the other key.

提交回复
热议问题