我想用py2neo構建一個完整的可變深度二叉樹。我一直試圖使用py2neo事務發送創建語句到服務器,但運行時可怕。用py2neo事務構建圖表的性能問題
構建一個深度爲8(255個節點)的樹大約需要16.7秒 - 絕大多數時間都是在事務提交時花費的(如果在提交之前執行Transaction.process(),則處理佔用大部分運行)。可能是什麼問題?密碼語句分別只是一個Match和一個節點+關係Create。
下面是其構建樹
def buildBinaryTree(self):
depth = 1
tx = self.graph.begin()
g = self.graph
leaves = []
leaves.append("Root")
tx.run('CREATE(n {ruleName: "Root"})')
timeSum = 0
while depth < self.scale:
newLeaves = []
for leaf in leaves:
leftName = leaf + 'L'
rightName = leaf + 'R'
newLeaves.append(leftName)
newLeaves.append(rightName)
start = timer()
statement = ('MATCH(n {ruleName: "' + leaf + '"}) '
'WITH n CREATE (n)-[:`LINKS TO`]'
'->({ruleName: "' + leftName + '"})')
tx.run(statement)
statement = ('MATCH(n {ruleName: "' + leaf + '"}) '
'WITH n CREATE (n)-[:`LINKS TO`]'
'->(m {ruleName: "' + rightName + '"})')
tx.run(statement)
end = timer()
timeSum += (end - start)
leaves = newLeaves
depth += 1
print("Depth = " + str(depth))
print(timeSum)
start = timer()
print("Processing...")
tx.process()
print (timer() - start)
print("Committing...")
tx.commit()
print("Committed")
print (timer() - start)
而且帶有刻度的輸出功能= 8
building tree...
Depth = 2
Depth = 3
Depth = 4
Depth = 5
Depth = 6
Depth = 7
Depth = 8
0.009257960999775605
Processing...
16.753949095999815
Committing...
Committed
17.28687257200022
請包括您正在使用的代碼,以便其他人可以幫助找出問題。 – manassehkatz