Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Enforcing surrogate key uniqueness in slwoly changing dimension Expand / Collapse
Author
Message
Posted Friday, December 9, 2011 3:30 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Wednesday, September 24, 2014 3:19 AM
Points: 33, Visits: 292
Comments posted to this topic are about the item Enforcing surrogate key uniqueness in slwoly changing dimension
Post #1219727
Posted Tuesday, January 3, 2012 8:49 PM


SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Yesterday @ 12:35 PM
Points: 204, Visits: 963
Maybe I didn't get something but I have not heard anything about the rule: "for each unique value of the natural key there should be only one value of the surrogate key". I always thought that SCD works differently. So my questions are:

1. Where is a Primary Key on the table? This is a fundamental requirement for ANY relational table.
2. How are you going to join a Fact table to this dimension table if there is no unique constraint?
3. Isn't it a best practise to create surrogate key as autoincremental IDENTITY field? So it can be a Primary Key as well and in this case why do we need any special unique constraints on an indexed view?

For DW with SCD Type 2 I found useful this index to check Natural Key uniqueness:

CREATE UNIQUE INDEX [IndexName] ON [TableName](Natural_Key) WHERE Date_To IS NULL




Alex Suprun
Post #1229643
Posted Wednesday, January 4, 2012 2:43 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Wednesday, September 24, 2014 3:19 AM
Points: 33, Visits: 292

Alexander , you are right : in my script is missing the Primary Key definition for the dimension .
The primary key will be on Surrogate_Key, Date_From .
This is not orthodox implementation of SCD Type 2 : the reason-to-be for Surrogate_Key is not to be and identity,
but to replace the alphanumeric Natural_Key with a numeric sequence to perform more efficient joins and
to take less space in fact tables.

Fact tables should contain Surrogate Key and Fact_Date to be joined with the dimension
Example :
  select * from fact_table a left join dim_scd_prova b on a.Surrogate_Key = b.Surrogate_Key 
and a.Event_Date between b.Date_From and isnull(b.date_to, '2100-01-01')

This method , although not orthodox, works fine, also because in general all fact tables contains a date.
Not using the Surrogate_Key would require to perform less efficient joins on the Natural_Key,
that is in general alphanumeric

This method requires too that for each Natural_Key there is only one Surrogate_Key, and my solution guarantees it
at the database level .

Your proposed solution
 
CREATE UNIQUE INDEX [IndexName] ON [TableName](Natural_Key) WHERE Date_To IS NULL


is clever, but does not examine the whole table, but only the current version,
and in some cases is not enough
Post #1229737
Posted Wednesday, January 4, 2012 9:59 AM


SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Yesterday @ 12:35 PM
Points: 204, Visits: 963
fediori,
This is interesting...
But what are the benefits of this approach comparing to the one when surrogate key is IDENTITY?



Alex Suprun
Post #1230120
Posted Thursday, January 5, 2012 12:39 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Wednesday, September 24, 2014 3:19 AM
Points: 33, Visits: 292
Benefit is a more loosely coupling between dimension and fact table .
It mays save up from need of recalculating the fact table if something go wrong, because the fact table contains only the reference to the entity contained in the dimension instead of the reference to the entity at a specific time .
Very often Sql Server is IO bound, so for the DBMS to perform a join on 2 columns instead of one is not problem since there is plenty of CPU
Post #1230520
Posted Thursday, January 5, 2012 7:55 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, September 23, 2014 1:03 AM
Points: 7, Visits: 760
you now have a composite PK, which isn't ideal, and joining to facts will always require 2 fields in the join clause.

the PK is now bigint + datetime, which is 8+8 bytes, double the amount of space than just a PK on the bigint alone.

you are building 'intelligence' into your PK - this should never be. your PK should be 'dumb', with no business value / logic.

i suggest you drop this SK field, use your PK as autoincrement and make it the SK, which joins to your FK in your fact table.
Post #1231230
Posted Monday, January 9, 2012 2:24 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Wednesday, September 24, 2014 3:19 AM
Points: 33, Visits: 292
Yes, you are right, this is not the best for disk space and performance, but recalculation of fact tables is much easier and often not necessary if there are some errors in loading dimensions .

If a fact table contains the natural key to the dimension, this fact table will never require recalculation if dimension has been bad loaded.
I just replace the natural key ( varchar) with an int .
I ensure that this hybrid approach can work fine .
By the way, my article was also written to show that, by using indexed views, is possible to implement sofisticated data integrity checks at DBMS level

h_d_t (1/5/2012)
you now have a composite PK, which isn't ideal, and joining to facts will always require 2 fields in the join clause.

the PK is now bigint + datetime, which is 8+8 bytes, double the amount of space than just a PK on the bigint alone.

you are building 'intelligence' into your PK - this should never be. your PK should be 'dumb', with no business value / logic.

i suggest you drop this SK field, use your PK as autoincrement and make it the SK, which joins to your FK in your fact table.
Post #1232258
Posted Thursday, January 19, 2012 6:00 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Wednesday, September 24, 2014 3:19 AM
Points: 33, Visits: 292
By the way, this dimension is Type 6 ..
http://en.wikipedia.org/wiki/Slowly_changing_dimension#Pure_Type_6_Implementation
Post #1238584
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse