Cassandra Materialized Views (Creation and Usage)

What is a Materialized View

A Materialized View is a feature in Cassandra that converts complex SELECT queries into physical tables, allowing data to be retrieved directly from this table.

Advantages

1. Reduced computational load: Materialized views reduce the computational burden on Cassandra because complex SELECT queries no longer need to be executed.

2. Improved performance: The results of a materialized view are already stored in the system and can be returned immediately to the client, thereby improving performance.

What is the format of a Materialized View?

1. CREATE MATERIALIZED VIEW statement

CREATE MATERIALIZED VIEW mv_name
WITH data_center = 'dc1',
        replication = {'replication_type': 'SimpleStrategy', 'replication_factor': 3}
AS
SELECT * FROM table_name;

2. Materialized View Options

  • data_center: The data center for the materialized view.
  • replication: The replication strategy for the materialized view.

Examples of Using Materialized Views

1. Create Table:

CREATE TABLE table_name (
        id uuid PRIMARY KEY,
        name text,
        age int
);

2. Create Materialized View:

CREATE MATERIALIZED VIEW mv_name
WITH data_center = 'dc1',
        replication = {'replication_type': 'SimpleStrategy', 'replication_factor': 3}
AS
SELECT * FROM table_name;

3. Query Materialized View:

SELECT * FROM mv_name;

Notes

  • Updates to materialized view data may take some time because the system needs to perform a full update.
  • If the table structure changes, the materialized view needs to be recreated.