卡桑德拉的不同的计数
问题描述:
我有一张桌子在“卡桑德拉”chat_matches。并且想要将所有消息的from_id计数为不同的to_id与行数(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(*)
是一个非常昂贵的操作,需要从所有的节点扫描所有的行只是给你计数并能产生超时异常。
所以除了使用count(*)
维护计数表像下面的一个:
CREATE TABLE message_counter (
from_id bigint,
to_id bigint,
count counter,
primary key((from_id, to_id))
);
当有新的消息出现时只是一个递增计数的值。
UPDATE message_counter SET count = count + 1 WHERE from_id = 1 AND to_id = 2;
现在,您可以通过from_id选择邮件数组非常有效地
SELECT * FROM message_counter WHERE from_id = 1 AND to_id = 2;
你想from_id,to_id,COUNT(*)GROUP BY from_id,to_id查询to_id? – nevsv
是的,想要获得总聊天数。 – Shanu