使用Cypher加速py2neo

时间:2013-09-25 20:41:51

标签: python sqlite neo4j py2neo

我正在将一个SQLite3数据库中的图形填充到neo4j中,在Ubuntu linux上使用py2neo和Python 3.2。尽管速度并不是最重要的考虑因素,但在总共500万行中,图表在大约3小时内只获得了40K行(每个sql行一个关系)。

这是主循环:

from py2neo import neo4j as neo
import sqlite3 as sql

#select all 5M rows from sql-database
sql_str =  """select * from bigram_with_number"""  

#loop through each row
for (freq, first, firstfreq, second, secondfreq) in sql_cursor.execute(sql_str):

    # create the Cypher query string using cypher 2.0 with merge
    # so that nodes are created only if needed

    query = neo.CypherQuery(neo4j_db,"""
        CYPHER 2.0 
            merge (n:word {form: {firstvar}, freq: {freqfirst}})
            merge(m:word {form: {secondvar}, freq: {freqsecond}}) 
            create unique (n)-[:bigram {freq: {freqbigram}}]->(m) return n, m""")
    #execute the string with parameters from sql-query
    result = query.execute(freqbigram = freq, firstvar = first, freqfirst=firstfreq, secondvar=second, freqsecond=secondfreq)

尽管数据库填充得很好,但它需要数周才能完成。 我怀疑有可能更快地做到这一点。

1 个答案:

答案 0 :(得分:2)

对于批量加载,您最好绕过REST接口并使用较低级别的东西,例如Michael Hunger的加载工具:https://github.com/jexp/neo4j-shell-tools。即使在最佳性能下,REST接口也不太可能达到您所需的速度。

顺便说一句,请注意我没有正式支持Python 3.2,尽管我支持3.3。

相关问题