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
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|
most flexible option for performance across a wide range of queries.
best overall performance for small scans (1000s of rows).
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.