【问题标题】:Cassandra - search by primary key using an arbitrary subset of the primary key columnsCassandra - 使用主键列的任意子集按主键搜索
【发布时间】:2013-05-15 21:49:47
【问题描述】:

是否可以在 Cassandra 中找到主键匹配所有主键字段的任意子集的记录?

示例:

使用下面描述的表格,可以在不指定idsize的情况下找到主键具有特定typename的记录?

CREATE TABLE playlists (
  id      uuid,
  type    text,
  name    text,
  size    int,

  artist text,

  PRIMARY KEY (id, type, name, size)
);

谢谢!

【问题讨论】:

    标签: nosql cassandra


    【解决方案1】:

    至少在 Cassandra 1.2 中是可能的,但默认情况下它是禁用的,

    如果你尝试这样做:

    SELECT * from playlist where type = 'sometype' and name = 'somename';
    

    您将收到此错误:

    Bad Request: Cannot execute this query as it might involve data filtering and thus may have unpredictable performance. If you want to execute this query despite the performance unpredictability, use ALLOW FILTERING
    

    然后你可以启用它运行这个:

    SELECT * from playlist where type = 'sometype' and name = 'somename' ALLOW FILTERING;
    

    在您的示例中,Cassandra 将允许您使用从左到右的主键的完整子集进行查询,例如:

    SELECT * from playlist where id = 'someid'; (ALLOWED)
    SELECT * from playlist where id = 'someid' and type = 'sometype'; (ALLOWED)
    SELECT * from playlist where id = 'someid' and type = 'sometype' and name = 'somename'; (ALLOWED)
    SELECT * from playlist where id = 'someid' and type = 'sometype' and name = 'somename' and size=somesize; (ALLOWED)
    

    希望对你有帮助

    【讨论】:

    • 啊,好吧,所以使用不按顺序排列的任意子集可能会导致性能不是 O(log n) 时间?
    • 示例中的 id 字段是分区键,它决定了行的存储位置。在 where 子句中使用此键的过滤器使 Cassandra 能够有效地将查询重定向到具有相应数据的节点。由于缺少此分区键,Cassandra 必须将您的查询发送到集群中的所有节点,这是低效的,因此默认情况下是禁用的。 'ALLOW FILTERING' 子句启用此类搜索,但您应该将此视为您可能想要更改架构的提示...
    猜你喜欢
    • 2018-10-07
    • 2017-11-07
    • 2020-10-19
    • 2021-11-01
    • 2020-01-13
    • 1970-01-01
    • 2017-05-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多