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开发者_JAVA技巧, 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.
精彩评论