Deleting in cassandra cql table using IN operator

我的未来我决定 提交于 2019-12-11 02:03:36

问题


I have a cql table with the following schema

  CREATE TABLE temp_date_time_wise (
  date text,
  timeuid timeuuid,
  temperature text
  PRIMARY KEY (date, timeuid)
)

I understood the feasibility of using IN operator by following the below link http://mechanics.flite.com/blog/2014/01/08/the-in-operator-in-cassandra-cql/ Now when i try perform a delete operation by using IN operator for the clustering key as the following

  delete from temp_date_time_wise where date in ('2014-11-17 00:00:00.0') and timeuid in (964966d0-6e1c-11e4-8362-5c260a5fc28f);

I get the following error

Bad Request: Invalid operator IN for PRIMARY KEY part timeuid

Can someone tell me the reason behind the error in executing the query ?


回答1:


That's because the IN operator only works on the last part of the primary key, or the last part of the clustering key. I thought before that "tuple notation" would work, but according to this doc, it only works on clustering columns. That being said, using IN combined with = should work on either key column:

aploetz@cqlsh:stackoverflow> SELECT * FROM temp_date_time_wise 
WHERE date = '2014-11-17 00:00:00.0' 
AND timeuid IN (964966d0-6e1c-11e4-8362-5c260a5fc28f);

 date                  | timeuid                              | temperature
-----------------------+--------------------------------------+-------------
 2014-11-17 00:00:00.0 | 964966d0-6e1c-11e4-8362-5c260a5fc28f |         34C

(1 rows)

or

aploetz@cqlsh:stackoverflow> SELECT * FROM temp_date_time_wise
WHERE date IN ('2014-11-17 00:00:00.0') 
AND  timeuid = 964966d0-6e1c-11e4-8362-5c260a5fc28f;

 date                  | timeuid                              | temperature
-----------------------+--------------------------------------+-------------
 2014-11-17 00:00:00.0 | 964966d0-6e1c-11e4-8362-5c260a5fc28f |         34C

(1 rows)

I should warn you though, that even if it does work, that statement is probably not going to perform well. You should read through this DataStax document, particularly the part titled When Not To Use IN:

The recommendations about when not to use an index apply to using IN in the WHERE clause. Under most conditions, using IN in the WHERE clause is not recommended. Using IN can degrade performance because usually many nodes must be queried.

Also, consider using different column names. Reserved words like timeuuid and non specific words like "date" can potentially lead to problems and confusion (especially when "date" really isn't one of the date types).



来源:https://stackoverflow.com/questions/27102579/deleting-in-cassandra-cql-table-using-in-operator

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!