存储分层标签的最佳方式
我有一个列表,其中每个列表条目都标有多个标签。每个标签还可以有子标签。列表中的每个条目可以有多个标签。
例如,谈论汽车的列表条目可以具有名为“汽车”、“车辆”、“法拉利”的标签。
我应该能够查看标签的层次结构,如下所示。此外,每个条目的标签数量以及标签的深度不应受到限制。
我如何存储这些数据?我愿意使用任何类型的 DBMS。
I have a list, where each list entry is tagged with multiple tags. Each tag can also have child tags. Every entry in the list can have more than one tags.
For example, a list entry that talks about cars can have tags called "cars", "vehicles", "ferrari".
I should be able to view a hierarchy of tags, like shown below. Also, there should be no limit to number of tags per entry, and also how deep the tags can go.
How do I store this data? I am open to using any type of DBMS.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
最简单的方法是使用父/子解决方案,但使用此数据模型编写有效的查询非常困难。
在 MySQL 中管理分层数据 是一篇关于分层数据结构的非常好的文章。我想其中大部分也可以应用于其他数据库系统。
The naive approach would be a parent / child solution, but it's very difficult to write efficient queries with this data model.
Managing Hierarchical Data in MySQL is a pretty good article about hierarchical data structures. I suppose most of it can be applied to other database systems, too.
我认为这对于任何数据库来说都是最简单的方法:
tag (id, name,parent_id)
,其中parent_id
指的是父标签的id
。I think this is the simplest way for any database:
tag (id, name, parent_id)
, whereparent_id
refers toid
of the parent tag.您正在使用两个数据源,但是,您似乎正在混合两者。
一种数据是您的列表条目,它似乎是线性的、非分层的。
例如,电影列表。
另一个数据源是分层数据的集合(“标签目录”)。
例如电影风格列表。
每部电影都可以与多种电影风格相关联:
在数据库设计方面,您应该至少有 3 个表或实体对象:
祝你好运。
You are using 2 sources of data, but, seems you are mixing both.
One data is your list entries, that seems to be lineal, non hierarchical.
For example, a list of movies.
The other source of data, its a collection of hierarchical data ("tags catalog").
For example a list of movie styles.
Each movie can be associated with several movie styles:
In terms of Database design, you should have unleast 3 tables or Entity Objects:
Good Luck.
使用 XML 格式,这将帮助您将节点存储为父节点和子节点
它可以有n个节点并且易于形成和处理。
注意:下面只是一个例子,所以你可以通过这种方式处理数据。
我想这可能对你有帮助。
Use the XML format which will help you in storing the nodes as parent and child
It can have n number of nodes and easy to form and handle.
Note: The below one is just an example, So by this way you can handle the data.
I think this may help you.
这就是我解决这个问题的方法:首先,我将绘制一个领域模型。在你的情况下,它看起来像:
这使得问题变得明确,没有任何疑问的余地。
现在,将其转换为数据模型。这非常简单:为每个关系引入合适的 PrimaryKey-ForeignKey 映射。多对多关系应该使用中间的新表分成两个 1-M 关系。
您在此阶段拥有的数据模型在功能上应该是正确的,但可能存在性能问题。现在是您关注所需查询并相应优化表结构的时候了。
(从领域模型开始的另一个类似的细化之旅也将为您提供最终类模型的设计)
希望这种方法有所帮助。
This is how I would approach the problem:First, I will draw a domain model. In your case it looks like:
This makes the problem explicit leaving no room for doubts.
Now, translate this to a data-model. This is pretty straightforward: for each relation introduce suitable PrimaryKey-ForeignKey mappings.Many-to-Many relations should be split into two 1-M relations using a new table in between.
The data-model you have at this stage should be functionally correct, but could have performance issues. Now is the time for you to focus on the queries that you would want and optimize the table structure accordingly.
(Another similar refinement trip starting from the domain model will give you the design for the final class model as well)
Hope this approach helps.
请参阅我的回答。我存储所有级别的父级 - 树的构建和查询所有后代非常容易。
See my answer here. I store parents for all the levels - the tree building and querying all descendants is extremely easy.