简体   繁体   English

多对多表上的索引

[英]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 object可以属于很多实体,实体可以有很多对象

associations table looks like this:关联表如下所示:

  • object_type_id, object_id, entity_type_id, entity_id 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.但我通常总是索引任何作为对另一个表的 id 引用的字段。

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.您可能还应该在关联表中添加主键 ID,因此当您不想删除关联时,可以通过主键引用来完成。

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.使用 mysql,如果您使用references关键字将外键定义为实际的 RI 外键,您将在表上自动定义一个索引。 And primary keys also get an index, so you shouldn't have to define any indexes manually.主键也有一个索引,所以你不必手动定义任何索引。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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