为什么Cassandra不允许通过IN限制查询聚类键?

时间:2016-09-08 08:36:20

标签: select cassandra cql nosql

有谁知道为什么我不能在INselect收集栏限制群集列的地方使用查询?

让我详细说明一下。假设我的数据模型类似于以下内容:

create table inventory (
                sku text,
                class text,
                unit text,
                node text,
                supply map<text, frozen<delta_and_time>>,
                supply_compacted int,
                primary key ((sku, class, unit), node));

当我尝试使用以下select语句时:

select sku, class, unit, node, supply_compacted where sku = '0' 
           and class = 'good' and unit = 'each' and node in ('1', '2', '3')
一切都很好。但是当我尝试select *时,我会遵循相同的限制 错误:

Cannot restrict clustering columns by IN relations when a collection is selected by the query

我试图找出为什么C *中有这样的限制,但我找不到任何东西。此外,我查看了代码,但没有信息为什么执行此类检查。

有谁知道这种限制的原因是什么?

2 个答案:

答案 0 :(得分:1)

这是一个限制,基于收集数据类型如何&#34;黑客攻击&#34;进入现有的存储引擎。通过将每个键作为唯一列名存储在与聚类列相同的区域中来实现Map集合。因此,让Cassandra难以有效地做一个&#34; IN&#34;特别适用于每个&#34;行&#34;。

的大型收藏品

但是我觉得你可以重新使用你的数据模型来解决这个限制,甚至不使用Collection类型(因为如果你不小心它们会有很多与之相关的问题)。它看起来像&#34; supply_compacted&#34;可能是供应图中总库存的累计?如果是这样,您可以执行以下操作:

create table inventory (
  sku text,
  class text,
  unit text,
  node text,
  supply_compacted int static, -- stored once, total amount of inventory across all nodes
  supply frozen<delta_and_time>,
  primary key ((sku, class, unit), node)
);

答案 1 :(得分:0)

我最初的想法是,约束是由于对内存和网络的影响来获取集合,因为可以使用in限制一次返回多个集合。但是,我发现如果只通过指定分区键来限制它,那么查询就会起作用。

我的测试数据(在Cassandra 3.7上):

cqlsh:test> create table mytable(X text, Y text, Z text, mylist list<int>, primary key (X,Y));
cqlsh:test> insert into mytable (X,Y,Z,mylist) values('x','y1','z1',[1,2,3]);
cqlsh:test> insert into mytable (X,Y,Z,mylist) values('x','y2','z2',[4,5,6]);
cqlsh:test> select x,y,z from mytable where x = 'x' and y in ('y1');

 x | y  | z
---+----+----
 x | y1 | z1

(1 rows)
cqlsh:test> select * from mytable where x = 'x' and y in ('y1');
InvalidRequest: Error from server: code=2200 [Invalid query] message="Cannot restrict clustering columns by IN relations when a collection is selected by the query"
cqlsh:test> select * from mytable where x = 'x';

 x | y  | mylist    | z
---+----+-----------+----
 x | y1 | [1, 2, 3] | z1
 x | y2 | [4, 5, 6] | z2

(2 rows)

底层sstable转储:

$ sstabledump mb-1-big-Data.db
[
  {
    "partition" : {
      "key" : [ "x" ],
      "position" : 0
    },
    "rows" : [
      {
        "type" : "row",
        "position" : 15,
        "clustering" : [ "y1" ],
        "liveness_info" : { "tstamp" : "2016-09-13T08:14:33.172799Z" },
        "cells" : [
          { "name" : "z", "value" : "z1" },
          { "name" : "mylist", "deletion_info" : { "marked_deleted" : "2016-09-13T08:14:33.172798Z", "local_delete_time" : "2016-09-13T08:14:33Z" } },
          { "name" : "mylist", "path" : [ "1a1a0760-798a-11e6-851a-e3954ecad15b" ], "value" : "1" },
          { "name" : "mylist", "path" : [ "1a1a0761-798a-11e6-851a-e3954ecad15b" ], "value" : "2" },
          { "name" : "mylist", "path" : [ "1a1a0762-798a-11e6-851a-e3954ecad15b" ], "value" : "3" }
        ]
      },
      {
        "type" : "row",
        "position" : 99,
        "clustering" : [ "y2" ],
        "liveness_info" : { "tstamp" : "2016-09-13T08:14:49.772718Z" },
        "cells" : [
          { "name" : "z", "value" : "z2" },
          { "name" : "mylist", "deletion_info" : { "marked_deleted" : "2016-09-13T08:14:49.772717Z", "local_delete_time" : "2016-09-13T08:14:49Z" } },
          { "name" : "mylist", "path" : [ "23fefce0-798a-11e6-851a-e3954ecad15b" ], "value" : "4" },
          { "name" : "mylist", "path" : [ "23fefce1-798a-11e6-851a-e3954ecad15b" ], "value" : "5" },
          { "name" : "mylist", "path" : [ "23fefce2-798a-11e6-851a-e3954ecad15b" ], "value" : "6" }
        ]
      }
    ]
  }
]

正如您所看到的,该集合与非主键列没有什么不同,除了在将其返回给客户端之前所需的聚合。我想知道这是否是对旧节俭实施的约束,并且即使没有明显的理由说明为什么这样做也无法完成。