December 9, 2011 at 3:30 pm
Comments posted to this topic are about the item Enforcing surrogate key uniqueness in slwoly changing dimension
January 3, 2012 at 8:49 pm
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
January 4, 2012 at 2:43 am
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
January 4, 2012 at 9:59 am
fediori,
This is interesting...
But what are the benefits of this approach comparing to the one when surrogate key is IDENTITY?
January 5, 2012 at 12:39 am
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
January 5, 2012 at 7:55 pm
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.
January 9, 2012 at 2:24 am
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.
January 19, 2012 at 6:00 am
By the way, this dimension is Type 6 ..
http://en.wikipedia.org/wiki/Slowly_changing_dimension#Pure_Type_6_Implementation
Viewing 8 posts - 1 through 7 (of 7 total)
You must be logged in to reply to this topic. Login to reply