11

I'm on my path to learning Cassandra, and the differences in CQL and SQL, but I'm noticing the absence of a way to check to see if a record exists with Cassandra. Currently, the best way that I have is to use

SELECT primary_keys FROM TABLE WHERE primary_keys = blah, 

and checking to see if the results set is empty. Is there a better way to do this, or do I have the right idea for now?

Jim Pedid
  • 2,730
  • 3
  • 23
  • 27

3 Answers3

21

Using count will make it traverse all the matching rows just to be able to count them. But you only need to check one, so just limit and return whatever. Then interpret the presence of the result as true and absence - as false. E.g.,

SELECT primary_keys FROM TABLE WHERE primary_keys = blah LIMIT 1
Nikita Volkov
  • 42,792
  • 11
  • 94
  • 169
9

That's the usual way in Cassandra to check if a row exists. You might not want to return all the primary keys if all you care about is if the row exists or not, so you could do this:

SELECT count(*) FROM TABLE WHERE primary_keys = blah, 

This would just return a 1 if the row exists, and a 0 if it doesn't exist.

Jim Meyer
  • 9,275
  • 1
  • 24
  • 49
1

If you are using primary key to filter rows, all the above 3 solutions (including yours) are fine. And I don't think there are real differences.

But if you are using a general way (such as indexed column, partition key) to filter rows, you should take the solution of "Limit 1", which will avoid useless network traffic.

There is a related example at: The best way to check existence of filtered rows in Cassandra? by user-defined aggregate?

Community
  • 1
  • 1
Ying LEE
  • 93
  • 5