简体   繁体   中英

faster way to create relationship between existing nodes?

I am working on an application in which I have only "11263" number of nodes in my neo4j database. I am using following cypher query to form the relationships between the nodes:

let CreateRelations(fromToList : FromToCount list)=
    client.Cypher
    .Unwind(fromToList, "fromToList")
    .Match("(source)", "(target)")
    .Where("source.Id= fromToList.SId and target.Id= fromToList.FId ")
    .Merge("(source)-[relation:Fights_With]->(target)")
    .OnCreate()
    .Set("relation.Count= fromToList.Count,relation.Date= fromToList.Date")
    .OnMatch()
    .Set("relation.Count= (relation.Count+ fromToList.Count )")
    .Set("relation.Date= fromToList.Date")
    .ExecuteWithoutResults()

It is taking almost 47 to 50 sec to form say 1000 relations in a neo4j database. I am new to the neo4j DB, Is there is any other efficient way to do it?

The big thing slowing you down is that you're not using an index to lookup your starting nodes. Your match to source is performing a scan of all nodes in your db to find possible matches, per row in your unwound list. Then it does the same thing with target .

You need to add labels on your nodes, and if they already have labels, use the labels in your query. You'll need either an index or unique constraint on the label and id property so the index will be used for lookup.

Best way to go about tuning your queries is to try them out in the browser, and use EXPLAIN to ensure you're using index lookups, and if it's still slow, use PROFILE on the query (it will execute the query) to see the rows generated and db hits as the query executes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM