Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Enforcing surrogate key uniqueness in slwoly changing dimension


Enforcing surrogate key uniqueness in slwoly changing dimension

Author
Message
Federico Iori
Federico Iori
Valued Member
Valued Member (57 reputation)Valued Member (57 reputation)Valued Member (57 reputation)Valued Member (57 reputation)Valued Member (57 reputation)Valued Member (57 reputation)Valued Member (57 reputation)Valued Member (57 reputation)

Group: General Forum Members
Points: 57 Visits: 300
Comments posted to this topic are about the item Enforcing surrogate key uniqueness in slwoly changing dimension
Alexander Suprun
Alexander Suprun
Old Hand
Old Hand (341 reputation)Old Hand (341 reputation)Old Hand (341 reputation)Old Hand (341 reputation)Old Hand (341 reputation)Old Hand (341 reputation)Old Hand (341 reputation)Old Hand (341 reputation)

Group: General Forum Members
Points: 341 Visits: 1516
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
Federico Iori
Federico Iori
Valued Member
Valued Member (57 reputation)Valued Member (57 reputation)Valued Member (57 reputation)Valued Member (57 reputation)Valued Member (57 reputation)Valued Member (57 reputation)Valued Member (57 reputation)Valued Member (57 reputation)

Group: General Forum Members
Points: 57 Visits: 300
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
Alexander Suprun
Alexander Suprun
Old Hand
Old Hand (341 reputation)Old Hand (341 reputation)Old Hand (341 reputation)Old Hand (341 reputation)Old Hand (341 reputation)Old Hand (341 reputation)Old Hand (341 reputation)Old Hand (341 reputation)

Group: General Forum Members
Points: 341 Visits: 1516
fediori,
This is interesting...
But what are the benefits of this approach comparing to the one when surrogate key is IDENTITY?


Alex Suprun
Federico Iori
Federico Iori
Valued Member
Valued Member (57 reputation)Valued Member (57 reputation)Valued Member (57 reputation)Valued Member (57 reputation)Valued Member (57 reputation)Valued Member (57 reputation)Valued Member (57 reputation)Valued Member (57 reputation)

Group: General Forum Members
Points: 57 Visits: 300
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
h_d_t
h_d_t
Forum Newbie
Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)

Group: General Forum Members
Points: 8 Visits: 821
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.
Federico Iori
Federico Iori
Valued Member
Valued Member (57 reputation)Valued Member (57 reputation)Valued Member (57 reputation)Valued Member (57 reputation)Valued Member (57 reputation)Valued Member (57 reputation)Valued Member (57 reputation)Valued Member (57 reputation)

Group: General Forum Members
Points: 57 Visits: 300
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.

Federico Iori
Federico Iori
Valued Member
Valued Member (57 reputation)Valued Member (57 reputation)Valued Member (57 reputation)Valued Member (57 reputation)Valued Member (57 reputation)Valued Member (57 reputation)Valued Member (57 reputation)Valued Member (57 reputation)

Group: General Forum Members
Points: 57 Visits: 300
By the way, this dimension is Type 6 ..
http://en.wikipedia.org/wiki/Slowly_changing_dimension#Pure_Type_6_Implementation
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search