【问题标题】:Cassandra modeling with key and indexes使用键和索引进行 Cassandra 建模
【发布时间】:2014-12-29 00:04:23
【问题描述】:

我有一个“用户”表,每个用户都有很多“项目”,每个项目都有很多“客户”,所以它是多对多的,所以我在不同的表中跟踪客户事件。

问题是我不知道如何选择键和索引,以便查询具有最佳性能。

带键的表:

CREATE TABLE project_clients_events(
    id timeuuid,
    user_id int,
    project_id int,
    client_id text,
    event text,
    PRIMARY KEY ((user_id, project_id), id, client_id)
);

现在每个 (user_id, project_id) 将有超过 100K 的事件,所以我需要能够对结果进行分页:
http://www.datastax.com/documentation/cql/3.0/cql/cql_using/paging_c.html

如何对结果进行分组和分页?

谢谢!

【问题讨论】:

    标签: sql cassandra pagination many-to-many nosql


    【解决方案1】:

    让我分两部分回答你的问题。首先是分页,然后是分区键

    Cassandra CQL 驱动现在支持自动分页,因此您无需担心设计复杂的 where 子句。

    Statement stmt = new SimpleStatement("SELECT * FROM images");
    stmt.setFetchSize(100);
    ResultSet rs = session.execute(stmt);
    
    // Iterate over the ResultSet here
    

    此链接会有所帮助: http://www.datastax.com/dev/blog/client-side-improvements-in-cassandra-2-0

    决定分区取决于您可能有的查询。例如,如果您的大多数查询使用 user_id 和 project_id(即您的大多数查询仅根据 user_id 和 client_id 获取结果),那么最好将 then 作为分区键的一部分,因为所有这些结果都将放在相同的 Cassandra 列(在同一节点上)并一起获取。

    因此,我建议您首先确定查询并相应地选择您的分区键。因为您的性能将取决于查询内容与列在 Cassandra 中的存储方式

    这可以帮助您http://www.slideshare.net/DataStax/understanding-how-cql3-maps-to-cassandras-internal-data-structure(幻灯片 45-70)

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-30
    • 2015-01-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多