2017-08-03 83 views
0

我想用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 
+0

請包括您正在使用的代碼,以便其他人可以幫助找出問題。 – manassehkatz

回答

0

嘗試做了這些改變,以確保你是真正在一個事務中運行,而不是不必要地延遲提交更新:

  • 更改tx.runtx.append。這將使每個Cypher語句排隊,而不是立即執行它。
  • 刪除tx.process(),因爲您沒有後續的Cypher語句排隊。

理想情況下,您還應該傳遞參數,而不是每次通過循環修改您的Cypher語句。但是,這不會對您所遇到的緩慢負責。

+0

我試過tx.append(),對運行時沒有任何影響。除去tx.process也沒有任何作用,我只是在那裏證明,無論process()做什麼都是如此之久。 – SharadV

+0

數據庫中有多少個節點?您的查詢正在完成256個完整的節點掃描。 – cybersam

+0

執行前數據庫爲空。我試圖自動化數據庫構建以用於測試目的。我想我會採用寫入CSV文件並從那裏導入,因爲它只是用於初始構建。無論如何,應該快得多 – SharadV