Denormalizing with materialized views
There are times when your use case requires you to read data using an alternate key entirely. In order to be able to read your data by partition key, and in sorted order, it is often necessary to write data in more than one way. Prior to version 3.0, we would accomplish this by literally creating and writing to multiple tables, one for each query type.
Fortunately Cassandra now provides an alternative, called materialized views. This new feature handles the administrative task of populating these alternate table views, removing the burden from our application and reducing the risk of orphaned data.
Creating a materialized view is straightforward. As an example, let's say we need to query for all authors in a given year, which is not possible with the authors
table introduced earlier. To accomplish this, we need a view that specifies a primary key starting with the year
column, followed by clustering columns for both name
and title
:
CREATE MATERIALIZED VIEW...