【问题标题】:Distinct count in CassandraCassandra 中的不同计数
【发布时间】:2017-08-28 06:39:34
【问题描述】:

我在“cassandra”中有一个表 chat_matches。并希望通过行数(按 to_id 分组)获取从_id 到不同 to_id 的所有消息的计数。

CREATE TABLE chat_matches(
    chat_id uuid,
    from_id bigint,
    to_id bigint,
    message  text,
    message_send_time timestamp,
    PRIMARY KEY ((chat_id,from_id),message_send_time)
);

【问题讨论】:

  • 你想要from_id,to_id,count(*) group by from_id,to_id 查询?
  • 是的,想亲自获得总聊天数。

标签: cassandra count distinct


【解决方案1】:

在 cassandra 中count(*) 是一个非常昂贵的操作,需要扫描所有节点的所有行只是为了给你计数并且会产生超时异常。

所以不要使用count(*),而是维护一个如下所示的计数器表:

CREATE TABLE message_counter (
    from_id bigint,
    to_id bigint,
    count counter,
    primary key((from_id, to_id ))
);

当出现新消息时,只需将 count 的值加一。

 UPDATE message_counter SET count = count + 1 WHERE from_id = 1 AND to_id = 2;

现在您可以非常有效地通过 from_id 到 to_id 选择消息计数组

SELECT * FROM message_counter WHERE from_id = 1 AND to_id = 2;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-05-07
    • 2020-01-28
    • 2014-09-27
    • 2018-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多