在关系数据库中建模层次结构/目录

发布于 2024-11-26 13:39:55 字数 747 浏览 2 评论 0原文

我想在 mysql 表中对层次结构/目录进行建模,如下所示。您可以在下面看到我正在考虑的表格架构。 然而,我所说的目录将由 100.000 个元素组成,深度约为 5-10 个级别。此外,我们将有一个标签池,目录的每个元素都可以链接到一个或多个标签。所以我想知道是否有更好的方法。我读到有些人决定设计不规范的表来影响高性能,我也在评估这个案例。

ps:有些人使用多路树在编程语言级别对此进行建模,因此如何在数据库中结束的问题仍然存在。

hierarchy:
A
| -> 1
     |->1
     |->2
| -> 2
| -> 3
B
| -> 1
| -> 2

table:
 ___________________________
| id      |element | father |
|---------------------------|
|  000    |   A    |  null  |
|  001    |   1    |  000   |
|  002    |   1    |  001   |
|  003    |   2    |  001   |
|  004    |   2    |  000   |
|  005    |   3    |  000   |
|  006    |   B    |  null  |
|  001    |   1    |  006   |
|  002    |   2    |  006   |
-----------------------------

I would like to model a hierarchy/directory like what you see below, in a mysql table. You can see below the table schema I was thinking.
However, the directory Im talking about would be comprised of 100.000 elements and the depth would be ~5-10 levels. Moreover, we will have a tags pool and each element of the directory may be linked to one or more tags. So I was wondering if there is better approach. I was reading that some people decide to design tables that are not canonical for the shake of high performance and I am evaluating this case too.

ps: some people use Multi-way Trees to model this in the programming language level so the question how this ends up in the database remains.

hierarchy:
A
| -> 1
     |->1
     |->2
| -> 2
| -> 3
B
| -> 1
| -> 2

table:
 ___________________________
| id      |element | father |
|---------------------------|
|  000    |   A    |  null  |
|  001    |   1    |  000   |
|  002    |   1    |  001   |
|  003    |   2    |  001   |
|  004    |   2    |  000   |
|  005    |   3    |  000   |
|  006    |   B    |  null  |
|  001    |   1    |  006   |
|  002    |   2    |  006   |
-----------------------------

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

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

发布评论

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

评论(1

秋千易 2024-12-03 13:39:55

一个非常快的层次树是一个嵌套集或 Celko 树,它有点像二叉树,或者当你有 MySQL 存储引擎时的哈夫曼树。缺点是删除和插入成本高昂。其他 RDBMS 也支持递归查询。一般来说,我没有看到很多嵌套集。创建和维护似乎也很复杂。当嵌套集合太复杂并且RDBMS不支持递归查询时,还有物化路径。

  1. http://www.ibase.ru/devinfo/DBMSTrees/sqltrees.html
  2. http://en.wikipedia.org/wiki/Binary_tree
  3. http:// en.wikipedia.org/wiki/Huffman_coding
  4. http://www.postgresql.org/docs/8.4/static/queries-with.html
  5. 是否可以进行递归 SQL 查询?
  6. http://www.cybertec.at/pgbook/node122.html

A very fast hierachical tree is a nested set or a Celko-tree, it's a bit like a binary tree, or a huffman tree when you have a MySQL storage engine. Disadvantage is expensive deletion and insertion. Other RDBMS supports also recursive queries. In general I didn't see many nested sets. It seems to be complicated too create and maintain. When the nested set is too complicated and the RDBMS doesn't support recursive queries there is also materialized path.

  1. http://www.ibase.ru/devinfo/DBMSTrees/sqltrees.html
  2. http://en.wikipedia.org/wiki/Binary_tree
  3. http://en.wikipedia.org/wiki/Huffman_coding
  4. http://www.postgresql.org/docs/8.4/static/queries-with.html
  5. Is it possible to make a recursive SQL query?
  6. http://www.cybertec.at/pgbook/node122.html
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文