You are on page 1of 13

Views in Snowflake

Veerendra
Agenda
• Views
• Types of Views
• Secured Views
• Materialized Views
• What is Materialized view?
• Refresh of Materialized views
• Cost of Materialized views
• When to create Materialized views
• Advantages of Materialized views
• Limitations of Materialized views
Views
• A View is a database object that contains SQL query built over one or multiple tables.
• A View can be considered as a virtual table that can be used almost anywhere that a
table can be used (filters, joins, subqueries, etc.).
• Whenever you query a view, the underlying SQL query associated with the view gets
executed dynamically and will fetch data from underlying tables.
• Views serve a variety of purposes like combining, segregating and protecting data.
• A view can be created with below syntax
CREATE OR REPLACE VIEW VIEW_NAME
AS
SELECT Statement;
Types of Views
In snowflake we can see 3 types of views.
1. Non-materialized views (Normal views)
2. Secure views
3. Materialized views
Secure Views
• A Secure View does not allow users to see the definition of the view.
• Users can’t see the underlying sql query.
• The definition of a secure view is only exposed to authorized users i.e. users who have
been granted the role that owns the view.
• 2 Advantages of secure views
• Can protect the data by not exposing to other users.
• I don’t want the users to see underlying tables present in our database.
• Use SECURE keyword to create secure views
CREATE OR REPLACE SECURE VIEW VIEW_NAME
AS
SELECT Statement;
Secure Views
• How to determine a view is secure view or not?
IS_SECURE column in the Information_Schema and Account_Usage tells us a view is secure
or not.

SELECT table_catalog, table_schema, table_name, is_secure


FROM mydb.information_schema.views
WHERE table_name = 'VIEW_NAME’;

SELECT table_catalog, table_schema, table_name, is_secure


FROM snowflake.account_usage.views
WHERE table_name = 'VIEW_NAME';
Materialized Views
• A materialized view stores pre-computed result set.
• Querying a materialized view gives better performance than querying the base
tables.
• Can create on a single table, can’t build on multiple tables.
• Designed for improved query performance when we are using same dataset
repeatedly.
• Available in Enterprise edition and higher.
• Can be created with MATERIALIZED key word
CREATE OR REPLACE MATERIALIZED VIEW VIEW_NAME
AS
SELECT Statement;
Refresh of Materialized Views
• No need to refresh the data manually.
• Snowflake performs automatic background maintenance of materialized views.
• When a base table changes, Snowflake runs a background process to keep the
materialized views up-to-date, but takes a minute to refresh.
Cost of Materialized Views
Materialized views impact your costs for both storage and compute resources:

• Storage Cost: Each materialized view stores query results, which adds to the monthly
storage usage for your account.
• Compute Cost: In order to prevent materialized views from becoming out-of-date,
Snowflake performs automatic background maintenance of materialized views. When a
base table changes, all materialized views defined on the table are updated by a
background service that uses compute resources provided by Snowflake. So there will be
compute cost associated with it.
When to create Materialized Views
• Create a materialized view when all of the following are true:
• The query results from the view don’t change often
• The results of the view are used often
• The query consumes a lot of resources means query takes longer time for
processing like aggregating data.

• Create a regular view when any of the following are true:


• The results of the view change often
• The results are not used often
• The query is simple
• The query contains multiple tables
Advantages of Materialized Views
• Improves the performance
• No need of additional maintenance, auto refresh of results
• Data accessed through materialized views is always current, regardless of the
amount of DML that has been performed on the base table.
Limitations of Materialized Views
• Can query a single table only
• Does not support Joins, including self-joins
• Does not support all aggregate and windowing functions
• When the base table is altered or dropped, the materialized view is suspended
• Materialized View cannot query
• Another Materialized View
• A Normal View
• A UDF(User Defined Function)
Thank You

You might also like