简体   繁体   English

为什么MySQL Workbench会在一个:m关系上创建两个索引?

[英]Why does MySQL Workbench create two indexes on a n:m relation?

I've been wondering this for a while now. 我一直想知道这已经有一段时间了。 When you use MySQL Workbench to create an:m relation between two tables (let's say A and B) the resulting table (let's say A_B) has three indexes (PRIMARY, one on A's primary key, one on B's primary key). 当您使用MySQL Workbench在两个表(比如说A和B)之间创建一个:m关系时,结果表(比如说A_B)具有三个索引(PRIMARY,一个在A的主键上,一个在B的主键上)。

According to this question PRIMARY KEY is always indexed by itself, so why would there be the need to index each singular field again with a singular index ? 根据这个问题, PRIMARY KEY总是自己索引的,那么为什么需要再次用奇异索引来索引每个奇异字段呢? Also if this is MySQL's policy, is it different with other DBMSs ? 另外,如果这是MySQL的策略,那么与其他DBMS是否不同?

it's a good rule of thumb in relational databases to have indexes on the columns which are involved in a foreign key relationship as most often your sql queries will join tables based on these columns. 在关系数据库中,在涉及外键关系的列上具有索引是一个很好的经验法则,因为大多数情况下,SQL查询将基于这些列联接表。

my guess is that workbench follows that rule of thumb but is not smart enough to see that an index already exists in those cases where one of the columns is a Pk. 我的猜测是,工作台遵循该经验法则,但不够聪明,无法看到在其中一列为Pk的情况下已经存在索引。

so it just adds one index per column and does not check if one of the columns is also a primary key. 因此它只为每列添加一个索引,而不检查其中一列是否也是主键。 (the pk column does not have to be a part of a foreign key relationship) (pk列不必是外键关系的一部分)

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

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