Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python中预计算lft/rgt_Python_Mptt - Fatal编程技术网

在python中预计算lft/rgt

在python中预计算lft/rgt,python,mptt,Python,Mptt,我们有两个MySQL表,其中包含lft/rgt(嵌套的集合/修改的前序树遍历)、parent\u id和level列。每天晚上,我们都会清理表,从客户那里获取新数据。由于我们从客户数据库获取数据的方式,我们可以轻松计算父id和级别,但是lgt和rgt是在与存储过程同步后设置的。到目前为止,我们只需要处理相当小的结果集,不超过30000行。但现在我们看到的是超过200000个,而且这要花很长时间。我已经让同步运行了一个多小时,但仍然没有完成,通常需要5-15分钟(我也觉得有点多) 在存储到数据库之

我们有两个MySQL表,其中包含
lft
/
rgt
(嵌套的集合/修改的前序树遍历)、
parent\u id
level
列。每天晚上,我们都会清理表,从客户那里获取新数据。由于我们从客户数据库获取数据的方式,我们可以轻松计算
父id
级别
,但是
lgt
rgt
是在与存储过程同步后设置的。到目前为止,我们只需要处理相当小的结果集,不超过30000行。但现在我们看到的是超过200000个,而且这要花很长时间。我已经让同步运行了一个多小时,但仍然没有完成,通常需要5-15分钟(我也觉得有点多)

在存储到数据库之前,是否有其他方法计算
lft
/
rgt
?(最好使用python)

我们的同步外观的一些伪代码:

class Node(object):
    def __init__(self, id, data=None):
        self.id = id
        self.children = []
        self.data = data
        self.parent = None

    def add_child(self, child):
        self.children.append(child)
        child.parent = self

def sync(source_sql_result):
    node_map = {}
    current_id = 0
    parent_node = None

    for source_row in source_sql_result:
        for i, row in enumerate(get_subrows(source_row), 1):
            try:
                parent_node = node_map[row['identifier']]
            except KeyError:
                # New node found
                current_id += 1
                row['level'] = i
                node = Node(id=current_id, data=row)
                if parent_node is not None:
                    parent_node.add_child(node)
                node_map[row['identifier']] = node
                parent_node = node

    for node in node_map.values():
        yield node.data