如何将关系数据库用作基于文档的数据库?

发布于 2024-08-15 07:12:32 字数 374 浏览 6 评论 0原文

为了制作一个文档管理系统,我正在考虑像 MongoDB 这样的文档存储,但是因为我对常规数据库(Firebird、Sql Server、Mysql)有更多的经验,所以我想知道是否可以在关系数据库之上建立文档存储模型。

无模式文档存储的优点:

  • 非常适合存储有关文件的任意元数据的任务
  • 无需升级模式
  • 根据 mongodb,对于像视频这样的 BLOB 具有出色的性能
  • 更容易扩展

但对于关系型存储:

  • 引用完整性
  • 更好的工具
  • 更具弹性崩溃&损坏
  • SQL

那么,在这种情况下关系数据库如何工作呢?

For make a document managment system, I'm looking at document stores like MongoDB, but because I have more experience with regular databases (Firebird, Sql Server, Mysql) I wonder if is possible model a document store on top a relational one.

The advantages about a document store, schema less:

  • Fit well to the task of store arbitrary metadata about files
  • No need to upgrade schemas
  • According to mongodb, excellent performance for BLOB like video
  • Easier scalability

But with a relational one:

  • Referencial integrity
  • Better tooling
  • More resilent to crash & corruptions
  • SQL

So, how could work a relational database in this case?

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

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

发布评论

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

评论(2

千里故人稀 2024-08-22 07:12:32

考虑 Martin Fowler 的序列化 LOB 模式:

CREATE TABLE Documents (
  documentid SERIAL PRIMARY KEY,
  -- fixed relational attributes ...
  document TEXT -- contains XML, YAML, whatever
);

您可以将任何具有动态属性的半结构化数据放入文档 列。您只是无法轻松地使用 SQL 谓词来搜索或按该 blob 中的字段进行排序。但无论如何你都不能——变量属性是一个非关系概念,无论如何在 SQL 中支持它们都是很尴尬的。

您可以使用混合方法,将一些固定属性存储在常规列中,并将所有可变属性存储在 blob 中。

这指出了为什么存在面向文档的数据库。它们旨在解决关系范式选择不支持的问题。但面向文档的数据库无法实现关系数据库所做的一些很酷的事情,例如引用完整性甚至数据类型一致性。

Consider Martin Fowler's Serialized LOB pattern:

CREATE TABLE Documents (
  documentid SERIAL PRIMARY KEY,
  -- fixed relational attributes ...
  document TEXT -- contains XML, YAML, whatever
);

You can put any semi-structured data with dynamic attributes into the document column. You just can't easily use SQL predicates to search or sort by fields in that blob. But you couldn't anyway -- variable attributes is a non-relational concept, and it's awkward to support them in SQL no matter what.

You can use a hybrid approach, storing some fixed attributes in conventional columns, and all the variable attribute stuff in the blob.

This points out why document-oriented databases exist. They are designed to solve a problem that the relational paradigm has chosen not to support. But document-oriented databases don't do some of the cool things that relational databases do, like referential integrity and even data type coherency.

请恋爱 2024-08-22 07:12:32

一个简单的 MySQL 示例:

CREATE TABLE Docs (
  id INT,
  attr VARCHAR(255),
  value BLOB,
  PRIMARY KEY (id, attr),
  KEY attr_index (attr)
)

一旦您拥有了该属性,您就可以向文档添加任何属性并在值中填充任何内容,并且您可以在文档表上使用自联接来执行复杂的查询,例如:

SELECT * FROM Docs AS d1, docs AS d2 WHERE d1.attr = "foo" AND d2.attr = "bar"

返回具有 foo 和 bar 属性的文档。

A simple MySQL example:

CREATE TABLE Docs (
  id INT,
  attr VARCHAR(255),
  value BLOB,
  PRIMARY KEY (id, attr),
  KEY attr_index (attr)
)

Once you have that you can add any attribute to a document and stuff anything in the value, and you can use self joins on the document table to do complex queries like:

SELECT * FROM Docs AS d1, docs AS d2 WHERE d1.attr = "foo" AND d2.attr = "bar"

Which returns documents with both foo and bar attributes.

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