• <i id='XaTHK'><tr id='XaTHK'><dt id='XaTHK'><q id='XaTHK'><span id='XaTHK'><b id='XaTHK'><form id='XaTHK'><ins id='XaTHK'></ins><ul id='XaTHK'></ul><sub id='XaTHK'></sub></form><legend id='XaTHK'></legend><bdo id='XaTHK'><pre id='XaTHK'><center id='XaTHK'></center></pre></bdo></b><th id='XaTHK'></th></span></q></dt></tr></i><div id='XaTHK'><tfoot id='XaTHK'></tfoot><dl id='XaTHK'><fieldset id='XaTHK'></fieldset></dl></div>

    <small id='XaTHK'></small><noframes id='XaTHK'>

  • <tfoot id='XaTHK'></tfoot>
      <legend id='XaTHK'><style id='XaTHK'><dir id='XaTHK'><q id='XaTHK'></q></dir></style></legend>
        <bdo id='XaTHK'></bdo><ul id='XaTHK'></ul>
      1. 在关系数据库中存储树结构的已知方法有哪些?

        What are the known ways to store a tree structure in a relational DB?(在关系数据库中存储树结构的已知方法有哪些?)
          <tbody id='HeuN8'></tbody>

        <legend id='HeuN8'><style id='HeuN8'><dir id='HeuN8'><q id='HeuN8'></q></dir></style></legend>
        • <bdo id='HeuN8'></bdo><ul id='HeuN8'></ul>
          <i id='HeuN8'><tr id='HeuN8'><dt id='HeuN8'><q id='HeuN8'><span id='HeuN8'><b id='HeuN8'><form id='HeuN8'><ins id='HeuN8'></ins><ul id='HeuN8'></ul><sub id='HeuN8'></sub></form><legend id='HeuN8'></legend><bdo id='HeuN8'><pre id='HeuN8'><center id='HeuN8'></center></pre></bdo></b><th id='HeuN8'></th></span></q></dt></tr></i><div id='HeuN8'><tfoot id='HeuN8'></tfoot><dl id='HeuN8'><fieldset id='HeuN8'></fieldset></dl></div>
        • <small id='HeuN8'></small><noframes id='HeuN8'>

          <tfoot id='HeuN8'></tfoot>

                1. 本文介绍了在关系数据库中存储树结构的已知方法有哪些?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  有给你的父母一个FK"方法,即每条记录都指向它的父记录.
                  这是一个难以阅读的动作,但很容易维护.

                  There is the "put a FK to your parent" method, i.e. each records points to it's parent.
                  Which is a hard for read actions but very easy to maintain.

                  然后还有一个目录结构键"的方法:

                  And then there is a "directory structure key" method:

                  0001.0000.0000.0000 main branch 1
                  0001.0001.0000.0000 child of main branch one
                  etc
                  

                  超级容易阅读,但很难维护.
                  其他方式及其优缺点是什么?

                  Which is super easy to read, but hard to maintain.
                  What are the other ways and their cons/pros?

                  推荐答案

                  一如既往:没有最好的解决方案.每个解决方案都会使不同的事情变得更容易或更难.适合您的解决方案取决于您最常执行的操作.

                  As always: there is no best solution. Each solution makes different things easier or harder. The right solution for you depends on which operation you will do most.

                  使用父 ID 的朴素方法:

                  优点:

                  • 易于实施

                  • Easy to implement

                  很容易将一个大的子树移动到另一个父级

                  Easy to move a big subtree to another parent

                  插入物便宜

                  可在 SQL 中直接访问的所需字段

                  Needed Fields directly accessible in SQL

                  缺点:

                  • 检索整棵树是递归的,因此成本很高

                  • Retrieving a whole tree is recursive and therefore expensive

                  查找所有父对象也很昂贵(SQL 不知道递归......)

                  Finding all parents is expensive too ( SQL doesn't know recursions... )

                  修改的预序树遍历(保存起点和终点):

                  优点:

                  • 检索一整棵树既简单又便宜

                  • Retrieving a whole tree is easy and cheap

                  找到所有的父母很便宜

                  可在 SQL 中直接访问的所需字段

                  Needed Fields directly accessible in SQL

                  奖励:您也在其父节点中保存了子节点的顺序

                  Bonus: you're saving the order of childnodes within its parentnode too

                  缺点:

                  • 插入/更新可能非常昂贵,因为您可能需要更新很多节点

                  在每个节点中保存路径:

                  优点:

                  • 找到所有的父母很便宜

                  • Finding all parents is cheap

                  检索一整棵树的成本很低

                  Retrieving a whole tree is cheap

                  插入很便宜

                  缺点:

                  • 移动一整棵树的成本很高

                  • Moving a whole tree is expensive

                  根据您保存路径的方式,您将无法直接在 SQL 中使用它,因此您始终需要获取 &解析它,如果你想改变它.

                  Depending on the way you save the path, you won't be able to work with it directly in SQL, so you'll always need to fetch & parse it, if you want to change it.

                  收尾表

                  优点:

                  • 易于实施

                  • Easy to implement

                  找到所有的父母很便宜

                  插入很便宜

                  检索整棵树很便宜

                  缺点:

                  • 需要一个额外的表

                  • Needs an additional table

                  与其他方法相比占用大量空间

                  Takes up a lot of space compared to other approaches

                  移动子树的开销很大

                  我更喜欢最后两个中的一个,具体取决于数据更改的频率.

                  I'd prefer one of the last two, depending on how often the data changes.

                  另见:http://media.pragprog.com/titles/bksqla/树木.pdf

                  这篇关于在关系数据库中存储树结构的已知方法有哪些?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  ibtmp1是非压缩的innodb临时表的独立表空间,通过innodb_temp_data_file_path参数指定文件的路径,文件名和大小,默认配置为ibtmp1:12M:autoextend,也就是说在文件系统磁盘足够的情况下,这个文件大小是可以无限增长的。 为了避免ibtmp1文件无止境的暴涨导致
                  What does SQL clause quot;GROUP BY 1quot; mean?(SQL 子句“GROUP BY 1是什么意思?意思是?)
                  MySQL groupwise MAX() returns unexpected results(MySQL groupwise MAX() 返回意外结果)
                  MySQL SELECT most frequent by group(MySQL SELECT 按组最频繁)
                  Why Mysql#39;s Group By and Oracle#39;s Group by behaviours are different(为什么 Mysql 的 Group By 和 Oracle 的 Group by 行为不同)
                  MySQL GROUP BY DateTime +/- 3 seconds(MySQL GROUP BY DateTime +/- 3 秒)
                      • <small id='y7vBp'></small><noframes id='y7vBp'>

                          <bdo id='y7vBp'></bdo><ul id='y7vBp'></ul>
                        • <tfoot id='y7vBp'></tfoot>
                        • <i id='y7vBp'><tr id='y7vBp'><dt id='y7vBp'><q id='y7vBp'><span id='y7vBp'><b id='y7vBp'><form id='y7vBp'><ins id='y7vBp'></ins><ul id='y7vBp'></ul><sub id='y7vBp'></sub></form><legend id='y7vBp'></legend><bdo id='y7vBp'><pre id='y7vBp'><center id='y7vBp'></center></pre></bdo></b><th id='y7vBp'></th></span></q></dt></tr></i><div id='y7vBp'><tfoot id='y7vBp'></tfoot><dl id='y7vBp'><fieldset id='y7vBp'></fieldset></dl></div>
                          <legend id='y7vBp'><style id='y7vBp'><dir id='y7vBp'><q id='y7vBp'></q></dir></style></legend>
                            <tbody id='y7vBp'></tbody>