Cassandra select 不同并按 cqlsh 排序
Cassandra select distinct and order by cqlsh
我是 Cassandra 和这个论坛的新手。我正在使用 cqlsh 执行 Cassandra 查询,但我不知道如何使用 Cassandra 执行 sql select distinct a, b, c from table order by d asc
之类的查询。我能怎么做? table 的结构是什么?
您的 primary key
由 partition keys
和 clustering columns
组成。
- DISTINCT 查询只能请求分区键。
- 聚集列支持 ORDER BY。
假设我们有一个样本 table 如下所示,
CREATE TABLE Sample (
field1 text,
field2 text,
field3 text,
field4 text,
PRIMARY KEY ((field1, field2), field3));
DISTINCT 要求所有分区键以逗号分隔传递。
所以你不能运行这个查询select distinct field1 from Sample;
。一个有效的表达式是 select distinct field1, field2 from Sample;
.
它在内部命中集群中的所有节点以查找所有分区键,因此如果您有数百万个分区table,我预计多个节点的性能会下降。
默认情况下,字段 3 的记录将按升序排列。下面的查询将按字段 3 的降序提供记录。
select * from Sample where field1 = 'a' and field2 = 'b' order by field3 desc;
如果您已经知道您的查询模式以及您要求数据排序的方式,您可以按照这种方式设计 table。假设您始终需要 field3 的降序记录,您可以这样设计 table。
CREATE TABLE Sample (
field1 text,
field2 text,
field3 text,
field4 text,
PRIMARY KEY ((field1, field2), field3))
WITH CLUSTERING ORDER BY (field3 DESC);
现在不使用 order by 进行查询将得到相同的结果。
您可以对多个聚簇列使用排序方式。但是你不能跳过订单。为了理解让我们有一个像下面这样的示例 table,
CREATE TABLE Sample1 (
field1 text,
field2 text,
field3 text,
field4 int,
field5 int,
PRIMARY KEY ((field1, field2), field3, field4));
我添加了一些虚拟记录。
您可以像这样使用多列排序select * from Sample1 where field1 = 'a' and field2 = 'b' order by field3 desc, field4 desc;
注意:所有字段都需要按正序 (field3 asc, field4 asc
) 或负序 (field3 desc, field4 desc
)。你不能这样做 (field3 asc, field4 desc
) 反之亦然。
上面的查询会产生这样的结果。
我们不能按顺序跳过顺序,我的意思是我们不能做类似 select * from Sample1 where field1 = 'a' and field2 = 'b' order by field4 desc;
的事情
希望对您有所帮助!
我是 Cassandra 和这个论坛的新手。我正在使用 cqlsh 执行 Cassandra 查询,但我不知道如何使用 Cassandra 执行 sql select distinct a, b, c from table order by d asc
之类的查询。我能怎么做? table 的结构是什么?
您的 primary key
由 partition keys
和 clustering columns
组成。
- DISTINCT 查询只能请求分区键。
- 聚集列支持 ORDER BY。
假设我们有一个样本 table 如下所示,
CREATE TABLE Sample (
field1 text,
field2 text,
field3 text,
field4 text,
PRIMARY KEY ((field1, field2), field3));
DISTINCT 要求所有分区键以逗号分隔传递。
所以你不能运行这个查询select distinct field1 from Sample;
。一个有效的表达式是 select distinct field1, field2 from Sample;
.
它在内部命中集群中的所有节点以查找所有分区键,因此如果您有数百万个分区table,我预计多个节点的性能会下降。
默认情况下,字段 3 的记录将按升序排列。下面的查询将按字段 3 的降序提供记录。
select * from Sample where field1 = 'a' and field2 = 'b' order by field3 desc;
如果您已经知道您的查询模式以及您要求数据排序的方式,您可以按照这种方式设计 table。假设您始终需要 field3 的降序记录,您可以这样设计 table。
CREATE TABLE Sample (
field1 text,
field2 text,
field3 text,
field4 text,
PRIMARY KEY ((field1, field2), field3))
WITH CLUSTERING ORDER BY (field3 DESC);
现在不使用 order by 进行查询将得到相同的结果。
您可以对多个聚簇列使用排序方式。但是你不能跳过订单。为了理解让我们有一个像下面这样的示例 table,
CREATE TABLE Sample1 (
field1 text,
field2 text,
field3 text,
field4 int,
field5 int,
PRIMARY KEY ((field1, field2), field3, field4));
我添加了一些虚拟记录。
您可以像这样使用多列排序select * from Sample1 where field1 = 'a' and field2 = 'b' order by field3 desc, field4 desc;
注意:所有字段都需要按正序 (field3 asc, field4 asc
) 或负序 (field3 desc, field4 desc
)。你不能这样做 (field3 asc, field4 desc
) 反之亦然。
上面的查询会产生这样的结果。
我们不能按顺序跳过顺序,我的意思是我们不能做类似 select * from Sample1 where field1 = 'a' and field2 = 'b' order by field4 desc;
希望对您有所帮助!