Third Normal Form, Star Schema, and a Performance Centric Data Strategy
Third Normal Form, Star Schema, and a Performance Centric Data Strategy
DBAs are expected to know more about data modeling than just throwing together a "normalized" database. What levels of normalization exist? Read on to get past the academics of the matter to see what it means for you.
Join the DZone community and get the full member experience.Join For Free
Databases are better when they can run themselves. CockroachDB is a SQL database that automates scaling and recovery. Check it out here.
Keeping it Straight
Data value comes from sharing, so staying organized and providing common data access methods across different groups can bring big payoffs.
Companies struggle daily to keep data formats consistent across applications, departments, people, divisions, and new software systems installed every year.
Passing data between systems and applications is called ETL, which stands for Extract, Transform, and Load. It is the process everyone loves to hate. There is no glamour in reconfiguring data such as date formats from one system to another, but there is glory in minimizing the amount of ETL needed to build new applications.
To minimize ETL friction, data architects often design schemas in third normal form, a database term that indicates data is well organized and unlikely to be corrupted due to user misunderstanding or system error.
Getting to Third Normal Form
The goal of getting to third normal form is to eliminate update, insertion, and deletion anomalies.
Take this employee, city, and department table as an example:
If Sam moves to Boston, but stays in two departments, we need to update both records. That process could fail, leading to inconsistent data.
If we have a new employee not yet assigned to a department and the "employee_dept" field does not accept blank entries, we would be unable to enter them in the system.
If the company closed department 42, deleting rows with department 42 might inadvertently delete employee's information like Lori's.
First Normal Form to Start
First, normal form specifies that table values should not be divisible into smaller parts and that each cell in a table should contain a single value.
So if we had a customer table with a requirement to store multiple phone numbers, the simplest method would be like this
However, this does not meet first normal form requirements with multiple values in a single cell, so to conform, we could adjust it to
Second Normal Form
Second normal form requires that
- Data be in 1st normal form
- Each non-key column is dependent on the tables primary keyConsider the following table with a primary key (customer_id)
In this example, city is a factor of the customer_id and not the customer_name, so in order to get this data into 2nd normal form, we must ensure that each non-key column is dependent only on the primary key (in this case customer_id)
Separating this into two tables ensures that is the case
Third Normal Form
We're almost there! With first normal form, we ensured that every column attribute only holds one value.
With second normal form, we ensured that every column is dependent on the primary key, or more specifically that the table serves a single purpose.
With third normal form, we want to ensure that non-key attributes are dependent on nothing but the primary key. The more technical explanation involves "transitive dependencies" but for the purpose of this simplified explanation, we'll save that for another day.
In the case of the following table, zip is an attribute generally associated with only one city and state. So it is possible with a data model below that zip could be updated without properly updating the city or state.
Splitting this into two tables, so there is no implied dependency between city and zip, solves the requirements for third normal form.
Benefits of Normalization
Normalizing data helps minimize redundancy and maintain the highest levels of integrity. By organizing column attributes and the relations between tables, data administrators can design systems for efficiency and safety.
More specifically, normalization helps ensure:
- Data is not unnecessarily repeated within a database.
- Inserts, modifications, and deletions only have to happen once in a database.
Data Management with Star Schema
Star schema is an approach of arranging a database into fact tables and dimension tables. Typically, a fact table records a series of business events such as purchase transactions. Dimension tables generally store fewer records than fact tables but may have more specific details about a particular record. A product attributes table is one example.
Star schemas are often implemented in a denormalized fashion, with typical normalization rules relaxed. The advantage of this can be simpler reporting logic and faster performance as data may be stored multiple ways to facilitate queries.
The disadvantage of this approach is that integrity is not necessarily enforced through the model leaving room for an update in one place that may not successfully propagate elsewhere.
Further, with normalization, a large variety of data analytics tools and approaches can be used to query data without explicit advanced knowledge. Without normalization, schemas tend to become isolated to specific functions and less flexible across a large organization.
Flexible Star Schema Deployments With MemSQL
Is it possible or desirable to merge normalization and star schemas? Sure.
While data management strategies can be very application specific, retaining data in the most universally accessible forms benefits larger organizations. With normalization, data organization transcends application use cases and database systems.
Star schemas often skip normalization for two reasons: simplicity of queries and performance.
Regarding query simplicity, this is a tradeoff between application-specific approaches and data ubiquity across an organization. Independent of the database, this tradeoff remains.
When it comes to performance, historical systems have had challenges with operations like fast aggregations, and a large number of joins driven by third normal form. Modern database architectures have eliminated those performance challenges.
With a solution like MemSQL, a memory-optimized, relational, distributed database, it is possible to achieve normalization and performance. Even with the increased number of tables, and subsequent JOINs, often resulting from third normal form, MemSQL maintains stellar performance. And the core relational SQL model makes it easy to create or import a range of tables as well as maintain relations between tables.
In the next sections, we'll explore table types in MemSQL and the associated benefits.
Using Multiple Table Types in MemSQL
MemSQL includes two table types:
- A rowstore table where all the data is retained in memory and all data is persisted to disk.
- A columnstore table where some data resides in memory and all data is persisted to disk.
Using these two table types, is it possible to design a wide range of schema configurations.
Contrary to popular belief, determining whether you use an all-memory or memory-plus-disk table has less to do with data size and more with how you plan to interact with the data.
Columnstores are useful when rows are added or removed in batches and when queries touch all or many records but only for a few columns. Aggregations like Sum, Average, and Count are good examples. Rowstores work well when operating over whole rows at a time. This includes updates to individual attributes or point lookups. For more detail on rowstores and columnstores, check out Should You Use a Rowstore or a Columnstore? From MemSQL VP of Engineering Ankur Goyal.
Creating a Star Schema in MemSQL
Whether you lean toward normalization, MemSQL makes it easy to create a star schema within a single database across multiple table types.
Figure: Basics of a star schema with Fact and Dimension tables
Both dimension tables and fact tables can be row or column based depending on requirements, which should be focused more on data use and operations rather than just capacity.
|Dimension Tables||Fact Tables||Recommendations|
|Row||Row||Most flexible option for performance across a wide range of queries.
Best overall performance for small scans (1000s of rows).
|Row||Column||Best when there is lots of Fact data.
Performance will be especially good when queries involve lots of scans.
|Column||Column||Good when there is lots of Dimension data and the data is append-only.|
Opportunities for Normalization and Star Schema
Whether your preferences sway toward normalization, star schema, or a combination, MemSQL provides a simple and easy way to achieve a standard data model that leverages the relational capabilities of ANSI SQL.
Beyond that, the MemSQL architecture delivers the utmost in performance and scalability, including the ability to ingest data from multiple systems and datastores for real-time analytics. This is a frequent use case for large corporations that need to track a variety of data sources with consolidated reporting.
Of course, handling all of this in real time brings another level of visibility and usefulness to the data. Now businesses can get a pulse of activity in the moment, allowing them to adapt and learn in real time.
Interested in giving MemSQL a try? Download it here http://www.memsql.com/download
Published at DZone with permission of Gary Orenstein , DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.