简体   繁体   中英

Indexes on many-to-many table

current setup is:

  • objects (notes, reminders, files) - each in separate table
  • entities (clients, projects) - each in separate table

object can belong to many entities, entities can have many objects

associations table looks like this:

  • object_type_id, object_id, entity_type_id, entity_id

How would you handle indexes on associations table? Any comments about the setup?

I'm not that strong on databases in general. But i usually allways index any field that is a id reference to another table.

So i'd probably index all the fields in your associations table, since they all refer to data in other tables (or so i assume).

You should probably also add a Primary KEY id to the associations table, so when you wan't to delete an association you can do it via a primary key reference.

With mysql, if you've defined the foreign keys as actual RI foreign keys using the references keyword, you get an index automatically defined on the table. And primary keys also get an index, so you shouldn't have to define any indexes manually.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM