[英]Implementing inheritance in MySQL: alternatives and a table with only surrogate keys
This is a question that has probably been asked before, but I'm having some difficulty to find exactly my case, so I'll explain my situation in search for some feedback: 这个问题以前可能已经提出过,但是我很难找到确切的案例,因此我将在寻求反馈的过程中解释我的情况:
I have an application that will be registering locations, I have several types of locations, each location type has a different set of attributes, but I need to associate notes to locations regardless of their type and also other types of content (mostly multimedia entries and comments) to said notes. 我有一个要注册位置的应用程序,我有几种类型的位置,每种位置类型都有一组不同的属性,但是我需要将注释与位置相关联,而不论它们的类型以及其他内容类型(主要是多媒体条目和注释)。 With this in mind, I came up with a couple of solutions:
考虑到这一点,我提出了一些解决方案:
Create a table for each location type, and a "notes" table for every location table with a foreign key, this is pretty troublesome because I would have to create a multimedia and comments table for every comments table, eg: 为每种位置类型创建一个表,并为每个具有外键的位置表创建一个“注释”表,这非常麻烦,因为我必须为每个注释表创建一个多媒体和注释表,例如:
LocationTypeA
LocationTypeA
- ID
ID
- Attr1
ATTR1
- Attr2
ATTR2
LocationTypeA_Notes
LocationTypeA_Notes
- ID
ID
- Attr1
ATTR1
- ...
...
- LocationTypeA_fk
LocationTypeA_fk
LocationTypeA_Notes_Multimedia
LocationTypeA_Notes_Multimedia
- ID
ID
- Attr1
ATTR1
- ...
...
- LocationTypeA_Notes_fk
LocationTypeA_Notes_fk
And so on, this would be quite annoying to do, but after it's done, developing on this structure should not be so troublesome. 依此类推,这样做会很烦人,但是完成之后,在此结构上进行开发就不会那么麻烦。
Create a table with a unique identifier for the location and point content there, like so: 创建一个具有唯一标识符的表,并在其中指向内容,如下所示:
Location
位置
- ID
ID
LocationTypeA
LocationTypeA
- ID
ID
- Attr1
ATTR1
- Attr2
ATTR2
- Location_fk
Location_fk
Notes
笔记
- ID
ID
- Attr1
ATTR1
- ...
...
- Location_fk
Location_fk
Multimedia
多媒体
- ID
ID
- Attr1
ATTR1
- ...
...
- Notes_fk
Notes_fk
As you see, this is far more simple and also easier to develop, but I just don't like the looks of that table with only IDs (yeah, that's truly the only objection I have to this, it's the option I like the most, to be honest). 如您所见,这要简单得多,而且开发起来也容易,但我只是不喜欢仅包含ID的表的外观(是的,这确实是我唯一的反对意见,这是我最喜欢的选项, 老实说)。
Similar to option 2, but I would have an enormous table of attributes shaped like this: 与选项2相似,但是我会有一个巨大的属性表,其形状如下:
Location
位置
- ID
ID
- Type
类型
Attribute
属性
- Name
名称
- Value
值
And so on, or a table for each attribute; 依此类推,或者每个属性都有一个表格; a la Drupal.
德拉普拉(la Drupal)。 This would be a pain to develop because then it would take several insert/update operations to do something on a location and the Attribute table would be several times bigger than the location table (or end up with an enormous amount of attribute tables);
开发将是一件痛苦的事情,因为这将需要多个插入/更新操作才能在某个位置上执行某些操作,并且属性表将比位置表大几倍(或者最终会产生大量的属性表); it also has the same issue of the surrogate-keys-only table (just it has a "type" now, which I would use to define the behavior of the location programmatically), but it's a pretty solution.
它也具有仅代理键表的问题(只是现在有了一个“类型”,我将使用它来以编程方式定义位置的行为),但这是一个很好的解决方案。
So, to the question: which would be a better solution performance and scalability-wise?, which would you go with or which alternatives would you propose? 那么,问题来了:哪种方法在性能和可伸缩性方面会更好呢?您会选择哪种方案? I don't have a problem implementing any of these, options 2 and 3 would be an interesting development, I've never done something like that, but I don't want to go with an option that will collapse on itself when the content grows a bit;
我没有实现这些问题,选项2和3会是一个有趣的发展,我从未做过类似的事情,但是我不想使用一个选项,当内容出现时它会自行折叠增长一点; you're probably thinking "why not just use Drupal if you know it works like you expect it to?", and I'm thinking "you obviously don't know how difficult it is to use Drupal, either that or you're an expert, which I'm most definitely not".
您可能在想“如果您知道它像您期望的那样工作,为什么不使用Drupal?”,而我在想“您显然不知道使用Drupal有多么困难,无论是那样还是您专家,我绝对不是。”
Also, now that I've written all of this, do you think option 2 is a good idea overall?, do you know of a better way to group entities / simulate inheritance? 另外,既然我已经编写了所有这些内容,那么您认为选项2总体上是一个好主意吗?您是否知道对实体进行分组/模拟继承的更好方法? (please, don't say "just use inheritance!", I'm restricted to using MySQL).
(请不要说“只使用继承!”,我仅限于使用MySQL)。
Thanks for your feedback, I'm sorry if I wrote too much and meant too little. 感谢您的反馈,对不起,我写的太多了,也太少了。
ORM systems usually use the following, mostly the same solutions as you listed there: ORM系统通常使用以下解决方案,与您在此处列出的解决方案基本相同:
One table per hierarchy 每个层次结构一张表
Pros: 优点:
Cons: 缺点:
When to use: 何时使用:
One table per concrete class 每个具体班级一张桌子
Pros: 优点:
Cons: 缺点:
When to use: 何时使用:
One table per class 每班一张桌子
Pros: 优点:
Cons: 缺点:
When to use: 何时使用:
Generic Schema 通用模式
Pros: 优点:
Cons: 缺点:
When to use: 何时使用:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.