Skip to content

Instantly share code, notes, and snippets.

@vranac
Last active August 29, 2015 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vranac/52f453c8194065c2236a to your computer and use it in GitHub Desktop.
Save vranac/52f453c8194065c2236a to your computer and use it in GitHub Desktop.
Neo4j relationships
CREATE INDEX ON :Airport(IataCode);
CREATE INDEX ON :Deal(OriginId);
CREATE INDEX ON :Deal(DestinationId);
MATCH (ao:Airport),(d:Deal)
WHERE d.OriginId = ao.IataCode
MERGE (ao)-[ro:ORIGIN]->(d);
MATCH (ad:Airport),(d:Deal)
WHERE d.DestinationId = ad.IataCode
MERGE (d)-[rd:DESTINATION]->(ad);
@vranac
Copy link
Author

vranac commented Dec 6, 2014

OK, so I have two node labels, Airport and Deal.
Indexes created for them.
I need to create two type of relationships:

  • connecting origin airport to deal (lines 5-7)
  • connecting destination airport to deal (lines 9-11)

I triggered the creation of origin relationships on 5m deal nodes and 3.5k airports, 3.5hours later still working with some ~220k relationships created.

Can this be optimised, and is there something I am missing?

@jexp
Copy link

jexp commented Dec 6, 2014

This should work better than the cross product:

MATCH (ao:Airport)
MATCH (d:Deal)
WHERE d.OriginId = ao.IataCode
MERGE (ao)-[ro:ORIGIN]->(d);

same here:

MATCH (ad:Airport)
MATCH (d:Deal)
WHERE d.DestinationId = ad.IataCode
MERGE (d)-[rd:DESTINATION]->(ad);

@jexp
Copy link

jexp commented Dec 6, 2014

You should be able to profile both statements in neo4j-shell

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment