Question

I imagine with Cassandra 2.0 + using CQL this will look something like

BATCH BEGIN;
UPDATE keyspace.Table SET column = "value" IF EXISTS
INSERT keyspace.Table (...) VALUES (...) IF NOT EXISTS
BATCH END;

basically two statements for each item to be inserted/updated; This basically should mimic the MERGE behaviour of T-SQL

Is this a right assumption for the task I'd like to perform?

Was it helpful?

Solution

INSERT and UPDATE in CQL are the same call under the covers, because on the storage layer, you only really have updates anyway. In CQL, INSERT and UPDATE are exposed as syntactic sugar to make CQL feel more like SQL. There are slight nuances, such as the UPDATE statement taking a WHERE clause and also being used to increment counter tables, but they don't really affect the underlying PUT semantics when CQL is translated to the storage layer.

IF NOT EXISTS used with an INSERT or IF used with an UPDATE in CQL tells Cassandra to execute a compare and swap operation on the backend. This means that the operation will only get executed if the pre-condition is met. This requires consensus, so in a distributed setting (e.g. more than 1 Cassandra node), this might incur a performance penalty.

So given this, I don't think that this mirrors the SQL MERGE behavior. The thing that it does have in common is that both variants require the server to acquire a row lock in order to perform the operation atomically.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top