数据库设计:EAV 选择?

发布于 2024-11-29 03:35:27 字数 982 浏览 2 评论 0原文

这只是一个数据库概念问题:以下 EAV 模型的优缺点是什么?

模型 1:

TABLE: attribute_value
======================================
| id | fk_id | attribute | value     |
======================================
| 1  | 10    | FName     | John      |
| 2  | 10    | Lname     | Doe       |
| 3  | 55    | FName     | Bob       |
| 4  | 55    | Lname     | Smith     |
--------------------------------------

模型 2:

TABLE: attribute
==================
| id | attribute |
==================
| 1  | FName     |
| 2  | Lname     |
------------------

TABLE: value
=====================================
| id | attribute_id | fk_id | value |
=====================================
| 1  | 1            | 10    | John  |
| 2  | 2            | 10    | Doe   |
| 3  | 1            | 55    | Bob   |
| 4  | 2            | 55    | Smith |
-------------------------------------

我认为模型 2 的一个好处是属性不包含重复项。

This is just a database concept question: what are the pros and cons of the following model for EAV?

Model 1:

TABLE: attribute_value
======================================
| id | fk_id | attribute | value     |
======================================
| 1  | 10    | FName     | John      |
| 2  | 10    | Lname     | Doe       |
| 3  | 55    | FName     | Bob       |
| 4  | 55    | Lname     | Smith     |
--------------------------------------

Model 2:

TABLE: attribute
==================
| id | attribute |
==================
| 1  | FName     |
| 2  | Lname     |
------------------

TABLE: value
=====================================
| id | attribute_id | fk_id | value |
=====================================
| 1  | 1            | 10    | John  |
| 2  | 2            | 10    | Doe   |
| 3  | 1            | 55    | Bob   |
| 4  | 2            | 55    | Smith |
-------------------------------------

One benefit I see with Model 2 is that the attribute does not contain duplicates.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

尹雨沫 2024-12-06 03:35:27

尽管如图所示是简约的,但 Model2 的属性表将元数据的概念引入到组合中,并带来了它带来的所有好处。 Model2 还有其他优点,例如与较小的行大小(值表)相关的性能提升,但我想重点关注元数据概念。

即使模型 2 的属性表也构成了所有有效属性的存储库(对于模型 1,需要运行各种聚合查询才能获得这样的列表)。此外,按原样,存储库足以引入外键约束,以帮助维护数据集的完整性(使用模型 1,需要对数据集进行外部形式的验证) 通过一些简单的添加

,属性表可以成为可用于各种目的的通用存储库,例如该表可能包含以下一些

  • 信息,例如每个属性的易于显示的名称。
  • 一些指示字段类型的标志(数字、字符串、字符串)日期等),用于区分处理/处理
  • 存储基础属性的特定值表(模型仅显示一个表,但优化/缩放有时会提示拆分表)
  • 该属性可以作为其自己的列存储在“值”表(又是一种优化形式,本质上是两全其美:EAV 模型模式的灵活性,以及​​传统关系模型对于最常用和/或最常见属性的性能所有实体。
  • 能够在不影响主表的情况下重命名属性。仅在元数据级别进行更改。
  • 各种面向应用的语义。例如,应提供特定属性作为基本搜索字段与高级搜索字段之一的指示符。

简而言之,属性表成为一种资源,使应用程序能够真正由数据驱动(或更准确地说,数据驱动)。事实上,您可能还喜欢一个实体表,即收集与各种实体类型相关的元数据的表:哪些是不同的实体类型,哪些属性允许哪些实体类型等。

现在...请注意来自的评论zerkms,位于问题本身下方。尽管有这么多优点,EAV 模型也有其缺点和挑战,正如所暗示的查询的复杂性以及性能问题。然而,这些问题不应先验地取消 EAV 的资格:在许多用例中,EAV 是更好的方法。
假设 EAV 是模型 2 的选择,甚至稍微复杂一点的东西也绝对优于模型 1。

Although minimalist as shown, the attribute table of Model2 introduces the concept of meta-data into the mix, with all the good that comes from it. There are other advantages to Model2, for example the performance gains associated with smaller row size (of the Value table), but I'd like to focus on the meta-data concept.

Even as-is Model2's attribute table constitute a repository of all valid attributes (with model1 one would need to run an aggregate query of sorts to get such a list). Also, and as-is, the repository is sufficient to introduce foreign key constraints to help maintaining the integrity of the dataset (with Model 1 one would need external forms of validation of the values stored in attribute column.

With a few simple additions, the attribute table can become a versatile repository which can be used for various purposes. For example the table may include some of the following

  • info such as the display-friendly name of each attribute
  • some flags indicating the type of field (numeric vs. string vs. date etc.), for differentiated handling / processing
  • the particular Value table where the underlying attribute is stored (Model only shows one table but optimization/scaling sometimes prompts splitting the tables)
  • the fact that the attribute may be stored as its own column in the "Value" table (again a form of optimization, essentially getting the best of both worlds: the flexibility of the schema of the EAV model but the performance of traditional relational model for the attributes that are the most used and/or the most common to all entities.
  • the ability to rename attributes, without disturbing the main table. Changes at meta-data level only.
  • various application-oriented semantics. For example indicators that a particular attribute should be offered as one of the basic vs. advanced search fields.

In a nutshell, the attribute table becomes a resource which allows the application to be truly data-driven (or more precisely, meta data driven). Indeed you may also like an entity table i.e. one where the metadata pertaining to the various entities types are gathered: which are the different entity types, which attributes are allowed for which entity type etc.

Now... do pay heed to the comment from zerkms, below the question itself. For all its benefits, the EAV model also comes with its share of drawbacks and challenges, as hinted the complexity of the queries come to mind, and also performance issues. These concerns should however not disqualify, a priori, EAV: there are many use cases where EAV is a better approach.
Assuming EAV is the choice then Model2, or even something slighly more sophisticated is definitively superior to model1.

丢了幸福的猪 2024-12-06 03:35:27

在概念层面上,这两个模型实际上是相同的。您刚刚将字符串替换为 ID 号。就这样。

就外键而言,如果您愿意,可以对模型 1 中的“属性”施加外键约束。

就优缺点而言,EAV 的这两种实现之间实际上没有区别。所有Bill Karwin 的观点都适用于两者。

At the conceptual level, these two models are virtually identical. You've just replaced strings with ID numbers. That's all.

As far as foreign keys go, you could impose a foreign key constraint on "attribute" in Model 1 if you wanted to.

As far as pros and cons go, there's really no difference between these two implementations of EAV. All Bill Karwin's points apply to both.

流殇 2024-12-06 03:35:27

对于模型 2,您可以在 attribute_id 上施加外键,并确保只有定义的属性才能进入表。

同样对于模型 2,您可以更快地查询来获取具有某些属性 id 的值,因为如果您创建外键(索引),查询将会更快。

For Model 2, you can impose a Foreign-Key on attribute_id and make sure that the only defined attributes can enter the table.

Also for Model 2, you can have faster queries to get values with certain attributes ids since if you make a foreign-key (index), querying will be faster.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文