I respectively use SimpleStatement and BoundStatement to select data from Cassandra 100 times.
I found BoundStatement didn't improve too much.
However, this link says:
SimpleStatement: a simple implementation built directly from a character string. Typically used for queries that are executed only once or a few times.
BoundStatement: obtained by binding values to a prepared statement. Typically used for queries that are executed often, with different values.
In my case, why the efficiency of SimpleStatement and BoundStatement is nearly the same?
long start0 = System.currentTimeMillis();
long start;
DataEntity mc = null;
ResultSet results = null;
BoundStatement bs = null;
PK pk = null;
CassandraData dao = new CassandraData();
long end;
Session session = dao.getSession();
//SimpleStatement ss = new SimpleStatement("select * from test where E='"+pk.E+"' and D="+pk.D+" and M="+pk.M);
PreparedStatement prepared = session.prepare(
"select * from test where E=? and D=? and M=?");
for (int i = 0; i < 100; i++) {
start = System.currentTimeMillis();
pk = ValidData.getOnePk();
//results = session.execute(ss);
bs = prepared.bind(pk.E, pk.D, pk.M);
results = session.execute(bs);
end = System.currentTimeMillis();
logger.info("Show One:" + (end - start) / 1000.0 + "s elapsed.");
}
long end0 = System.currentTimeMillis();
logger.info("Show All:" + (end0 - start0) / 1000.0 + "s elapsed.");
Its because you are doing too less iterations. 100 isn't enough to see the difference.
But in the end they both do quite much the same job except that the simple statement:
So what makes the BoundStatement different is that you prepare it once, bind and execute it multiple times.
So once you prepare the BoundStatement, you are only sending the binding variables over the connection. And not the whole statement. So from there is the difference of efficiency should come.
What else you could do is enable tracing and check how much time actually statement spends and where.
And for testing I would recommend doing some warmup before actually starting to measure times. As there are lot of factors that can actually affect the results.
PD: I assume that ValidData.getOnePk() returns random data and you aren't hitting the Cassandra row cache.