Materialized Views in Netezza - learn Reading Time: 5 minutes

Materialized View is a sorted projection of one and only one table MVs reduce the width of the data being scanned by creating a thin version of the table–based on frequently queried columns. This selection of frequently queried columns is called projection.

MVs are typically sorted like the base tables, but can be ordered on columns different from base table. This reduces table scans due to enhanced zone maps. and it improves query performance if the sorted columns are included in the “where” clause of the query.  Essentially, MVs improve performance by reducing the amount of data transferred from the disk to the CPU-RAM.

 

Materialized view will always has same distribution key as base table. Materialized view inherits the insertion order from base table if the order by is not specified. New rows that are inserted to to the base table are appended to end of MV.

 

The materialized views containing the sorted projection (columns) is stored in a table on disk and is used to increase query performance. A materialized views reduces scan time for multi-column queries that examine only a few columns and a small subset of the overall base table.

we will cover these topics: hide 1) Benefits of Materialized views 2) Following are some of the benefits of Materialized views: 3) Restriction on Materialized Views 4) Materialized Views: Best Practices 5) Materialized Views: Maintenance 6) Drop materialized views 7) To create materialized view 8) What to read next? Benefits of Materialized views Following are some of the benefits of Materialized views: Restriction on Materialized Views

Like any other databases, Netezza also has the some restrictions on the Materialized views:

Materialized Views:  Best Practices

Below are the some of the best practices you should follow:

Materialized Views: Maintenance

Below are the some of maintenance tips:

e.g.

$nzsql TRAINING(admin)=>ALTER VIEWS ON customer MATERIALIZE SUSPEND;

To rebuild a view after a base table change, use the CREATE OR REPLACEMATERIALIZED VIEW command to update the view, as follows:

MYDB.SCHEMA(ADMIN)=>CREATE OR REPLACE MATERIALIZED VIEW weather_v AS SELECTcity, temp_lo, temp_hi FROM weather ORDER BY city;CREATE MATERIALIZED VIEW

Do not drop and re-create the materialized view because those steps result in anew view with a different object ID, which can affect other objects that reference the materialized view.

Drop materialized views

When you use SQL to drop a materialized view, the system removes the view definition, the materialized table that contains the materialized records, and frees the disk storage that is allocated to the table. To drop a materialized view, enter:

MYDB.SCHEMA(USER)=>DROP VIEW customers_mview;

When you create a materialized views from a base table, the Netezza system stores the view definition for the lifetime of the SPM view and is visible as a materialized view. SPM view data slices are co-located on the same data slices as the corresponding base table data slices hence increases the performance of the query.

A materialized views reduces the width of number of columns being scanned in a base table, these type of view contains a small subset of frequently queried columns. When you query the table (table with large number of columns) Materialized Views  avoid scanning unreferenced columns from the base tables.

E.g. If the base table has 300 columns, and only 4 columns to be referenced then you can create materialized view on top of base table by taking only those 10 columns.

To create materialized view

CREATE MATERIALIZED VIEW CUSTOMER_DIM_MVASSELECTcustomer_key,persona_key,customer_birthdate,customer_zip,update_datetimeFROMCUSTOMER_DIMORDER BY customer_key;

selectcustomer_key, customer_birthdate, customer_emailfrom (select base.customer_key,cd.customer_birthdate,cd.customer_email,row_number() over (partition by base.customer_key order by cd.update_datetime desc) trans_datefrom extract_base basejoin CUSTOMER_DIM cd on base.customer_key = cd.customer_key) awhere a.trans_date = 1;



Meet Ananth Tirumanur. Hi there 👋

I work on projects in data science, big data, data engineering, data modeling, software engineering, and system design.

Connect with me:

My Resources:

Languages and Tools:

AWS, Bash, Docker, Elasticsearch, Git, Grafana, Hadoop, Hive, EMR, Glue, Athena, Lambda, Step Functions, Airflow/MWAA, DynamoDB, Kafka, Kubernetes, Linux, MariaDB, MySQL, Pandas, PostgreSQL, Python, Redis, Scala, SQLite