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