是具有MAX()和GROUP BY()效率的选择,还是将读取所有行

时间:2019-04-13 19:32:50

标签: cassandra cql cassandra-3.0

我有一个创建的cassandra表,如下所示:

create table messages
    (user_id int, peer_id int, send_on timestamp, message text, 
    PRIMARY KEY (user_id, peer_id, send_on))
    WITH CLUSTERING ORDER BY (peer_id ASC, send_on DESC);

并填充数据。

我想查询给定用户的每个peer_id的最新消息,我想到的是:

select peer_id, max(send_on), message 
  from messages 
  where user_id = 1 group by peer_id;

我想知道这是否是要读取所有消息并仅提取最新消息,还是足够聪明以至于只能接收最新消息。

我问的原因是因为用以下值填充表:

1, 1, now(), hello 1
1, 1, now(), hello 2
1, 1, now(), hello 3
1, 2, now(), hello 4
1, 2, now(), hello 5
1, 2, now(), hello 6
...
1, 3, now(), hello 9

当我运行查询时,我看到了预期的结果:

select peer_id, max(send_on), message from messages where user_id = 1 group by peer_id;

 peer_id | system.max(send_on)             | message
---------+---------------------------------+---------
       1 | 2019-04-13 19:20:48.567000+0000 | hello 3
       2 | 2019-04-13 19:21:07.929000+0000 | hello 6
       3 | 2019-04-13 19:21:22.081000+0000 | hello 9

(3 rows)

但是,随着追踪的进行,我看到:

 activity                                                                                                                      | timestamp                  | source    | source_elapsed | client
-------------------------------------------------------------------------------------------------------------------------------+----------------------------+-----------+----------------+-----------
                                                                                                            Execute CQL3 query | 2019-04-13 19:24:54.948000 | 127.0.0.1 |              0 | 127.0.0.1
 Parsing select peer_id, max(send_on), message from messages where user_id = 1 group by peer_id; [Native-Transport-Requests-1] | 2019-04-13 19:24:54.956000 | 127.0.0.1 |           8812 | 127.0.0.1
                                                                             Preparing statement [Native-Transport-Requests-1] | 2019-04-13 19:24:54.957000 | 127.0.0.1 |          10234 | 127.0.0.1
                                                                    Executing single-partition query on messages [ReadStage-2] | 2019-04-13 19:24:54.962000 | 127.0.0.1 |          14757 | 127.0.0.1
                                                                                    Acquiring sstable references [ReadStage-2] | 2019-04-13 19:24:54.962000 | 127.0.0.1 |          14961 | 127.0.0.1
                                       Skipped 0/0 non-slice-intersecting sstables, included 0 due to tombstones [ReadStage-2] | 2019-04-13 19:24:54.962000 | 127.0.0.1 |          15211 | 127.0.0.1
                                                                       Merged data from memtables and 0 sstables [ReadStage-2] | 2019-04-13 19:24:54.963000 | 127.0.0.1 |          15665 | 127.0.0.1
                                                                          Read 9 live rows and 0 tombstone cells [ReadStage-2] | 2019-04-13 19:24:54.963000 | 127.0.0.1 |          15817 | 127.0.0.1
                                                                                                              Request complete | 2019-04-13 19:24:54.964448 | 127.0.0.1 |          16448 | 127.0.0.1

因此,它似乎读取了所有9行。有没有一种方法可以对此进行优化?也许更改我的架构?

2 个答案:

答案 0 :(得分:0)

我可以想到的两个选择是让您创建另一个表,该表充当每个userID和peerID的最大记录的索引。这两个字段将组成您的分区键,然后包含您需要在消息表中找到该用户ID和PeerID的最大记录所需的其余数据。每当您将数据放入数据时,数据都会被升序处理,因此,您始终只会将最新消息写入该表,并且始终是最大值。您可以做的另一件事就是将最后一条消息完全存储在那里,然后就不必为实际数据完全引用消息表了。我之前提到的相同分区键,也只需在其中写入实际消息。

答案 1 :(得分:0)

这是一个想法;将分区键更改为user_idpeer_id,然后可以使用PER PARTITION LIMIT构造。这将只读取单个行(每个分区),然后您也不必使用MAX,因为CLUSTERING ORDER BY (send_on DESC),第一行将是最近的:

> CREATE TABLE messages
    (user_id int, peer_id int, send_on timestamp, message text,
    PRIMARY KEY ((user_id, peer_id), send_on))
    WITH CLUSTERING ORDER BY (send_on DESC);

> SELECT peer_id, send_on, message
          FROM messages
          WHERE user_id = 1 AND peer_id=1
          PER PARTITION LIMIT 1;

 peer_id | send_on                         | message
---------+---------------------------------+---------
       1 | 2019-04-15 15:21:40.350000+0000 | hello 3

(1 rows)

> SELECT peer_id, send_on, message
          FROM messages PER PARTITION LIMIT 1;

 peer_id | send_on                         | message
---------+---------------------------------+---------
       3 | 2019-04-15 15:21:40.387000+0000 | hello 9
       2 | 2019-04-15 15:21:40.365000+0000 | hello 6
       1 | 2019-04-15 15:21:40.350000+0000 | hello 3

(3 rows)

注意:最后一个查询是一个多键查询,仅出于演示目的而已,显然在大型生产集群中不需要执行该操作。