tupjohny
tupjohny

Reputation: 1

Why is a simple Cypher query so slow?

Using Neo4j 2.3.0 Community Edition with Oracle JDK 8 and Windows 7

I am new to Neo4j and just trying how it works with Java. In the Neo4j Browser I created 3 nodes with the following statement:

CREATE (c:Customer {name:'King'})-[:CREATES]->(:Order {status:'created'}),
       (c)-[:CREATES]->(:Order {status:'created'})

Executed from the Neo4j Browser, the following query returns in 200 ms:

MATCH  (c:Customer)-[:CREATES]->(o:Order)
WHERE  c.name = 'King'
RETURN o.status

Executing this in Eclipse takes about 2500 ms, sometimes up to 3000 ms:

String query = "MATCH  (c:Customer)-[:CREATES]->(o:Order) "
             + "WHERE  c.name = 'King' "
             + "RETURN o.status";
Result result = db.execute(query);

This is incredibly slow! What am I doing wrong? In addition, I ran the following snippet in Eclipse and it only took about 50 ms:

Node king = db.findNode(NodeType.Customer, "name", "King");
Iterable<Relationship> kingRels = king.getRelationships(RelType.CREATES);
for(Relationship rel : kingRels) {
    System.out.println(rel.getEndNode().getProperty("status"));
}

So there are actually two things I am suprised of:

  1. Running a Cypher query in the Neo4j Browser seems to be way slower than doing a comparable thing with the Neo4j Core Java API in Eclipse.
  2. Running a Cypher query "embedded" in Java code is incredibly slow compared to the Neo4j Browser solution as well as compared to the plain Java solution.

I am pretty sure that this cannot be true. So what am I doing wrong?

Upvotes: 0

Views: 139

Answers (1)

Michael Hunger
Michael Hunger

Reputation: 41706

How do you measure it? if you measure the full runtime, then your time includes, jvm startup, database startup and class-loading and loading of store-files from disk.

Remember in the browser all of that is already running, and warmed up etc.

If you really want to measure your query, run it a number of times to warm up and then measure only the query execution and result loading.

Also consider using indexes or constraints as indicated and parameters, e.g. for your customer.name.

Upvotes: 0

Related Questions