【问题标题】:Cassandra using composite indexes and secondary togetherCassandra 使用复合索引和辅助索引一起使用
【发布时间】:2012-06-05 12:35:18
【问题描述】:

我们想使用 cassandra 来存储复杂的数据
但我们不知道如何组织索引。

我们的表(列族)如下所示:

Users =
  { 
    RandomId int,
    Firstname varchar,
    Lastname varchar,
    Age int,
    Country int,
    ChildCount int
  }

我们的查询包含必填字段(名字、姓氏、年龄)和额外的搜索选项(国家、ChildCount)。
我们应该如何组织索引以使这种查询更快?

首先我想,在 (Firstname, Lastname, Age) 上创建复合索引并在剩余字段 (Country 和 ChildCount) 上添加单独的二级索引是很自然的。
但是创建二级索引后我无法将行插入表中,也无法查询该表。

使用

  • cassandra 1.1.0
  • 带有--cql3 选项的cqlsh。

欢迎任何其他解决我们问题的建议(具有强制性和附加选项的复杂查询)。

【问题讨论】:

    标签: nosql cassandra cql


    【解决方案1】:

    这是我的想法。您可以简单地创建一个列族,将 RandomId 作为行键,将所有剩余字段简单地作为列(例如,列名“firstname”,列值“jonh”)。在此之后,您必须为这些列中的每一个创建一个二级索引。您的值的基数似乎很低,因此它应该稍微有效。

    CQL 代码应该是这样的:

    create column family users with comparator=UTF8Type and column_metadata=[{column_name:  firstname, validation_class: UTF8Type,index_type: KEYS},
    {column_name: lastname, validation_class: UTF8Type, index_type: KEYS},
    {column_name: contry, validation_class: IntegerType, index_type: KEYS},
    {column_name: age, validation_class: IntegerType, index_type: KEYS]},
    {column_name: ChildCount, validation_class: IntegerType, index_type: KEYS]];
    

    http://www.datastax.com/docs/0.7/data_model/secondary_indexes 是一个很好的参考

    如果我错了,请告诉我;

    【讨论】:

      【解决方案2】:

      对于涉及大量分区的查询,索引不是很有效。

      我认为最好根据您要进行的查询来考虑表格:您想要一个基于用户名的查询表格,这似乎是存储有关用户的所有信息的正确位置。另一方面,您希望能够根据国家/地区进行搜索,以提供用户列表:因为您实际上并不需要所有信息,可能只需要名字和姓氏,或者只需要电子邮件,等等。然后另一个表可以做到。

      这涉及到一些数据重复,但更符合 Cassandra 数据建模理念。

      这将给出:

      CREATE TABLE users(
         id UUID,
         lastname TEXT,
         firstname TEXT,
         age INT,
         country TEXT,
         childcount INT,
         PRIMARY KEY(UUID)
      );
      
      CREATE TABLE users_by_country(
         country TEXT,
         firstname TEXT,
         lastname TEXT,
         user_uuid UUID,
         PRIMARY KEY((country), firstname, lastname)
      );
      
      CREATE TABLE users_by_age(
         age INT,
         firstname TEXT,
         lastname TEXT,
         user_uuid UUID,
         PRIMARY KEY((age), firstname, lastname)
      );
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-10-19
        • 1970-01-01
        • 2014-01-11
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多