首页 > 解决方案 > 按主分区分组

问题描述

我无法在主分区上执行 Group by。我正在使用 Cassandra 3.10。当我分组时,我收到以下错误。 InvalidReqeust: Error from server: code=2200 [Invalid query] message="Group by currently only support groups of columns following their declared order in the Primary Key. 即使我仍然面临问题,我的列也是主键。

我的架构是

Table trends{
name text,
price int,
quantity int,
code text,
code_name text,
cluster_id text
uitime timeuuid,
primary key((name,price),code,uitime))
with clustering order by (code DESC, uitime DESC)

我运行的命令是:select sum(quantity) from trends group by code;

标签: cassandra

解决方案


首先,您的架构无效。您不能在代码上设置聚类顺序,因为它是分区键。顺序将由它的哈希确定(除非使用字节顺序分区器 - 但不要这样做)。

不过,您谈论的查询和事情确实有效。例如,您可以运行

> SELECT keyspace_name, sum(partitions_count) AS approx_partitions FROM system.size_estimates GROUP BY keyspace_name;

 keyspace_name      | approx_partitions
--------------------+-------------------
        system_auth |               128
              basic |           4936508
          keyspace1 |               870
 system_distributed |                 0
      system_traces |                 0

他们的架构在哪里:

CREATE TABLE system.size_estimates (
    keyspace_name text,
    table_name text,
    range_start text,
    range_end text,
    mean_partition_size bigint,
    partitions_count bigint,
    PRIMARY KEY ((keyspace_name), table_name, range_start, range_end)
) WITH CLUSTERING ORDER BY (table_name ASC, range_start ASC, range_end ASC)

您提供的伪模式可能与实际模式不同。describe table xxxxx你能在你的问题中提供输出吗?


推荐阅读