gurmitsa_187384 avatar image
gurmitsa_187384 asked alexandre.dutra answered

Does spring-data-cassandra optimize INSERT queries?

We have recently observed some issue that we trying to the find the root cause for. The issue was we are seeing some performance impact where in write and read to cassandra clusters are slow. Looking at the logs, we noticed a tombstones and a lot of prepared statements discard warnings. Tombstones is something we have under control. We are a little unclear about why our application is prepared so many prepared statements, looking in the system.prepared_statements, we noticed a lot of entries as below. insert into table1(col1, col2, col3, col4, col5) values(?,?,?,100,125); insert into table1(col1, col2, col3, col4, col5) values(?,?,?,25,500);

Within our code, we are using spring-data-cassandra with CQLOperations insert to save the object. In its simple form, the object looks like this.

 public class table_1{
 @PrimaryKeyColumn(type = PARTITIONED)
 private string col1;
 @PrimaryKeyColumn(type = PARTTIONED, ordinal = 1)
 private String col2="";
 @PrimaryKeyColumn(type = CLUSTERED)
 private String col3 = "";
 private long col4;
 private double col5;

I am trying to understand if the way we defined the above class drives how the insert CQLOperations insert is optimized?

We do not see this issue when we use datastax api with mapper to save the object.

Thank you!

10 |1000

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

alexandre.dutra avatar image
alexandre.dutra answered

Which methods in CqlOperations are you using? Beware that all the execute* methods indeed prepare statements before executing them, contrary to the query* methods. If you don't need prepared statements, then make sure you use query* methods exclusively.

Also, if you still want to make usage of prepared statements, take care with PreparedStatementCreator beans: there are two implementations available, CachedPreparedStatementCreator and SimplePreparedStatementCreator. You should always use CachedPreparedStatementCreator in production, because SimplePreparedStatementCreator will keep repreparing each statement again and again, which could possibly explain why you are seeing so many statement preparations.

One last suggestion: when using Spring Data's mapping framework, you should rather use CassandraOperations, not CqlOperations. CassandraOperations is easier to use and less error-prone.

10 |1000

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

Erick Ramirez avatar image
Erick Ramirez answered

There isn't anything obvious to me but as I understand it, CqlOperations use prepared statements. I'm not knowledgeable enough to know how it could be affecting the performance of reads or writes.

We don't directly support Spring Data Cassandra but we do the underlying Java driver it uses. I'm going to reach out internally at DataStax to see if we can give you some ideas. Cheers!

10 |1000

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.