Building a Data Warehouse, Part 2: Building a New Schema
Join the DZone community and get the full member experience.Join For Free
in part i we looked at when you should build your data warehouse and concluded that you should build it sooner rather than later to take advantage of reporting and view optimization. today we will look at your options to build your data warehouse schema.
when architecting a data warehouse, you have two basic options: build a flat “reporting” table for each operation you are performing, or build with bi/cubes in mind and implement a “star” or “snowflake” schema. let’s take a quick look at the first option and then we will take a look at the star and snowflake schemas.
whenever the business requests a complex report, developers usually slow down the system with a complex sql statement or operation. for example, pretend in our order entry system (oltp) the business wants a report that says this: show me the top ten customers in each market including their overall rank. you would usually have to perform a query like this:
- complex joins for unique customer
- rollup the sales
- ranking functions to determine overall rank
- partition functions to segment the rank by country
- standard aggregates to get the sales
- dump all of this to a work table in order to pull out the top 10 (if you don’t do this, you will lose the overall rank)
a typical sql statement to do steps 1-5 would look like this:
with cteterritory as ( select cr.name as countryname, customerid, sum(totaldue) as totalamt from sales.salesorderheader soh inner join sales.salesterritory ter on soh.territoryid=ter.territoryid inner join person.countryregion cr on cr.countryregioncode=ter.countryregioncode group by cr.name, customerid ) select *, rank() over (order by totalamt desc) as overallrank, rank() over (partition by countryname order by totalamt desc, customerid desc) as nationalrank from cteterritory
argh! no wonder developers hate sql and want to use orms! (i challenge the best orm to make this query!)
instead you can create a table, lets call it salesrankbyregion, with the fields: countryname, customerid, totalsales, overallrank, and nationalrank, and use the above sql as part of a synchronization/load script to fill your reporting table on a regular basis. then your sql statement for the above query looks like this:
select * from salesrankbyregion where customernationalrank between 1 and 10 order by countryname, customernationalrank
the results look like:
that is more like it! a simple select statement is easier for the developer to write, the orm to map, and the system to execute.
the salesrankbyregion table is a vast improvement over having to query all of the oltp tables (by my count there are three tables plus the temp table). while this approach has its appeal, very quickly, your reporting tables will start to proliferate.
your best option is to follow one of the two industry standards for data warehouse tables, a “star” or a “snowflake’ schema. using a schema like this gives you a few advantages. they are more generic than the salesrankbyregion, which was a table built for one query/report, giving you the ability to run many different reports off each table. another advantage is that you will have the ability to build cubes very easily off of a star or snowflake schema as opposed to a bunch of salesrankbyregion tables.
the design pattern for building true data warehouse tables are to build a “fact” table, or a table that contains detail level (or aggregated) “facts” about something in the real world, like an order or customer for example. inside of the fact table you will also have “measures” or a numeric value that represents a “fact.” to support your fact table you will have “dimension” tables. dimensions are a structure that will categorize your data, usually in the form of a hierarchy. a dimension table for example could be “time” with a hierarch of orderyear, orderquarter, ordermonth, orderdate, ordertime.
there are tons of tutorials on the internet that show you how to build a star or snowflake schema and the difference between them, so i will not repeat them here. (you may want to start here .) i’ll give you the high level on a simple star schema here.
let’s say we have an order entry system, such as northwind (in the microsoft sql server sample database.) you can have a fact table that revolves around an order. you can then have three (or more) fact tables that focus on: time, product, and salesperson. the time dimension would roll-up the order date by year, quarter, month, and date. the product dimension would roll-up the product by the product and category. (in most systems you would have a much deeper hierarchy for products.) the salesperson dimension would be roll-up of the employee, the employee manager and the department they work in. the key in each of these tables would then be foreign keys in the fact table, along with the measure (or numerical data describing the fact.)
there is an example similar to this in programming sql server 2008 , a book where i am a co-author. here is modified version of that demo:
create table [dwh].[dimtime] ( [timekey] [int] identity (1, 1) not null primary key, [orderdate] [datetime] null , [year] [int] null , [quarter] [int] null , [month] [int] null ) create table [dwh].[dimproduct] ( [productid] [int] not null primary key, [productname] nvarchar(40) not null, [unitprice] [money] not null, [categoryid] [int] not null, [categoryname] nvarchar(15) not null ) create table [dwh].[dimemployee] ( employeeid int not null primary key, employeename nvarchar(30) not null, employeetitle nvarchar(30), managername nvarchar(30) ) fact table: create table [dwh].factorder ( [postalcode] [nvarchar] (10) collate sql_latin1_general_cp1_ci_as null , [productid] [int] not null , [employeeid] [int] not null , [shipperid] [int] not null , [total sales] [money] null , [discount] [float] null , [unit sales] [int] null , [timekey] [int] not null )
we have the basis of a star schema. now we have to fill those tables and keep them up to date. that is a topic for part iii.
Published at DZone with permission of Stephen Forte, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.