Materialized views

Projeto de banco de dados

Lis Sulmont

Curriculum Manager

Two types of views

Views

  • Also known as non-materialized views
  • How we've defined views so far
Projeto de banco de dados

Two types of views

Views

  • Also known as non-materialized views
  • How we've defined views so far

Materialized views

  • Physically materialized
Projeto de banco de dados

Materialized views

  • Stores the query results, not the query
  • Querying a materialized view means accessing the stored query results
    • Not running the query like a non-materialized view
  • Refreshed or rematerialized when prompted or scheduled
Projeto de banco de dados

When to use materialized views

  • Long running queries
  • Underlying query results don't change often
  • Data warehouses because OLAP is not write-intensive
    • Save on computational cost of frequent queries
Projeto de banco de dados

Implementing materialized views

(in PostgreSQL)

$$

CREATE MATERIALIZED VIEW my_mv AS SELECT * FROM existing_table;

$$

REFRESH MATERIALIZED VIEW my_mv;
Projeto de banco de dados

Managing dependencies

  • Materialized views often depend on other materialized views
Projeto de banco de dados

Dependency example

$$ Diagram of X and Y views with an arrow from X and Y indicating a dependency

Projeto de banco de dados

Managing dependencies

  • Materialized views often depend on other materialized views
  • Creates a dependency chain when refreshing views
  • Not the most efficient to refresh all views at the same time
Projeto de banco de dados

Tools for managing dependencies

  • Use Directed Acyclic Graphs (DAGs) to keep track of views $$ $$
  • Pipeline scheduler tools

Example of a directed acyclic graph

Logos of Airflow and Luigi

Projeto de banco de dados

Let's practice!

Projeto de banco de dados

Preparing Video For Download...