简体   繁体   English

关系数据库中面向对象分析模式的实现

[英]Implementation of object-oriented analysis patterns in a relational database

I have recently started reading Martin Fowler's "Analysis Patterns" in an attempt to improve my data modelling skills and I find the book really interesting and enlightening as far as object-oriented approaches are concerned. 我最近开始阅读Martin Fowler的“ Analysis Patterns”,以尝试提高我的数据建模技能,并且我发现这本书真的很有趣,并且对于面向对象的方法很有启发性。 However, although I think I understand how most of the patterns would translate into a relational database schema, I fail to see how to implement them in a realistic way. 但是,尽管我认为我了解大多数模式将如何转换为关系数据库模式,但是我看不到如何以现实的方式实现它们。

For example, implementing the "Measurement" pattern (chapter 3.4) in a (non object-)relational database would require: 例如,在(非对象)关系数据库中实现“度量”模式(第3.4章)将需要:

  • 1 table to store Persons. 1个用于存储人员的表。
  • 1 table to store Measurements for a Person (N-1 relationship). 1个表,用于存储人的度量(N-1关系)。
  • 1 table to store the Quantity of a Measurement (1-1 relationship). 1个表,用于存储度量值(1-1关系)。
  • 1 table to store the Phenomenon Types that may characterise a Measurement (1-N relationship). 1个表,用于存储可以表征度量(1-N关系)的现象类型。

In an RDBMS all this amounts to several sort operations to realise joins and to a large number of records that need to be joined, especially if we're storing lots of Persons and lots of Measurements for each Person. 在RDBMS中,所有这些都相当于实现排序的多种排序操作以及需要连接的大量记录,尤其是当我们要存储大量人员和每个人员很多度量时。 I suspect it would not perform well. 我怀疑它的性能不好。

The question, I suppose, is whether I am correct in my "suspicion" and if someone could point me to the right direction in terms of books or other sources that discuss the implementation of such analysis patterns to relational databases. 我想的问题是,我是否对我的“怀疑”是正确的,是否有人可以就讨论这种分析模式对关系数据库的实现的书籍或其他资料向我指出正确的方向。

Thank you very much in advance, Nico 尼科,非常感谢您

That's not complicated. 这并不复杂。 From an Entity-Relationship modelling perspective, you're describing what sounds like 4 entities: 从实体关系建模角度,您正在描述听起来像4个实体的情况:

  • Person
  • Measurement 测量
  • Quantity 数量
  • Phenomenon Type 现象类型

And a few relationships between them: 以及它们之间的一些关系:

  • Each Person has zero-to-many Measurements; 每个人都有零到多的度量; each Measurement measures one-and-only-one Person. 每次测量仅测量一个人。
  • Each Measurement has one-and-only-one Quantity; 每个度量都有一个唯一的数量; Each Quantity is of one-and-only-one Measurement. 每个数量都是唯一的一个度量。
  • Each Measurement describes one-and-only-one Phenomenon Type; 每个度量描述唯一的一种现象类型; each Phenomenon Type has zero-to-many Measurements. 每个现象类型都有零到多的测量值。

So...4 tables and 3 relationships in the database. 所以...数据库中有4个表和3个关系。

The model so described is probably incorrect, though: Assuming that he 1:1 relationship between Measurement and Quantity is mandatory — each Measurement has a single Quantity; 但是,这样描述的模型可能是错误的:假定“度量”与“数量”之间的1:1关系是强制性的-每个度量都有一个“数量”; each Quantity describes a single Measurement — is a smell. 每个数量描述一个测量值–是一种气味。

Relationships with 1:1 cardinality are often (usually?) an indication that the two entities involved are really partial views of the same underlying logical entity. 与1:1基数的关系通常(通常是?)表明所涉及的两个实体实际上是同一基础逻辑实体的部分视图。 The model should be refactored to a single entity carrying all the attributes. 该模型应重构为带有所有属性的单个实体。 Doing that refactoring brings you down to 3 tables and 2 relationships. 进行重构将使您下降到3个表和2个关系。 Note, however, that this is not necessarily true with optional unary relationships (0:1). 但是请注意,对于可选的一元关系(0:1),不一定是正确的。

Either way, that's nothing, provided the physical database design implementing the logical entity model has proper covering indices. 不管采用哪种方法,只要实现逻辑实体模型的物理数据库设计具有适当的覆盖索引,都可以。

The thing with a properly normalized model is that you typically have many narrowish tables: narrow tables mean more rows per data page and thus faster seeks. 具有正确规范化模型的问题是,您通常具有许多狭窄的表:狭窄的表意味着每个数据页有更多的行,因此查找速度更快。 One can always denormalize if there is an actual performance problem due to the database design. 如果由于数据库设计而导致实际的性能问题,您总是可以将其归一化。 If you start with a denormalized model, once you've got a live database with gigabytes of data in it, putting into proper form gets difficult and time consuming. 如果您非规范化模型开始 ,那么一旦有了包含千兆字节数据的实时数据库,将其放入正确的格式将变得困难且耗时。

I do not know the book you mention. 我不知道你提到的书。 Hence, I do not understand the difference between "measurement" and "quantity". 因此,我不理解“度量”和“数量”之间的区别。 I am assuming that "Measurement" would be the related to the act of measuring, ie contain the date and time of the measurement, and "quantity" is the value measured, like 180 for the height of a person in centimeters. 我假设“度量”与度量行为有关,即包含度量的日期和时间,“数量”是度量的值,例如一个人的身高(以厘米为单位)为180。

For analysis using relational databases, a star schema is a common approach: You would have one central fact table containing the quantities, and foreign keys to the dimension tables like Date , Phenomenon , and Person . 对于使用关系数据库进行分析, 星型模式是一种常见的方法:您将拥有一个包含数量的中央事实表,以及维表(如DatePhenomenonPerson外键。

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

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