mptt

Deeply nested subqueries for traversing trees in MySQL

狂风中的少年 提交于 2019-12-05 15:58:58
I have a table in my database where I store a tree structure using the hybrid Nested Set (MPTT) model (the one which has lft and rght values) and the Adjacency List model (storing parent_id on each node). my_table (id, parent_id, lft, rght, alias) This question doesn't relate to any of the MPTT aspects of the tree but I thought I'd leave it in in case anyone had a good idea about how to leverage that. I want to convert a path of aliases to a specific node. For example: "users.admins.nickf" would find the node with alias "nickf" which is a child of one with alias "admins" which is a child of

Finding breadcrumbs for nested sets

走远了吗. 提交于 2019-12-04 13:16:46
问题 I'm using nested sets (aka modified preorder tree traversal) to store a list of groups, and I'm trying to find a quick way to generate breadcrumbs (as a string, not a table) for ALL of the groups at once. My data is also stored using the adjacency list model (there are triggers to keep the two in sync). So for example: ID Name ParentId Left Right 0 Node A 0 1 12 1 Node B 0 2 5 2 Node C 1 3 4 3 Node D 0 6 11 4 Node E 3 7 8 5 Node F 4 9 9 Which represents the tree: Node A Node B Node C Node D

efficient function to retrieve a queryset of ancestors of an mptt queryset

一曲冷凌霜 提交于 2019-12-03 11:27:59
Does anybody have an efficient algorithm to retrieve all ancestors of an mptt queryset? The best I could think of so far is something like this: def qs_ancestors(queryset): if isinstance(queryset, EmptyQuerySet): return queryset queryset_aggs = queryset.values_list('tree_id', 'level').annotate(max_lft=Max('lft'), min_rght=Min('rght')) new_queryset = queryset.none() for tree_id, level, max_lft, min_rght in queryset_aggs: ancestors = MyModel.objects.filter( tree_id=tree_id, level__lt=level, lft__lte=max_lft, rght__gte=min_rght, ) new_queryset = ancestors | new_queryset return new_queryset There

Finding breadcrumbs for nested sets

丶灬走出姿态 提交于 2019-12-03 09:16:23
I'm using nested sets (aka modified preorder tree traversal) to store a list of groups, and I'm trying to find a quick way to generate breadcrumbs (as a string, not a table) for ALL of the groups at once. My data is also stored using the adjacency list model (there are triggers to keep the two in sync). So for example: ID Name ParentId Left Right 0 Node A 0 1 12 1 Node B 0 2 5 2 Node C 1 3 4 3 Node D 0 6 11 4 Node E 3 7 8 5 Node F 4 9 9 Which represents the tree: Node A Node B Node C Node D Node E Node F I would like to be able to have a user-defined function that returns a table: ID

Django treebeard what are differences between AL, NS, MP

穿精又带淫゛_ 提交于 2019-12-02 14:54:55
I'm trying to make a model for categorizing some objects. I already tried using django-mptt to easily retrieve related categories, and now I'm searching different solutions to find the best one. I can't find out though what are main differences between Materialized Path, Adjacency List and Nested Set. Wikipedia didn't give me a short answer, all I know is mptt is probably Nested Set... Can anyone explain it to me in few words ? It's easier to explain with examples than with a few words. Consider the sample tree, storing names: William Jones Blake Adams Tyler Joseph Miller Flint Materialized

How to repair a corrupted MPTT tree (nested set) in the database using SQL?

主宰稳场 提交于 2019-11-30 10:41:32
问题 I have an MPTT tree of over 100,000 records stored in MySQL using lft , rght and parent_id columns. Now the left/right values became corrupted, while the parent ids are still intact. It would require tons of queries to repair it in the application layer. Is there a good way to put the burden on the database and have it recalculate the left/right values using only SQL? Just to clarify, I need to recalculate the numeric lft/rght values of a nested set, not the ids of neighboring records.

Managing hierarchies in SQL: MPTT/nested sets vs adjacency lists vs storing paths

旧城冷巷雨未停 提交于 2019-11-30 01:25:21
For a while now I've been wrestling with how best to handle hierarchies in SQL. Frustrated by the limitations of adjacency lists and the complexity of MPTT/nested sets, I began thinking about simply storing key paths instead, as a simple node_key/node_key/... string. I decided to compile the pros and cons of the three techniques: Number of calls required to create/delete/move a node: Adjacency = 1 MPTT = 3 Path = 1 (Replace old node path with new node path across all nodes that contain that path) Number of calls required to get a tree: Adjacency = [number of sub-levels] MPTT = 1 Path = 1

django-mptt get_descendants for a list of nodes

随声附和 提交于 2019-11-29 03:07:35
问题 I am trying to get all descendants(include_self=True) not for one Node, but for a list (a QuerySet) of Nodes. This should be one SQL query. Example (that actually is not working:) some_nodes = Node.objects.filter( ...some_condition... ) some_nodes.get_descendants(include_self=True) #hopefully I would like to have all possible Nodes starting from every node of "some_nodes" The only idea I have right now is to iterate through some_nodes and run get_descendants() for every node - but this is

Managing hierarchies in SQL: MPTT/nested sets vs adjacency lists vs storing paths

天大地大妈咪最大 提交于 2019-11-28 21:40:47
问题 For a while now I've been wrestling with how best to handle hierarchies in SQL. Frustrated by the limitations of adjacency lists and the complexity of MPTT/nested sets, I began thinking about simply storing key paths instead, as a simple node_key/node_key/... string. I decided to compile the pros and cons of the three techniques: Number of calls required to create/delete/move a node: Adjacency = 1 MPTT = 3 Path = 1 (Replace old node path with new node path across all nodes that contain that

How to repair a corrupted MPTT tree (nested set) in the database using SQL?

纵然是瞬间 提交于 2019-11-28 17:57:12
问题 I have an MPTT tree of over 100,000 records stored in MySQL using lft , rght and parent_id columns. Now the left/right values became corrupted, while the parent ids are still intact. It would require tons of queries to repair it in the application layer. Is there a good way to put the burden on the database and have it recalculate the left/right values using only SQL? Just to clarify, I need to recalculate the numeric lft/rght values of a nested set, not the ids of neighboring records.