【问题标题】:Get rows from Cassandra table, which matches column1 = '123' or column2 = '123'从 Cassandra 表中获取与 column1 = '123' 或 column2 = '123' 匹配的行
【发布时间】:2020-09-15 00:02:39
【问题描述】:

我知道 Cassandra 在 where 子句中不支持 OR,但我需要以下查询的等效项:

select * from table where column1 = '123' or column2 = '123';

由于 Cassandra 不支持 OR,我可以使用如下的批处理查询并将结果组合到程序中吗?它是否有效?

[ select * from table where column1 = '123' , select * from table where column2 = '123']

如果没有,有没有办法实现上述简单逻辑。

就我而言,column1 和column2 都是主键。即复合分区键。

我可以将 column1 和 column2 组合为一个列表 [column1, column2] 并像这样查询:

select * from table where list contains '123';

并在列表上创建二级索引

【问题讨论】:

  • Cassandra 中的 BATCH 并没有按照您的想法进行操作。它原子地应用操作。原子地应用SELECTs 没有任何好处。

标签: cassandra


【解决方案1】:

正如您所说,您无法使用 or 进行查询,如果不使用二级索引或 allow filtering,您也无法执行这两个查询。

select * from table where column1 = '123' 
select * from table where column2 = '123'

根据这些查询,可能性是;

  • column1column2 是分区键
  • column1column2 是分区键
  • 其中一个是分区键,另一个是集群键。

你不能根据这些可能性来查询它们。如果要在没有allow filtering 的情况下查询它们,则需要创建二级索引。之后,您可以进行两个查询并在应用程序层上进行比较。二级索引不是灵丹妙药,有一些缺点,@​​987654322@ 和 here 中有很好的解释。

如果您需要这些类型的 or 查询,那么最好根据查询重新设计您的表,以避免此类二级索引或允许过滤。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-22
    • 2022-11-03
    • 2020-06-11
    • 2014-07-11
    • 2018-05-18
    • 1970-01-01
    相关资源
    最近更新 更多