rPod
rPod

Reputation: 21

Need help to optimize neo4j cypher CREATE and MERGE queries

I am parsing bitcoin blockchain, the whole idea is to build a node graph that looks like this (address)-[redeemed]->(tx)-[sent]->(address) so I can see how bitcoin addresses are related to each other. The problem is the execution time, sometimes it takes a few minutes to import just one transaction. Besides, some of these queries are too long, like few thousands of lines, and won't execute at all. I have read a few articles on how to optimize match queries, but found almost nothing about create and merge. I saw a few guys here recommending to use UNWIND and send as much data as possible as parameters, to make queries shorter, but I have no idea how to implement this in my query.

Here is example of my query: http://pastebin.com/9S0kLNey

Upvotes: 2

Views: 188

Answers (1)

cybersam
cybersam

Reputation: 66999

You can try using the following simple query, passing the string parameters "hash", "time", "block", and "confs"; and a collection parameter named "data":

CREATE (tx:Tx {hash: {hash}, time: {time}, block: {block}, confirmations: {confs}})
FOREACH(x IN {data} |
  MERGE (addr:Address {address: x.a})
  CREATE (addr)-[:REDEEMED {value: x.v}]->(tx)
);

The values to use for the string parameters should be obvious.

Each "data" element would be a map containing an address ("a") and a value ("v"). For example, here is a snippet of the "data" collection that would correspond to the data in your sample query:

[
  {a: "18oBAMgFaeFcZ5bziaYpUpsNCJ7G8EgH8g", v: "240"},
  {a: "192W3HUVDyrp6ewvisHSijcx9f5ZoarrwX", v: "410"},
  {a: "18tnEFy4usZvpMZLnjBFPjbmLKEzqPz958", v: "16.88"},
  ...
]

This query should run faster than your original sample, but I don't know how much faster.

Upvotes: 1

Related Questions