Views in Snowflake

Someone asked this question on Reddit

trying to build a data mart basically and most of the times it will be file source that will be transformed through ETL and eventually loaded into snowflake. So after that plan will be to build some views on top of these transformed tables which will be later used for data science purposes so wanted to know how to first approach building tables on snowflake (transient, permanent) i heard from some people suggesting to use transient tables in development environment So was getting confused what should be used when and how and when things like time travel should be enabled Along with that if there are best practices for roles accesses, data security, designing of tables and views will be great

I previously posted about tables here. As for views, I don’t really have strong opinions on them. I have so many strong opinions on other things, like cats, that there’s no room left for views. This post will again be derivative of Snowflake documentation, with a bit of advice.

There are four types of views according to Snowflake’s documentation:

Note that I will not be covering recursive views because, to be honest, I’ve never used them so I can’t add anymore than the Snowflake documentation includes. They can always be replaced with recursive CTEs, and the complication is in the recursion itself, not the creation of the view. So we will focus on:

Views, regular old views

What they are

I always tell people to think about views as just saved queries. Let’s say you wrote a query to find the number of cats per day:

SELECT 
    cat_name,
    SUM(cats)
FROM cat_info
GROUP BY cat_name

If you need to rerun that query often, you may save it in a Snowflake tab or a file and call it cat_count. All you are saving is the query that gets the data, not the data. Wouldn’t it be easy if you could name that save query and invoke it with one line? That is effectively a view. You would make that view like this:

CREATE OR REPLACE VIEW cat_view AS 
    SELECT 
        cat_name,
        SUM(cats)
    FROM cat_info
    GROUP BY cat_name

And you would call it like this: Select * from cat_view

Pros

Cons

Materialized views

What they are

Materialized views are…well, materialized. They are views that, instead of updating (running) whenever they are selected from, they update whenever there is an DML action to the underlying table. So if cat_info is updated with a MERGE or UPDATE at 10 am, cat_view will then update and materialize. When cat_view is queried throughout the day, it will produce the same materialized view.

Pros

Cons

Secure views

What they are

Secure views can be either regular or materialized; the difference is that they protect the underlying data and the view definition from end users. If the latter isn’t important to you, then don’t use them. To be clear, users in Snowflake do not need access to cat_info in order to query cat_view, whether you use a secure view or not. The difference is subtler than that; the Snowflake website gives a particularly terrifying example of a user forcing an error to see if there is any data in the underlying table:

SELECT *
FROM widgets_view
WHERE 1/iff(color = 'Purple', 0, 1) = 1;

According to their documentation, his query can be used to determine if there are any purple widgets even if widgets_view is filtered to red widgets only. Additionally, a secure view means that the end user can’t use GET_DDL or SHOW VIEWS to find out the view definition.

Pros

Cons