yusuf
yusuf

Reputation: 3646

Why Neo4j is slow for Friend of Friend query with Cypher in embedded

Using neo4j 2.0.2 with Oracle JDK 7 and Mac OS X 10.8

I am testing Neo4j as embedded database to see how fast it can be for a simple friend of friends query.

This is how create DB:

GraphDatabaseService graphDb = new GraphDatabaseFactory()
                .newEmbeddedDatabaseBuilder("./data")
                .setConfig(GraphDatabaseSettings.node_auto_indexing, "true")
                .setConfig(GraphDatabaseSettings.relationship_keys_indexable, "true")
                .newGraphDatabase();

This is how I create index on 'username' for user nodes:

try (Transaction tx = graphDb.beginTx()) {
    Schema schema = graphDb.schema();
    schema.indexFor(DynamicLabel.label("User")).on("username").create();      
    tx.success();
}

Then I create a node for myself and create 1000 nodes as users and randomly I make just 1 as my friend. Each of them has 50 nodes as friends. So I get total of 50 friends of friend. (through KNOW relationship)

And this is how I query: (Edited after solution: you can see me:User in cypher query which solves the problem.)

StringBuilder sb = new StringBuilder();
long start = System.currentTimeMillis();
ExecutionResult result = engine.execute("MATCH (me:User { username: 'erdogany' })-[:KNOWS*2..2]-(friend_of_friend) " +
        "WHERE NOT (me)-[:KNOWS]-(friend_of_friend) " +
        "RETURN ID(friend_of_friend)");


for (Map.Entry<String, Object> column : result.javaIterator().next().entrySet()) {
    sb.append(column.getKey()).append(": ").append(column.getValue()).append("\n");
}

long total = System.currentTimeMillis() - start;

System.out.println(total);
System.out.println(sb.toString());

Regardless of how many times I run the query it takes around 1400ms

Result: (I run the query in a loop so it warms up, but doesn't seem to be)

1399
ID(friend_of_friend): 2552

1397
ID(friend_of_friend): 2552

1405
ID(friend_of_friend): 2552
...

This is damn slow. What I am missing here?

PS: you can find the full code here: https://github.com/erdogany/neo4j-embedded-simple/blob/master/src/main/java/org/yusuf/Application.java

Upvotes: 0

Views: 714

Answers (1)

Michael Hunger
Michael Hunger

Reputation: 41706

You forgot to use that label :User for your own node in the query. Make sure you used that label too when creating your nodes.

And you don't need the old autoindex.

Upvotes: 3

Related Questions