Reputation: 331
I'm trying to insert a bunch of nodes Neo4J using the following code:
import org.neo4j.driver.v1.*;
public class BoltClass
{
public static void minimalWorkingExample() throws Exception
{
Driver driver = GraphDatabase.driver( "bolt://localhost", AuthTokens.basic( "neo4j", "admin4j" ) );
Session session = driver.session();
int k=0;
for (int i = 0; i < 1000; i++) {
int count = 1000;
long begin = System.currentTimeMillis();
for (int j = 0; j < count; j ++) {
session.run("CREATE (a:Person {id:" + k + ", name:'unknown'})");
}
long end = System.currentTimeMillis();
System.out.print("Inserting " + (double)count/((double)(end-begin)/count) + " nodes per second.\n");
k++;
}
session.close();
driver.close();
}
public static void main(String[] args)
{
try {
minimalWorkingExample();
} catch (Exception e) {
e.printStackTrace();
}
}
}
Results:
Inserting 58.8235294117647 nodes per second.
Inserting 76.92307692307692 nodes per second.
Inserting 50.0 nodes per second.
Inserting 76.92307692307692 nodes per second.
Inserting 55.55555555555556 nodes per second.
Inserting 62.5 nodes per second.
Inserting 66.66666666666667 nodes per second.
Inserting 55.55555555555556 nodes per second.
Inserting 62.5 nodes per second.
Inserting 55.55555555555556 nodes per second.
Inserting 47.61904761904762 nodes per second.
Inserting 45.45454545454545 nodes per second.
Inserting 58.8235294117647 nodes per second.
Inserting 83.33333333333333 nodes per second.
I'm using Neo4j 3.0.3 and org.neo4j.driver 1.0.4. The graph was blank before the insertions. The machine used has an i5 2-2.6GHz CPU and 8GB RAM.
LE: I've just discovered transactions:
public static void TransactionExample() throws Exception
{
Driver driver = GraphDatabase.driver( "bolt://localhost", AuthTokens.basic( "neo4j", "admin4j" ) );
Session session = driver.session();
int k=0;
for (int i = 0; i < 1000; i++) {
int count = 1000;
long begin = System.currentTimeMillis();
try ( Transaction tx = session.beginTransaction() )
{
for (int j = 0; j < count; j ++) {
tx.run("CREATE (a:Person {id:" + k + ", name:'unknown'})");
}
tx.success();
}
long end = System.currentTimeMillis();
System.out.print("Inserting " + (double)count/((double)(end-begin)/count) + " nodes per second.\n");
k++;
}
session.close();
driver.close();
}
Results:
Inserting 20000.0 nodes per second.
Inserting 17857.142857142855 nodes per second.
Inserting 18867.924528301886 nodes per second.
Inserting 15384.615384615385 nodes per second.
Inserting 19607.843137254902 nodes per second.
Inserting 16666.666666666668 nodes per second.
Inserting 16393.44262295082 nodes per second.
Nice performance boost. Can it be improved even more?
Upvotes: 4
Views: 1914
Reputation: 41676
You should also use parameters {id}
and {name}
parameters for your statement, otherwise Cypher has to re-parse and re-compile each of your queries which adds up. With parameters it can compile it once and re-use the compiled plan.
You should also increment k
in the inner loop.
public static void TransactionExample() throws Exception
{
Driver driver = GraphDatabase.driver("bolt://localhost", AuthTokens.basic("neo4j", "admin4j"));
Session session = driver.session();
int k=0;
String query = "CREATE (a:Person {id:{id}, name:{name}})";
for (int i = 0; i < 1000; i++) {
int count = 1000;
long begin = System.currentTimeMillis();
try (Transaction tx = session.beginTransaction())
{
for (int j = 0; j < count; j++) {
tx.run(query, Values.parameters("id", k, "name", unknown));
k++;
}
tx.success();
}
long end = System.currentTimeMillis();
System.out.print("Inserting " + (double)count/((double)(end-begin)/count) + " nodes per second.\n");
}
session.close();
driver.close();
}
Upvotes: 3
Reputation: 30397
While adding an index (or unique constraint) beforehand on :Person(id) probably won't speed up insert (it might even slow insert a bit as it will have to update the index), it should significantly speed up any operations later which require you to match on :Person nodes by ID, such as when adding relationships from a :Person to other nodes or adding properties to a person.
Upvotes: 1