简体   繁体   中英

Improve Neo4j Cypher query performance

USING PERIODIC COMMIT LOAD CSV WITH HEADERS FROM "file:///sample.csv" 
AS row WITH row 
MATCH (server:Server {  name:row.`SOURCE_HOST`, source:'sample'   })
MATCH (server__1:Server__1 {  name:row.`TARGET_HOST`, source:'sample'   })
MERGE (server)-[:DEPENDS_ON]->(server__1)

this query is taking very long time to create the relationships. there are 1875 server and server__1 nodes each Thanks in advance

The best approach to defining graph schemas is to have a unique id for each entity in your graph. This means that you could look up your nodes by their unique id. That way you can define a unique constraint for that property, which will speed up the query execution. For example, if the name property of the server were enough to look up the Server node, you could first define a unique constraint:

CREATE CONSTRAINT constraint_name
ON (s:Server) ASSERT s.name IS UNIQUE;

Then when you will be searching for the server, the queries will perform much better.

MATCH (server:Server {name:row.`SOURCE_HOST`})

This is the best approach as far as I have seen. If you don't have unique property, you could create one by combining two properties, in your case name + source . If that is not an option for you, you can create an index on both name and source properties, and if you use the Enterprise version you can also create a composite index on both properties to optimize performance.

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