This document presents various data modeling techniques that may be followed for your presentation layer of operational data warehouse or data lake. It presents various techniques for data modeling using google Big Query and similar technologies like Redshift, Snowflake, Azure SQL data warehouse, Couchbase, MongoDB and so on. Based on use cases and usage patterns, these techniques can be adopted for different modules and projects within our data lake sphere.
The presented data model techniques are a combination of denormalized (Flattened), Dimensional and Set/Relational models that should suit all your use cases.
These data modeling techniques will be used for operational Big Query or similar datasets from where the end users access data through various tools like Tableau.
Business master data
This data contains all of your business master data and will be stored in a separate dataset. This data will be shared among all other projects/datasets.
This will help you to avoid duplicating master data thus reducing manageability. This will also provide a single source of truth so that different projects don't show different values for the same.
As this data is very critical, we will follow type 2 slowly changing dimensional approach which will be explained later in this document.
There are lot of MDM tools available to manage master data more appropriately but for moderate use cases, you can store this using database you are using.This metadata is neither business metadata or the technical metadata but may need to store some data that will help your ETL/ELT workflows and various other systems. This may help in reducing hard coding within the ETL/ELT pipelines and any other systems. The example of this data includes external APIs, FTP/SFTP site information and their access credentials and any other reference data. When that information changes, you can update corresponding tables and don’t need to modify jobs and other references where it's being used.
This data will not be used by any end users or reports.
Transactional data
This is the main project specific data which will have all the data related to various projects which includes historical data as per the business retention requirements.
Different datasets will be created for different projects. Generally, this data along with business metadata will be consumed by end user applications like Tableau reports.
Metadata and Cataloging
This is entirely different kind of metadata and to avoid confusion, this is handled separately. This data is not stored in any tables and not used by end users or any systems/applications.
All the tables need to have the following labels so that we can integrate it with metadata catalog tool, so that users can search and view the data easily.
Handling
slowly changing dimensions is the key for any successful operational data lake
or data warehousing implementations. It's a little harder to implement this for
traditional databases like Oracle and SQL Servers but as most of the modern
databases like Big Query allow complex types, so it became a little easier to
handle slowly changing dimension types in the modern Big Data type databases.
In the traditional Data warehouse implementations, the following are the 3 types of slowly changing dimensions:
In a Type 1 SCD the new data overwrites the existing data. Thus, the existing data is lost as it is not stored anywhere else. This is the default type of dimension we create. We do not need to specify any additional information to create a Type 1 SCD.
Type 2 SCDs - Creating another dimension record
A Type 2 SCD retains the full history of values. When the value of a chosen attribute changes, the current record is closed. A new record is created with the changed data values and this new record becomes the current record. Each record contains the effective time and expiration time to identify the time period between which the record was active.
Type 3 SCDs - Creating a current value field
A Type 3 SCD stores two versions of values for certain selected level attributes. Each record stores the previous value and the current value of the selected attribute. When the value of any of the selected attributes changes, the current value is stored as the old value and the new value becomes the current value.
In the modern data lake implementations, we can club together Type 2 and Type 3 into one, we can call it as “retaining historical values”.
So, there are only 2 types of SCDs in Big Data implementations
In this type, the new data overwrites the existing data. Thus, the existing data is lost as it is not stored anywhere else. This is the default type of dimension we create. We do not need to specify any additional information to create this type of dimension.
In this type, we retain historical changes using complex data types available within Big Data Technologies. This is some kind of semi structured type and will have full control of up to what extent we can retain historical changes. JSON, MAP, STRUCT are some example types of storage options.
This will give full freedom on how much retention we can use and any associated values like when it was changed and how it was changed and so on. Also, this can be different for different dimensions.
How to handle Type 2 and Type 3 together
Type 2 and Type 3 (in the big data world, “retaining historical values”) can be handled in Big Query and similar technologies using complex data types. Wherever we need to retain historical data for any dimension, we need to include the following column to be created along with dimensional tables and be managed through data flow jobs.
SCD_History ARRAY<STRUCT<updatedon DATETIME or TIMESTAMP, columnname STRING, oldvalue STRING, newValue STRING>>
the above syntax is for Big Query. You need to find similar data type for different type of databases like Redshift, Snowflake, Couchbase and so on. For example, Cassandra you can use MAP data type, MongoDB, you can use nested JSON and so on. Where complex data type not allowed, you can use STRING or TEXT types and use JSON format.
Supplier_Key | Supplier_Code | Supplier_Name | Supplier_State | SCD_History |
---|---|---|---|---|
123 | ABC | Acme Supply Co | IL | [{updatedOn: 22-Dec-2004, columnName: Supplier_State, OldValue: CA, newValue: IL}] |
The following are the various data modeling techniques that can be used to implement various use cases within your data lake (presentation layer not in raw layer). Which technique you use will be decided based on the requirements like how the data is accessed by end users and how the data gets into our database like Big Query.
Different use cases, projects or modules within our projects can follow one or more of the below modeling techniques to satisfy the requirements.
In this modeling technique, we denormalize and flatten fact with its dimensional data. You should be careful in choosing this approach and this technique works well with very simple use cases and does not work with most of the complex data warehouse needs.
Advantages of using flattened denormalized model
●Simple to query which avoids joins.
As all the data stored in one table, it's easy to query and don’t need to join many tables.
●Perform well with latest Big Data technologies including Big Query.
Most of the Big Data technologies which are all distributed in nature prefer this kind of data model and performs very well.
Disadvantages of using flattened denormalized model
Here are some of the reasons why this technique does not work with most of the use cases:
●Where dimensional data changes
When the dimensional data changes, we need to update millions of rows within a big denormalized table. It is very time consuming; we cannot use this technique when dimensional data constantly changes.
●Where there are too many attributes
Some dimensions are simple like states which will have state code and name, but most of the dimensions will have too many attributes like Patient. Patient dimension will have too many attributes like id, name, date of birth, address, insurance and so on. It's difficult to tuck all the information into a single denormalized flattened table.
●Dimensional data is nested and has complex hierarchies.
Sometimes dimensional data is nested like Region dimension will have regions, countries within region and states within countries and cities within states. It's very difficult to maintain all that data into one single wide table.
●Where they need to keep track of changes
Most of the time we need to handle historical changes to dimension data which will be difficult to handle if we go with denormalized flattened approach.
●Users need to see dimensional values in drop-down lists
Most of the time when users use front-end applications including reporting tools like Tableau, they need to see dimensional values in the drop-downs to filter out and/or slice and dice data, which will be a huge process to get all distinct values for that dimension from millions or billions of rows from flattened tables.
Big Query, Snowflake and some data warehouse databases allows joins among tables, so Modified Star Schema approach with some changes may be a good data modeling approach for our data lake implementations. Where the joins not allowed, might requires multiple queries to achieve same if we use this model, for example Cassandra.
Here are some changes we can make to the traditional star schema approach.
●Club together Type 2 and Type 3 slowly changing dimensions when we need to track historical changes. This is explained above under slowly changing dimensions.
● Club together similar dimensions into one big denormalized dimension table.
Example, using state, country and region dimensions separately, using one-dimension table can be called “Geography” or “Location” which will have region, country and state information together will perform better and a good approach.
● Grouping together multiple facts into one big fact table
Similarly, we can group together multiple fact tables into single or less number of denormalized fact tables, so that users don't need to use multiple fact tables in their queries and instead use a single flattened fact table that will speed up the query performance.
The whole idea is to use denormalized/flattened schema model as much as possible which will reduce the number of tables and thus joins within queries. At the same time, the modified star schema approach which addresses shortcomings that we mentioned earlier while using fully denormalized flattened schema approach.
There are other kinds of data modeling techniques that might suit certain operational data lake needs may be considered based on the use case and requirements.
In this case we use denormalized schema models which tie together facts and certain dimensions together. The dimension that will never change and simple will go along with the fact table and complex dimensions and which will change constantly will be kept separate.
Sometimes we can keep part of the dimension that is used by end user applications or reports will become part of the fact table and other parts of the dimension will stay in separate tables.
You can even keep the nested dimensions that will not change over time be part of the fact table using complex data types like ARRAY<STRUCT>> in case of Big Query or JSON (MongoDB) or MAP(Cassandra) columns in case of any other NoSQL databases.
This is another kind of data modeling technique that is used when using NoSQL technologies like Cassandra, HBase, Dynamo and so on where “Joins” among tables are not allowed.
The table is designed based on the query usage patterns. This model helps to serve data very fast but complex to maintain. Also, there will be too much redundancy.