简体   繁体   English

创建通用链接表是一个坏主意吗?

[英]Is it a bad idea to make a generic link table?

Imagine a meta database with a high degree of normalization. 想象一下具有高度规范化的元数据库。 It would blow up this input field if I would attempt to describe it here. 如果我试图在这里描述它,它将炸毁该输入字段。 But imagine, every relationship through the entire database, through all tables, go through one single table called link . 但是想像一下,整个数据库,所有表中的每个关系都通过一个称为link的表。 It has got these fields: master_class_id, master_attr_id, master_obj_id, class_id2, obj_id2 . 它具有以下字段: master_class_id,master_attr_id,master_obj_id,class_id2,obj_id2 This table can easily represent all kinds of relationships: 1:1, 1:n, m:n, self:self. 该表可以轻松表示各种关系:1:1、1:n,m:n,self:self。

I see the problem that this table is going to get HUUUUGE. 我看到该表将变得HUUUUGE的问题。 Is that bad practice? 那是不好的做法吗?

That is wrong on two accounts: 这在两个帐户上是错误的:

  1. It'll be a tremendous bottleneck for all your queries and it'll kill any chance of throughput. 这将是所有查询的巨大瓶颈,并且将消除吞吐量的任何机会。

  2. It reeks of bad design: you should be able to describe things more concisely and closer to reality. 设计不良的后果是:您应该能够更简洁,更接近现实地描述事物。 If this is really the best way to store the data you can consider partitioning or even another paradigm instead of the relational 如果这确实是存储数据的最佳方法,则可以考虑分区甚至其他范式,而不是关系型

In a word, yes , this is a bad idea 一句话, 是的这是一个坏主意

Without going into too many details, I would offer the following: 在不涉及太多细节的情况下,我将提供以下内容:

  • for a meta database, the link table should be split by (high level) entity : that is, you should have a separate link table for each entity 对于元数据库,链接表应按(高级)实体划分:也就是说,每个实体都应有单独的链接表
  • another link table is required for the between-entities links 实体之间的链接需要另一个链接表

Normally the high-level entities are fairly easy to identify, like Customer. 通常,高层实体很容易识别,例如客户。

It is usually bad practice but not because the table is huge. 这通常是不好的做法,但不是因为表很大。 The problem is that you are mixing unrelated data in one table. 问题是您正在将不相关的数据混合在一张表中。

The reason to keep the links in separate tables, is because you won't need to use them together. 之所以将链接保留在单独的表中,是因为您不需要一起使用它们。

It is a common mistake that is also done with data itself: You should not mix two sets of data in one table only because the fields are similar if the data itself is unrelated. 数据本身也是一个常见的错误: 您不应仅在一个表中混合两组数据,因为如果数据本身不相关,则字段相似。

Relational databases don't actually fit for this model. 关系数据库实际上不适合该模型。

It's possible to implement it but it will be quite slow. 可以实现它,但是会很慢。 The main drawback is that you won't be able to index the links efficiently. 主要缺点是您将无法有效地索引链接。

However, this design can be useful in two cases: 但是,此设计在两种情况下可能有用:

  1. This only stores the metadata: declared relationships between the entities. 这仅存储元数据:实体之间的声明关系。 The actual data are stored in the plain relational tables, so this links are only used to show the structure but not in the actual queries. 实际数据存储在普通的关系表中,因此此链接仅用于显示结构,而不用于实际查询中。

  2. This stores some structures which are complex but contain few data, so that the ease of development overweights the performance drawbacks. 这会存储一些结构复杂但包含很少数据的结构,因此易于开发会加重性能缺陷。

This design can be seen in several ORM s (one of which I even developed). 这种设计可以在几个ORM看到(我什至开发了一个)。

I don't see the purpose of this type of table anyway. 我仍然看不到这种类型的桌子的目的。 If you have table A that is one-to-many to table B then A is going to still have a PK and B will still have a PK. 如果表A与表B一对多,则A将仍然具有PK,而B将仍然具有PK。 A would normally contain a FK to B. A通常包含B的FK。

So in the Master_Table you will have to store A PK, B FK which is just a duplicate of what is already there. 因此,在Master_Table中,您将必须存储A PK,B FK,这仅是已经存在的副本。 The only thing you will 'lose' is the FK in table A but you just migrated it into a giant table that is hard to deal with by the database, the dba, and anyone coding using the db. 您唯一会丢失的是表A中的FK,但您只是将其迁移到了一个巨大的表中,数据库,dba和使用db进行编码的任何人都难以处理。

Those table appear in Access most frequently and show up on the DailyWTF because they are insanely hard to read and understand. 这些表最经常出现在Access中,并显示在DailyWTF上,因为它们难以阅读和理解。

Oh! 哦! And a main problem is that to make the table ubiquitous you will have to make generic columns which will probably end up destroying data integrity. 一个主要问题是,要使该表无处不在,您将必须创建通用列,这可能最终会破坏数据完整性。

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

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