I am creating an event to user lookup table using the Innodb search engine.
Table Event_Users
event_id
user_id
Index (PRIMARY) -> event_id, user_id
Index (secondary) -> user_id, event_id ??
The application will search user to event and event to user. How do I define the second index if the primary key is clustered? Should it simply be user_id and then MYSQL will figure out that event_id is already present in the PK or should I be redundant and include user_id, event_id in the secondary index?
In addition, is this a good use of a clustered index? Any guidance and help with Innodb clustered indexes would be greatly appreciated. Thanks!
MySQL should be able to use the fields in the PK as a covering index without making a composite secondary index.
Therefore:
Table (Engine = INNODB)
If you would like to search by
column_bsimply add index:column_b.Since the table using the Innodb engine, the PK will exist in the
column_bindex. Mysql will be able to use the index (using index). No need to create indexcolumn_b, column_aas this would be redundant. – Remember, inndob clusters by PK and all indexes reference the PK.A surrogate key is a poor choice for this design because extra work would have to be made to ensure
column_a, column_bdoes not already exist.