卡桑德拉的独特计数 [英] Distinct count in Cassandra
问题描述
我在 cassandra中有一个chat_matches表。并希望获取所有消息from_id到具有行数(按to_id分组)的不同to_id的计数。
I have a table chat_matches in "cassandra". And want to get the count of all messages from_id to distinct to_id with count of rows(group by 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)
);
推荐答案
在卡桑德拉 count(* )
是一项非常昂贵的操作,需要扫描所有节点上的所有行,只是为了给您计数并可能产生超时异常。
In cassandra count(*)
is a very costly operation, need to scan all the row from all the node just to give you the count and can generate timeout exception.
因此,与其使用 count(*)
,而是维护一个如下所示的计数器表:
So Instead of using count(*)
maintain a counter table like the below one :
CREATE TABLE message_counter (
from_id bigint,
to_id bigint,
count counter,
primary key((from_id, to_id ))
);
出现新消息时,只需将count的值增加一即可。
When a new message appears just increment the value of count by one.
UPDATE message_counter SET count = count + 1 WHERE from_id = 1 AND to_id = 2;
现在您可以非常有效地按from_id到to_id选择邮件计数组
Now you can select the message count group by from_id to to_id very efficiently
SELECT * FROM message_counter WHERE from_id = 1 AND to_id = 2;
这篇关于卡桑德拉的独特计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!