У меня есть созданная таблица кассандры, например:
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 строк.Есть ли способ оптимизировать это?Может быть, изменить мою схему?