Skip to content

Instantly share code, notes, and snippets.

@paulbarbu
Last active September 12, 2020 17:23
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 paulbarbu/fa17983998bafa639daf67851ff2b7e4 to your computer and use it in GitHub Desktop.
Save paulbarbu/fa17983998bafa639daf67851ff2b7e4 to your computer and use it in GitHub Desktop.
neo4j test with 250k nodes
# pip install neo4j-driver
from timeit import default_timer
from neo4j import GraphDatabase, basic_auth
# http://localhost:7474/browser/
# cypher-shell
# export the DB:
# systemctl stop neo4j
# neo4j-admin dump --database=neo4j --to=test.dump
driver = GraphDatabase.driver(
"neo4j://localhost:7687",
auth=basic_auth("neo4j", "admin"))
session = driver.session()
# cypher_query = '''
# MATCH (n)
# RETURN id(n) AS id
# LIMIT 10
# '''
# results = session.run(cypher_query,
# parameters={})
# for record in results:
# print(record['id'])
# MATCH (n) DETACH DELETE n
# CREATE (G1:Group {name: "G1"})
# CREATE (AND0000000001:Meter {name: "AND0000000001"}),
# (AND0000000002:Meter {name: "AND0000000002"}),
# UNWIND [{name: "AND2"}, {name: "AND3"}] as props
# MERGE (n:Meter)-[:BELONGS]->(g:Group {name: "G1"}) SET n = props
# MATCH (n:Meter), (g:Group {name: "G1"}) MERGE (n)-[:BELONGS]->(g)
# UNWIND [{name: "AND2"}, {name: "AND3"}] as props
# MATCH (g:Group {name: "G1"})
# MERGE (n:Meter)-[:BELONGS]->(g) SET n = props
# neo4j@neo4j> MATCH (m:Meter)-[:BELONGS]->(g:Group) WHERE g.name STARTS WITH "G" RETURN m.name, g.name ORDER BY m.name;
# 249975 rows available after 41 ms, consumed after another 13 ms
# MATCH (child:Group) WHERE child.id >= 1 AND child.id <= 5
# MERGE (g:Group {name: "A", id: -1})
# MERGE (g)<-[:BELONGS]-(child);
# get all meters (which are not direct children) in group A
# MATCH (g:Group)<-[:BELONGS*2..]-(m:Meter) WHERE g.id=-1 RETURN m
# Copy all meters from Group G1 in B
# MATCH (g:Group)<-[:BELONGS]-(m:Meter) WHERE g.id=1
# MERGE (ng:Group {name: "B", id: -2})
# MERGE (m)-[:BELONGS]->(ng)
# get all 6000 meters in the B group whether direct children or grand, grand... children
# MATCH (g:Group)<-[:BELONGS*]-(m:Meter) WHERE g.id=-2 RETURN m
# get direct children in group B
# MATCH (g:Group)<-[:BELONGS]-(m:Meter) WHERE g.id=-2 RETURN m
# Move all meters from Group G1 in B - effectively delete their relationship with group G1
# MATCH (g:Group)<-[r:BELONGS]-(m:Meter) WHERE g.id=1
# DELETE r
#MATCH (m:Meter)-[:BELONGS]->(g:Group) WHERE m.id<10000 RETURN m,g;
# Copy the meters that are now direct children of B back in G1
#MATCH (g:Group)<-[:BELONGS]-(m:Meter) WHERE g.id=-2
#MERGE (ng:Group {name: "G1", id: 1})
#MERGE (m)-[:BELONGS]->(ng)
def detach_and_delete():
session.run('MATCH (n) DETACH DELETE n')
def create_all():
for j in range(1, 250000//10000+1):
q = ''
for i in range((j-1)*10000+1, j*10000):
q += f'{{name: "AND{i:0=10}", id: {i} }},'
i = j*10000
q += f'{{name: "AND{i:0=10}", id: {i} }}'
print(f'Creating group G{j}')
start = default_timer()
res = session.run(f'CREATE (G{j}:Group {{name: "G{j}", id:{j} }})')
print(f'Time elapsed: {default_timer() - start}')
print(f'Creating meters {(j-1)*10000+1} - {j*10000}')
start = default_timer()
res = session.run(f'UNWIND [{q}] as props CREATE (n:Meter) SET n = props', parameters={})
print(f'Time elapsed: {default_timer() - start}')
print(f'Moving meters {(j-1)*10000+1} - {j*10000} in group G{j}')
start = default_timer()
res = session.run(f'''MATCH (n:Meter), (g:Group {{id: {j} }})
WHERE n.id >= {(j-1)*10000+1}
AND n.id <= {j*10000}
MERGE (n)-[:BELONGS]->(g)''')
print(f'Time elapsed: {default_timer() - start}')
def create_higher_levels():
for index, g in enumerate(['A', 'B', 'C', 'D', 'E']):
print(f'Moving groups G{index*5 + 1} - {(index+1)*5} in group {g}')
start = default_timer()
res = session.run(f'''MATCH (child:Group) WHERE child.id >= {index*5 + 1} AND child.id <= {(index+1)*5}
MERGE (g:Group {{name: "{g}", id: {-index - 1} }})
MERGE (g)<-[:BELONGS]-(child);''')
print(f'Time elapsed: {default_timer() - start}')
# MATCH (g:Group) WHERE g.id <0 RETURN g
if __name__ == '__main__':
detach_and_delete()
create_all()
create_higher_levels()
# MATCH (child:Group) WHERE child.id >= 1 AND child.id <= 5
# MERGE (g:Group {name: "A", id: -1})
# MERGE (g)<-[:BELONGS]-(child);
# MATCH (child:Group) WHERE child.name IN ["G1", "G2", "G3", "G4", "G5"] MERGE (g:Group {name: "C", id: -2}) MERGE (g)<-[:BELONGS]-(child);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment