簡體   English   中英

如何從python獲取密碼查詢的執行時間?

[英]How to get the execution time of a cypher query from python?

我試圖比較從 python 獲取 Cypher 查詢的執行時間,即在 neo4j-server 上計算所需的時間(不包括輸出結果所需的時間)。 現在我正在使用以下代碼:

from neo4j.v1 import 
driver = GraphDatabase.driver('bolt://localhost:7687', auth=('neo4j', '1234'))

n_repeats = 3
cypher = "MATCH (a) -[:{}*]- (b) WHERE ID(a) < ID(b) RETURN DISTINCT a, b".format(graphname + '_edges')

with driver.session() as session:
    total_time = 0
    for _ in range(n_repeats):
        with session.begin_transaction() as tx:
            start = time.time()
            tx.run(cypher)
            total_time += time.time() - start

avg_time = total_time*1000 / n_repeats
print('Average execution time:', avg_time, 'ms')

有沒有更好的方法來計算密碼查詢的執行時間? 例如,在 postgresql 中有 EXPLAIN ANALYZE 語句,它也提供了執行 SQL 查詢所需的時間。 在 Cypher 中有 EXPLAIN 和 PROFILE 語句,但兩者似乎都沒有返回特定時間。

我現在正在使用 neo4j-driver 連接到 neo4j,但我願意切換到另一個庫。

事實上,所用時間在所有結果中都可用,無需分析。 它們位於結果的摘要中,執行時間分為任何結果流可用之前的時間和服務器消耗整個結果流之前的時間。

它們可以相加得到查詢的總執行時間,以毫秒為單位:

result = tx.run(query, params)
avail = result.summary().result_available_after
cons = result.summary().result_consumed_after
total_time = avail + cons

Neo4j Python 驅動程序 1.7

from neo4j.v1 import 
driver = GraphDatabase.driver("bolt://localhost:7687", auth=("neo4j", "password"))

n_repeats = 3
cypher = "MATCH (a) -[:{}*]- (b) WHERE ID(a) < ID(b) RETURN DISTINCT a, b".format(graphname + '_edges')

with driver.session() as session:
    total_time = 0
    for _ in range(n_repeats):
        with session.begin_transaction() as tx:
            start = time.time()
            result = tx.run(cypher)
            records = list(result)  # consume the records
            tx.commit()
            total_time += time.time() - start

avg_time = total_time*1000 / n_repeats
print('Average execution time:', avg_time, 'ms')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM