Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cassandra/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我可以在Cassandra 2.1中对集合进行多重查询吗?_Cassandra_Cql - Fatal编程技术网

我可以在Cassandra 2.1中对集合进行多重查询吗?

我可以在Cassandra 2.1中对集合进行多重查询吗?,cassandra,cql,Cassandra,Cql,在Cassandra2.1中,我们可以通过在列上创建二级索引来查询集合 cqlsh:play> select * from songs where tags contains 't1'; id | tags | title --------------------------------------+--------------+------- e99f8f30-d212-11e4-bc9e-5d

在Cassandra2.1中,我们可以通过在列上创建二级索引来查询集合

cqlsh:play> select * from songs where tags contains 't1';

 id                                   | tags         | title

--------------------------------------+--------------+-------

 e99f8f30-d212-11e4-bc9e-5d1b1922b94d | {'t1', 't2'} | Song1
但我想查询多个值-如下所示:

select * from songs where tags contains 't1|t2';

这可能吗?

您不能:Cassandra中不支持
运算符

干杯, 卡洛

这可能吗

有点,是的。你应该这样做吗?不,不是真的。让我解释一下

虽然Carlo是正确的,因为CQL不支持
,但这可以与
一起使用。也就是说,如果要查询两个标记是否存在,可以执行以下操作:

aploetz@cqlsh:stackoverflow> SELECT * FROM songs
  WHERE tags CONTAINS 't2' AND tags CONTAINS 't1' ALLOW FILTERING;

 id                                   | tags         | title
--------------------------------------+--------------+-------
 75e46eb2-292a-42d0-8330-510fb35c635b | {'t1', 't2'} | Song1

(1 rows)
虽然这在技术上可行,但这是一个糟糕的想法

  • 多键查询已被确定为一种反模式。使用同步、异步查询通常比使用
    IN
    CONTAINS
    为多个键返回行要快。DataStax在
    SELECT
    文档中有一个标题为“您应该通读”的部分
  • 二级索引的性能不好,集合上的二级索引的性能甚至比单值索引的性能更差,这是合乎逻辑的。事实上,文档中有一个完整的部分,您应该在使用它们之前仔细阅读
  • 要使
    操作员对集合进行两次操作,需要
    允许筛选
    ALLOW FILTERING
    实质上是返回您拥有的每一行(来自每个节点),然后过滤结果。如果您有一个大型数据集和/或多个节点,则应不要使用需要
    允许过滤
    才能完成的查询
正确的方法是构建一个额外的查询表,其中
标记
作为分区键(并且
id
作为唯一性的集群键)

这将允许您按特定标记查询歌曲,而无需二级索引。虽然这将允许您在
中使用
(本质上是一个
),但对每个键(标记)的多个异步查询仍会更快

CREATE TABLE songsByTag (
  tag text,
  title text,
  id uuid,
  PRIMARY KEY ((tag),id));