質問

I have tried the DataStax Java driver and it seems the fastest way to insert data is to compose a CQL string with all parameters inline.

This loop takes 2500ms or so on my test cluster:

PreparedStatement ps = session.prepare("INSERT INTO perf_test.wibble (id, info) VALUES (?, ?)")
for (int i = 0; i < 1000; i++) session.execute(ps.bind("" + i, "aa" + i));

The same loop with the parameters inline is about 1300ms. It gets worse if there are many parameters. I know I can use batching to insert all the rows at once but thats not the purpose of this test. I also tried using session.execute(cql, params) and it is faster but still doesn't match inline values.

Composing CQL strings is certainly convenient and simple but is there a much faster way?

役に立ちましたか?

解決

You can do two things to improve performance. First is to use the executeAsynch function in the driver instead of execute.

Second thing is to use a batch statement instead of a loop (I know you mentioned it's not the purpose of the test, but when it comes to inserts with a loop, batching is what you want).

PreparedStatement ps = session.prepare("INSERT INTO messages (user_id, msg_id, title, body) " +
                                       "VALUES (?, ?, ?, ?)");
BatchStatement batch = new BatchStatement();
batch.add(ps.bind(uid, mid1, title1, body1));
batch.add(ps.bind(uid, mid2, title2, body2));
batch.add(ps.bind(uid, mid3, title3, body3));
session.execute(batch);
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top