无架构 SQL 数据库表 - 实用的折衷方案
这个问题试图找到这个问题的实用解决方案< /a>.
我需要为我的 SQL 数据库进行半无模式设计。但是,我可以限制灵活性,将其硬塞到整个 SQL 范例中。将来可能会选择迁移到无模式数据库,但现在我仍坚持使用 SQL。
我在 SQL 数据库中有一个表(我们称之为 Foo
)。当向其中添加一行时,它需要能够存储任意数量的“元”字段。一个例子是能够附加任意元数据,如标签、协作者等。所有字段都是可选的,但问题是它们的类型不同。有些可能是数字,有些可能是文本等。
一个简单的设计将 Foo
链接到 OptionalValues
表,其中包含 name
、等字段value_type
、value_string
、value_int
、value_date
等看起来很直接,尽管它下降到整个 EAV 模型,它看起来相当浪费。另外,我想当它增长时,查询会非常慢。不过,我不希望按此表中的任何内容进行搜索或排序。我所需要的是,当我从 Foo 中获取一行时,这些额外的属性也应该是可以获得的。
在 SQL 数据库中是否有实现此类设置的最佳实践,或者我只是错误地看待整个事情?
This question is an attempt to find a practical solution for this question.
I need a semi-schema less design for my SQL database. However, I can limit the flexibility to shoehorn it into the entire SQL paradigm. Moving to schema less databases might be an option in the future but right now, I' stuck with SQL.
I have a table in a SQL database (let's call it Foo
). When an row is added to this, it needs to be able to store an arbitrary number of "meta" fields with this. An example would be the ability to attach arbitrary metadata like tags, collaborators etc. All the fields are optional but the problem is that they're of different types. Some might be numeric, some might be textual etc.
A simple design linking Foo
to a table of OptionalValues
with fields like name
, value_type
, value_string
, value_int
, value_date
etc. seems direct although it descends into the whole EAV model which Alex mentions on that last answer and it looks quite wasteful. Also, I imagine queries out of this when it grows will be quite slow. I don't expect to search or sort by anything in this table though. All I need is that when I get a row out of Foo
, these extra attributes should be obtainable as well.
Are there any best practices for implementing this kind of a setup in a SQL database or am I simply looking at the whole thing wrongly?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将字符串列“Metafields”添加到表“Foo”中,并将元数据作为 XML 或 JSON 字符串存储在那里。
Add a string column "Metafields" to your table "Foo" and store your metadata there as an XML or JSON string.