将邻接列表模型和嵌套集模型结合起来是否被认为是糟糕的设计?

发布于 2024-08-23 08:24:44 字数 406 浏览 8 评论 0原文

我正在努力在 MySQL 中构建树结构,并一直在尝试不同的表示数据的方式。但无论怎么切,都存在不足之处。

嵌套集模型允许我轻松选择树的整个分支 - 这是我需要做的。然而,选择节点的直接子节点并不那么容易。

邻接列表模型非常适合获取直接子节点,但不太适合返回树的整个分支。

我想知道,构建这样的树结构有什么特别不好的地方吗:

TABLE: Tree
    ID
    name
    lft
    rgt
    parentID

所以我所拥有的是邻接列表模型和嵌套集模型都在同一个表中。这样我就可以使用 / 或,具体取决于我选择的情况。

人们的想法是什么?这是允许的吗? (花生酱和巧克力终于在一起了?)或者这被认为是糟糕的设计?

提前致谢,

I'm working on building a tree structure in MySQL and have been experimenting with different ways of representing the data. However, no matter how I slice it, there are shortcomings.

The nested sets model allows me to easily select entire branches of the tree - which I need to do. However it is not so easy to select immediate children of a node.

The adjacency list model is great for getting immediate children, but not so good for returning entire branches of the tree.

I'm wondering, is there anything particularly bad about building a tree structure like this:

TABLE: Tree
    ID
    name
    lft
    rgt
    parentID

So what I have is the adjacency list model and the nested sets model all in the same table. That way I can use either / or, depending on the circumstances of what I am selecting for.

What are peoples' thoughts? Is this allowed? (Peanut butter and chocolate together at last?) Or is this considered bad design?

Thanks in advance,

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

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

发布评论

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

评论(2

染年凉城似染瑾 2024-08-30 08:24:44

我本人强烈推荐它。这是 CakePHP 用于其树结构的样式,正如您所指出的,它为您提供了很大的灵活性。您只需要做更多的工作来确保树的完整性,但这是非常值得的。

I highly recommend it myself. This is the style which CakePHP uses for its tree structures, and as you pointed out, it gives you great flexibility. You just need to do a bit more work to ensure the integrity of your tree, but it's quite worth it.

中二柚 2024-08-30 08:24:44

不,它的设计一点也不差。至少在我看来。

然而我只会实现一个。例如,我使用邻接列表模型作为添加新元素和检索的主要方法。(显然是检索,因为它很容易)但是,我还为嵌套集添加了parentId。

这样做的好处是,我可以随时重建我的 ALM,并且可以轻松地快速移动内容,前提是我保留了parentId。

但是,我不会在您的代码中使用两者,只是将其放在后台...

编辑评论

您提到选择直接子项并不那么容易。我想你可能做错了什么。即使在邻接列表中选择直接子项也非常容易。

如果您创建一个新问题,我将回答如何做到这一点。

No, its not bad design at all. At least in my opinion.

I would however only implement one. For example, I used Adjacency List Model as my prime method of adding new elements and retreiving.. (obviously retreiving since its so easy) however, I also added parentIds for Nested Set.

The benefit of this is that I can rebuild my ALM at any time and can easily move things quickly providing I keep the parentIds.

I would not use both in your code however, just have it there in the background...

Edited Comments

You mention that it is not so easy to select immediate children. I think you might be doing something wrong. It is extremely easy even in Adjacency List to select the immediate children.

If you create a new question I will answer how to do it in that.

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