Cassandra的新特性

Cassandra的新特性汇总:

3.0:物化视图

物化视图(materialized view)

Whereas secondary indexes which are suited for low cardinality data, materialized views are suited for high cardinality data. Secondary indexes on high cardinality data require all nodes in a cluster to be queried, causing high read latency. With materialized views, the data is arranged serially based on the new primary key in a new table. Materialized views will cause hotspots if low cardinality data is inserted.

创建视图的限制条件:

  1. 原表的PK必须全部作为视图的PK的一部分,也就是说不允许原表的PK有些字段没有作为视图的PK
  2. 原表的普通字段添加到视图中,一次只能添加一个作为视图PK
  3. 也就是说视图的PK组成部分:原表的所有PK + 原表的一个普通字段

示例:

1
2
3
4
5
6
7
8
9
#假设原表的PK只有一个字段cid,当然可以根据业务需求设计PK为一个或多个
CREATE TABLE cyclist_mv (cid UUID PRIMARY KEY, name text, age int, birthday date, country text);

#将普通的age字段作为视图的PK的一部分,类似于二级索引
CREATE MATERIALIZED VIEW cyclist_by_age AS
SELECT age, birthday, name, country FROM cyclist_mv
WHERE age IS NOT NULL AND cid IS NOT NULL PRIMARY KEY (age, cid);

SELECT age, name, birthday FROM cyclist_by_age WHERE age = 18;

文章目录
  1. 1. 物化视图(materialized view)