博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
树形菜单的存储设计
阅读量:5916 次
发布时间:2019-06-19

本文共 2412 字,大约阅读时间需要 8 分钟。

  hot3.png

##关系型数据库设计 ###parent_id |id |name | parent_id| |---|-------|-----------| |1 |A |NULL | |2 |B |1 | |3 |C |1 | |4 |D |2 |

优缺点:

  • Pros: Easy to understand, fast to insert and move
  • Cons: Requires multiple queries to get whole subtrees

针对查询问题,可以应用缓存来解决

###left&right

id name parent_id left right
1 A NULL 1 8
2 B 1 2 5
3 C 1 6 7
4 D 2 3 4
  • Pros: Lookup up entire subtrees with a single query (fast), intrinsic ordering of children

  • Cons: Slow to insert and move, due to many modifications of existing records

针对修改问题,由于菜单读多写少,可以接受

###记录path

|id |name |parent_id | path | |:--:|:--:|:--:|---| |1 |A |NULL| 1- | |2 |B |1| 1-2- | |3 |C |1| 1-2- | |4 |D |2| 1-2-4- |

查询快 修改麻烦

##文档型数据库 ###直接存json

{  "name": "A",  "children": [    {"name": "B", "children": [{"name": "D"}]},    {"name": "C"}  ]}
  • Pros: Native tree-like data structure, intrinsic ordering of children
  • Cons: Could get ugly with larger and more complex documents, concurrency is limited

增加version,进行版本/乐观锁并发控制

###使用parent_id

{"_id": "A"}{"_id": "B", "parent_id": "A"}{"_id": "C", "parent_id": "A"}{"_id": "D", "parent_id": "B"}
  • Pros: Simple to understand, easy to find parent
  • Cons: Needs good view or index to find child documents, no intrinsic ordering of children

###使用children

{"_id": "A", "children": ["B", "C"]}{"_id": "B", "children": ["D"]}{"_id": "C"}{"_id": "D"}

Pros: Simple to understand, easy to find children, intrinsic ordering of children Cons: Needs good view or index to find parent document

###索引法

{  "leaf": "A",  "children": [    {"leaf": "B", "children": [{"leaf": "D"}] },    {"leaf": "C"}  ]}{"_id": "A", ...}{"_id": "B", ...}{"_id": "C", ...}{"_id": "D", ...}
  • Pros: Two lookups to find any node, native tree data structure, data separate from tree, intrinsic ordering

  • Cons: Traversing from one node to another requires referring back to the tree data structure (maybe this is not a bad thing — it can be cached), concurrency is limited

##存储层级以及路径 Storing the node path along with hierarchy level int the document.

{ele: "a", path: "/a" , lvl:1} {ele: "b", path: "/a/b", lvl:2} {ele: "c", path: "/a/b/c", lvl:3}
  • Pros : Easy to fetch a subtree of a given node. Traversing up a tree is not that difficult. Getting all parent nodes of c:db..find({"path" : {"$in" : ["/a", "/a/b"] } }).
  • Cons : If hierarchical changes are frequent, then path update is needed but still easier than other models.

##doc

转载于:https://my.oschina.net/go4it/blog/790884

你可能感兴趣的文章
Swift 懒加载
查看>>
对象存储云cos + tinify + python实现压缩图片之后上传并返回外链。
查看>>
软件工程概论第二章
查看>>
自动化之UI(autoit)
查看>>
团队绩效评估计划
查看>>
测试-关于Unity获取子层级内容的几种接口(Transform FindChild, Component GetComponentInChildren,...)...
查看>>
ECMAScript5的其它新特性
查看>>
django-自动验证账号-连接转圈-头像上传-回显-写入表格
查看>>
vb中的dsr文件如何创建?
查看>>
nginx简易教程
查看>>
设计模式之观察者模式
查看>>
计算机视觉专业名词中英文对照(转)
查看>>
awk支持多个记录分隔符的写法
查看>>
SaltStack技术入门与实践
查看>>
【Linux】网卡配置与绑定
查看>>
winform datagridview 绑定泛型集合变得不支持排序的解决方案
查看>>
sp_addextendedproperty
查看>>
常用的正则表达式
查看>>
linux内存管理之活动内存区
查看>>
posix多线程有感--线程高级编程(均衡负载CPU绑定)
查看>>