neo4j在插入行

时间:2016-07-22 01:35:57

标签: neo4j graph-databases

每个批次的数据库插入似乎都会变慢。

这是插入代码:

public class Write {

public static void main(String[] args) {
    final Connect con = new Connect();

    final Random rand = new Random(1);
    long start = System.currentTimeMillis();

    long last = start;
    System.out.format("Idx\tLast\tAvg\n");
    for (int j = 0; j < 100; j++) {
        final Transaction tx = con.session.beginTransaction();
        try {

            for (int i = 0; i < 1000; i++) {
                final Man man = new Man(rand);
                con.session.save(man);
            }
            tx.commit();
        } catch (Exception e) {
            tx.rollback();
            throw new RuntimeException(e);
        }
        tx.close();
        long loop = System.currentTimeMillis();
        System.out.format("%d\t%d\t%d\n", j, (loop-last), ((loop - start) / (j + 1)));
        last = loop;
    }

    long stop = System.currentTimeMillis();
    System.out.format("Time to run:%d(sec)\n", (stop - start) / 1000);

}

}

连接到locatl文件:

public class Connect {

final public Session session;

public Connect() {
    final Configuration configuration = new Configuration();
    configuration.driverConfiguration()
            .setDriverClassName("org.neo4j.ogm.drivers.embedded.driver.EmbeddedDriver")
            .setURI("file:///home/fodon/neo");

    final SessionFactory sessionFactory = new SessionFactory(configuration, "fod.neo.domain");
    session = sessionFactory.openSession();
}

}

对象也很简单:

public class Man extends Entity {

String name;
Set<Car> cars;

public Man() {

}

public Man(final Random rand) {
    name = RandomStringUtils.randomAlphanumeric(8);
    cars = new HashSet<>();
    for (int i = 0; i < rand.nextInt(10)+1; i++) {
        cars.add(new Car(rand));
    }
}

@Override
public String toString() {
    final StringBuffer sb = new StringBuffer();
    sb.append(String.format("%d:%s\n", getId(), name));
    for (Car car : cars) {
        sb.append("\t");
        sb.append(car.toString());
    }
    return sb.toString();
}

}

Car对象存储拥有的汽车:

public class Car extends Entity {

String type;

public Car() {

}

public Car(Random rand) {
    type = RandomStringUtils.randomAlphanumeric(10);
}

@Override
public String toString() {
    return String.format("%d:%s\n", getId(), type);
}

}

两者都是实体:

public abstract class Entity {

@GraphId
protected Long id;

Entity() {

}

public long getId() {
    return id;
}    

}

每次插入时数据库似乎都会变慢。

插入时间(ms)

0 8166

1 3682

2 4139

3 5416

4 6153

5 6852

6 8462

7 10211

8 11764

9 12825

10 14176

11 15548

12 16212

13 19995

14 23751

15 25247

16 26173

17 27535

18 28170

19 29541

20 29715

...

30 51122

如果这样慢,它就无法扩展。 我怎样才能加快速度呢?

0 个答案:

没有答案