如何对未知属性的数据进行建模?

发布于 2024-10-14 19:59:06 字数 475 浏览 1 评论 0原文

对需要查询但无法预先完全定义的数据进行建模的好方法是什么?

例如...假设我想对有关世界各国的信息进行建模。每个国家/地区都有人口国旗语言列表,这很简单。但是,假设我们还想对国家棒球队的胜/负记录进行建模,当然,并不是所有国家都有这样的记录。或者,我们想追踪他们的国王和国王的血统。皇后区(同样,显然不适用于大多数国家)。或者,我们决定对平均氏族成员一生中将建造的蒙古包数量进行建模

无论如何,重点是,我们不(也永远不会)知道会发生什么,直到它降临到我们身上。有哪些既可扩展又可查询的方法?

这对于以文档为中心数据库(MongoDB?)来说可能是一个很好的用途,或者也许某些设计模式可以应用于经典的关系数据库?

What are good ways to model data that will need to be queried but where it's impossible to fully define up front?

For instance... say I want to model information about the countries of the world. Each country has a population, a flag and a list of languages, that's easy enough. But say we also want to model the win/loss record of their national baseball team and not all countries have one, of course. Or, we want to track the lineage of their kings & queens (again, obviously not applicable to most countries). Or, we decide we want to model the number of yurts the average clan member will erect in a lifetime.

Anyway, point is, we don't (and won't ever) know what's coming until it hits us. What approaches are there that are both scalable and query-able?

Is this, perhaps, a good use for a Document-centric database (MongoDB?) or perhaps some design pattern could be applied to the classic Relational database?

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

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

发布评论

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

评论(4

时光磨忆 2024-10-21 19:59:06

您可以在纯关系数据库中做到这一点,并享受关系数据库的速度和强大功能。

您需要使用第六范式,这是具有完全完整性和控制力的正确方法。

EAV 是 6NF 的子集,没有完整性或控制,并且通常实施得非常糟糕。

我对这些问题的回答提供了对该主题的全面处理。由于上下文和提出的论点,最后一篇特别长。

EAV-6NF 答案一
EAV-6NF 答案二
EAV-6NF 答案三

You can do that in a pure Relational Database, and enjoy the speed and power of Relational databases.

You need to use Sixth Normal Form, the proper method with full integrityand control.

EAV is a subset of 6NF without the Integrity or control, and usualy very badly implemented.

My answers to these questions provide a full treatment of the subject. The last one is particularly long due to the context and arguments raised.

EAV-6NF Answer One
EAV-6NF Answer Two
EAV-6NF Answer Three

疯狂的代价 2024-10-21 19:59:06

所有数据库都应该能够随着时间的推移而发展。如果您拥有合适的人员和组织,那么在出现新属性时向模型添加新属性应该没有问题。

All databases ought to be capable of evolving over time. If you have the right people and organisation in place then you should have no problem adding new attributes to the model as they arise.

冬天的雪花 2024-10-21 19:59:06

您可以应用实体属性值模型,但它是rails中的PITA;我使用过 MongoDB,它非常适合您的需求。

You can apply the Entity Atribute Value Model but it is a PITA in rails; I have used MongoDB and it is great for what you need.

疾风者 2024-10-21 19:59:06

如果您使用 Rails,则可以在模型中使用 serialize :column_name,它会成功地为您保留大多数对象,而无需任何额外的工作。如果您认为不需要无模式 NoSQL 数据库,这可能是获得此功能最简单的方法。

class Country << ActiveRecord::Base
  serialize :data

  def add_statistic(name, value)
    data[name] = value
  end

  def get_statistic(name)
    data[name]
  end
end

这些方法有些多余;他们只是为了给你一个例子。

此类系统的最大缺点是您是否需要根据这些内容进行搜索或查询。毕竟,Rails 不会为您处理 Country.find_by_win_loss_record_of_national_basketball_team

If you're using Rails, you can use serialize :column_name in your model and it'll persist most objects successfully for you without any additional work. If you don't think you'll have a need for a schema-less NoSQL database, this is probably about the easiest thing you can do to get this functionality.

class Country << ActiveRecord::Base
  serialize :data

  def add_statistic(name, value)
    data[name] = value
  end

  def get_statistic(name)
    data[name]
  end
end

Those methods are somewhat superfluous; they're there just to show give you an example.

The biggest downside to this type of system is if you have a need to search or query based on these things. Rails won't handle Country.find_by_win_loss_record_of_national_basketball_team for you, after all.

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