MongoDB 与 SQL Server 用于存储递归数据树

发布于 2025-01-04 10:44:44 字数 850 浏览 1 评论 0原文

我目前正在指定一个存储线程评论树的项目。

对于那些不熟悉我正在谈论的内容的人,我将解释一下,基本上每个评论都有一个父评论,而不仅仅是属于一个线程。目前,我正在研究存储这些数据的关系 SQL Server 模型,因为这是我所习惯的。它看起来像这样:

Id int  --PK
ThreadId int  --FK
UserId int  --FK
ParentCommentId int  --FK (relates back to Id)
Comment nvarchar(max)
Time datetime

我所做的就是选择 ThreadId 的所有注释,然后在代码中递归地构建我的对象树。我还进行了连接以获取用户名等信息。

在我看来,像 MongoDB 这样的 NoSql 文档存储可能是此类模型的更好选择。但我对此一无所知。

  • 如果我选择 MongoDB 会有哪些陷阱?
  • 如果我将其作为文档存储在 MongoDB 中,我是否必须在每个评论中包含用户名,以防止自己必须按键提取每个用户记录,因为它不是“关系型”?
  • 当您使用 MongoDB 时,您是否必须积极地将“相关”数据缓存到您需要的对象上?

编辑:我确实找到了关于在 MongoDB 中存储信息树的这篇文章。鉴于我的要求之一是能够向登录用户列出他最近的评论列表,我现在强烈倾向于仅使用 SQL Server,因为我认为我无法做任何聪明的事情使用 MongoDB 将带来真正的性能优势。但我可能是错的。我真的希望有关此事的一位(或两位)专家能够提供更多信息。

I'm currently specing out a project that stored threaded comment trees.

For those of you unfamiliar with what I'm talking about I'll explain, basically every comment has a parent comment, rather than just belonging to a thread. Currently, I'm working on a relational SQL Server model of storing this data, simply because it's what I'm used to. It looks like so:

Id int  --PK
ThreadId int  --FK
UserId int  --FK
ParentCommentId int  --FK (relates back to Id)
Comment nvarchar(max)
Time datetime

What I do is select all of the comments by ThreadId, then in code, recursively build out my object tree. I'm also doing a join to get things like the User's name.

It just seems to me that maybe a document storage like MongoDB which is NoSql would be a better choice for this sort of model. But I don't know anything about it.

  • What would be the pitfalls if I do choose MongoDB?
  • If I'm storing it as a Document in MongoDB, would I have to include the User's name on each comment to prevent myself from having to pull up each user record by key, since it's not "relational"?
  • Do you have to aggressively cache "related" data on the objects you need them on when you're using MongoDB?

EDIT: I did find this arcticle about storing trees of information in MongoDB. Given that one of my requirements is the ability to list to a logged in user a list of his recent comments, I'm now strongly leaning towards just using SQL Server, because I don't think I'll be able to do anything clever with MongoDB that will result in real performance benefits. But I could be wrong. I'm really hoping an expert (or two) on the matter will chime in with more information.

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

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

发布评论

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

评论(1

贵在坚持 2025-01-11 10:44:44

在 Mongo(和其他文档数据库)中存储分层数据的主要优点是能够存储数据的多个副本,从而使不同用例的查询更加高效。在您的情况下,如果将整个线程存储为分层嵌套文档,那么检索整个线程会非常快,但您可能还希望将每个注释存储为未嵌套的或可能存储在用户记录下的数组中以满足您的需要第二个要求。由于任意嵌套,我认为 Mongo 无法通过用户 ID 有效地索引您的层次结构。

与所有 NoSQL 存储一样,您可以通过扩展到大量数据节点来获得更多好处,从而允许许多并发读取器和写入器。

希望有帮助

The main advantage of storing hierarchical data in Mongo (and other document databases) is the ability to store multiple copies of the data in ways that make queries more efficient for different use cases. In your case, it would be extremely fast to retrieve the whole thread if it were stored as a hierarchical nested document, but you'd probably also want to store each comment un-nested or possibly in an array under the user's record to satisfy your 2nd requirement. Because of the arbitrary nesting, I don't think that Mongo would be able to effectively index your hierarchy by user ID.

As with all NoSQL stores, you get more benefit by being able to scale out to lots of data nodes, allowing for many simultaneous readers and writers.

Hope that helps

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