What you will be able to do
- Explain what declared primary and foreign keys add to a data model on Snowflake standard tables, and what they do not guarantee
- Describe a star schema and why its fact-to-dimension key joins work well with BI tools and the optimizer
- Describe Data Vault hubs, links and satellites, and how a Snowflake satellite holds semi-structured data in a VARIANT column
- Weigh hash keys against natural keys in a Snowflake Data Vault
Key concept
Informational constraints — On Snowflake standard tables, PRIMARY KEY, UNIQUE and FOREIGN KEY constraints describe the model but are not checked when data is written. They tell people and tools how the tables relate. Keeping the data consistent with them is the job of your load process.
1.What a data model gets from declared keys
A dimensional star schema and a Data Vault both describe relationships between tables with primary and foreign keys. On Snowflake standard tables, those keys describe the model but do not police it, which is different from many other databases. The documentation says constraints on standard tables exist "primarily for data modeling purposes and compatibility with other databases", and also to support client tools that read them.
The metadata is still useful. Declared keys help a project team understand the schema design and see how the tables relate. Most BI and visualization tools also import foreign key definitions and build join conditions from them. That means each developer doesn't have to guess the joins for themselves, so joins are configured consistently.
CREATE OR REPLACE TABLE salespeople (
sp_id INT NOT NULL UNIQUE,
name VARCHAR DEFAULT NULL,
region VARCHAR,
constraint pk_sp_id PRIMARY KEY (sp_id)
);
CREATE OR REPLACE TABLE salesorders (
order_id INT NOT NULL UNIQUE,
quantity INT DEFAULT NULL,
description VARCHAR,
sp_id INT NOT NULL UNIQUE,
constraint pk_order_id PRIMARY KEY (order_id),
constraint fk_sp_id FOREIGN KEY (sp_id) REFERENCES salespeople(sp_id)
);Nothing in this DDL stops a row in salesorders from pointing to an sp_id that doesn't exist in salespeople. On standard tables, primary-key/foreign-key relationships are informational. Of the declared constraints, only NOT NULL is enforced here. So a model built on standard tables relies on its load pipeline, not the database engine, for referential integrity. That trade-off runs through both models on this page: you get flexible, fast loading, and you take on responsibility for data quality.
Checkpoint 1 of 4· Check yourself
Primary and foreign keys on Snowflake standard tables are not enforced. Which of these is a real reason to declare them in a data model anyway?
Declared keys are metadata that people and tools use. BI tools read foreign key definitions to build correct joins. Snowflake does not reject violating rows on standard tables, and only hybrid tables require a primary key.
“most business intelligence (BI) and visualization tools import the foreign key definitions with the tables and build the proper join conditions”Source: docs.snowflake.com
2.Star schema: a fact table surrounded by dimensions
A star schema puts a central fact table in the middle and joins it to dimension tables. Snowflake's own semantic-view modeling guidance recommends it as the place to start. The documentation's join examples use a small star: dim_products has one row per product, and fact_sales has one row per sale.
| Table | One row per | Role of product_id |
|---|---|---|
| dim_products | product available for purchase | Uniquely identifies a product |
| fact_sales | sale of a product | Identifies the product sold; matches dim_products.product_id |
The benefit is simple join paths. Every query joins a fact to a dimension on the dimension's key, which is exactly the join a BI tool builds from declared foreign keys. Because the shape is so regular, the optimizer can also remove a dimension join that a query doesn't need. It only does this when you mark the keys as trustworthy with the RELY constraint property.
The limitation follows from the key concept. The star only gives correct answers if product_id is unique in dim_products and every fact_sales.product_id exists there. On standard tables Snowflake doesn't check either of those rules, so you are responsible for enforcing them. Your pipeline has to guarantee the star's integrity before you tell the optimizer to rely on it.
Checkpoint 2 of 4· Check yourself
A query selects only fact_sales columns but still joins dim_products on product_id. Which constraint property must the key carry before Snowflake can drop that unnecessary join?
Join elimination happens only when the RELY property says the data complies with the key constraints. On standard tables, setting ENFORCED isn't supported.
“These optimizations are performed only if you use the RELY constraint property”Source: docs.snowflake.com
3.Data Vault: hubs, links and satellites
Data Vault splits a model into three kinds of tables. Hubs hold business keys. In Snowflake's Data Vault example, the source system's untreated key stays with the descriptive data, and the treated business key goes to the hub table. Links hold relationships. They are designed for many-to-many (associative) cardinality, but the same link table can also hold mandatory 1:1 or 1:M relationships and optional ones. Satellites hold descriptive content, and a satellite is always the child of either a hub or a link. If the content is transactional, it usually goes in a link-satellite.
On Snowflake, a satellite can carry semi-structured data directly. Snowflake's guidance adds a Data Vault metadata column, dv_object, that stores the original document as a VARIANT. The document can be JSON, XML, an array or an object, and you can query it in place. This lets the vault grow without refactoring each time the source changes shape.
The drawback is performance: semi-structured content is slower to query, and nested content may need flattening before you can reach an attribute. To deal with this, profile the data and copy the critical data elements into strongly typed columns in the same satellite. In the example, those are the business event timestamp (used as the applied date), the business key and the source system's event ID. Typed columns can also improve pruning. Snowflake's table-design guidance also says to store dates and timestamps in DATE or TIMESTAMP columns rather than VARCHAR. Don't flatten nested content at staging. If the time from business event to analytical value is critical, let the dashboard flatten the content at query time.
Checkpoint 3 of 4· Match them up
Match each Data Vault element to its role in a Snowflake vault
Tap a term, then the definition that fits it.
Hubs hold keys, links hold relationships, and satellites hold descriptive context, including the raw VARIANT document on Snowflake.
“A satellite is either the child table of a hub or a link table.”Source: www.snowflake.com
4.Hash keys or natural keys
In Snowflake's Data Vault example, the staging view builds the hub hash key with sha1_binary. It hashes the tenant ID, the business key collision code and the treated customer ID together. The alternative is to use natural keys with no hashing. In that case the natural keys must be kept in the satellite so it can join to every related hub. Teams choose this when they find hashing too costly or slow. The trade-off is that queries against the satellite need more columns to join on.
Pick one approach for the whole vault. A Data Vault should use either natural keys or hash keys, not a mix of the two. Mixing them forces users to treat each table differently and makes the vault inconsistent to work with. Collision codes and multi-tenant IDs add to the problem, because they have to appear in every join. If hashing cost is the worry, Streams and Tasks can reduce how much data is processed into the vault, which cuts the hashing work.
Natural business keys often span several columns. Snowflake supports multi-column keys, but they must be declared out-of-line. Like any key on a standard table, they describe the vault's structure and aren't enforced, so your load process has to keep hubs, links and satellites consistent.
Checkpoint 4 of 4· Check yourself
A team builds its Data Vault on natural keys to avoid the cost of hashing. What should it expect?
Natural keys have to be kept in the satellite to join to each hub, so joins use more columns. Mixing key styles is discouraged, and key constraints on standard tables are not enforced.
“queries running on this satellite will essentially need to use more columns to join on”Source: www.snowflake.com
Exam traps
Each one states something that sounds right. Open it to see what is actually true.
1.Using hash keys for some hubs and natural keys for others is a fine way to balance hashing cost.Why is that wrong?
Snowflake's Data Vault guidance says to build the vault on either natural keys or hash keys, not both. A mix makes users treat each table differently and makes the vault inconsistent.
Covered in Hash keys or natural keys
Sources
Every claim above is drawn from one of these pages, quoted as it was written on the date shown.
- 1.
“Constraints on standard tables are provided primarily for data modeling purposes and compatibility with other databases”
↩︎ What a data model gets from declared keys - 2.
“enable your project team to understand the schema design”
↩︎ What a data model gets from declared keys“referential integrity constraints, as defined by primary-key/foreign-key relationships, are informational; they are not enforced”
↩︎ What a data model gets from declared keys“Snowflake stores DATE and TIMESTAMP data more efficiently than VARCHAR, resulting in better query performance.”
↩︎ Data Vault: hubs, links and satellites“referential integrity constraints, as defined by primary-key/foreign-key relationships, are informational; they are not enforced”
↩︎ Key concept“most business intelligence (BI) and visualization tools import the foreign key definitions with the tables and build the proper join conditions”
↩︎ Checkpoint - 3.
“A simple star schema (a central fact table joined to dimension tables) is a good starting point.”
↩︎ Star schema: a fact table surrounded by dimensions - 4.
“dim_products is a table that contains a row for each product available for purchase.”
↩︎ Star schema: a fact table surrounded by dimensions“For standard tables, you are responsible for enforcing constraints on the data.”
↩︎ Star schema: a fact table surrounded by dimensions“These optimizations are performed only if you use the RELY constraint property”
↩︎ Checkpoint - 5.https://www.snowflake.com/en/blog/handling-semi-structured-dataSecondary source
“link tables are designed to serve many-to-many/associative (M:M) cardinality”
↩︎ Data Vault: hubs, links and satellites“This new column will store the original semi-structured data as data type VARIANT.”
↩︎ Data Vault: hubs, links and satellites“you need to persist those key-pairs as strongly typed structured columns within the same satellite table”
↩︎ Data Vault: hubs, links and satellites“We can mitigate the cost of hashing by using Snowflake features such as Streams and Tasks”
↩︎ Hash keys or natural keys“A Data Vault should be produced as either natural-key or hash-key based and not a mix of the two.”
↩︎ Exam trap 1“doing this at ingestion time causes a delay in loading the satellite table”
↩︎ Prediction“A satellite is either the child table of a hub or a link table.”
↩︎ Checkpoint“queries running on this satellite will essentially need to use more columns to join on”
↩︎ Checkpoint - 6.
“Multi-column constraints must be created with a separate out-of-line clause that specifies the columns in the constraint.”
↩︎ Hash keys or natural keys