Solved

Slowly Changing Dimension (Type 3) T-SQL Example

Posted on 2004-08-02
9
1,126 Views
Last Modified: 2012-06-22
Is there a best way to write a T-SQL (SQL Server 2000) so that I can compare the current records with the old records so to perform slowly changing dimension Type3. I am now using a cursor in T-SQL, however it takes ages to fetch exach records and compare the old records. Other suggestion are welcome, e.g. DTS ... etc.
0
Comment
Question by:acwng
  • 2
  • 2
  • 2
  • +1
9 Comments
 
LVL 15

Expert Comment

by:jdlambert1
ID: 11700176
I've had cursors speed up by orders of magnitude just by changing READ_ONLY to FAST_FORWARD:

DECLARE Cur1 CURSOR FAST_FORWARD FOR
 <select_statement, , SELECT au_fname FROM pubs.dbo.authors>


0
 
LVL 34

Expert Comment

by:arbert
ID: 11700420
Post what you're doing--it sounds like something that could be accomodated with a set operation or possibly a join...
0
 
LVL 1

Author Comment

by:acwng
ID: 11700499
Let me post the actual situation:

Current_table - all the current records imported from Source Database

Dimension_table - a history of all records from the Current_table

I declare a cursor to fetch the records in Current_table 1 by 1 and then compare the value of each of the field related to the Dimension_table, if found any record changes between the 2 tables, I will do insert or update in Dimension_table.
0
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
LVL 9

Expert Comment

by:xenon_je
ID: 11700618
In general cursors are slow, so try to avoid if possible.
In your case to me it sounds as if those statements can be replaced by 2 SQL-statements, an update and an insert

Eg:

update Dimension_table
set ....
from Dimension_table join Current_table on ..
where ... (your condition for which records to update)


select ....into  Dimension_table
from Current_table join Dimension_Table
where ...

Also to not mix up the updated with inserted you could create 2 temp tables in which to put exactly those records from curent table that need to be updated and inserted
EG:
select * into temp_ToInsert from Current_table join Dimension_table on.. where ...
select * into temp_ToUpdate from Current_table join Dimension_table on.. where ...

--now make the update and insert using the Dimension_table and the 2 temp tables created above...

I hope this will give you some ideas...Because the problem was described prety vaguely, the answer could not be more specific...

good luck,
 xenon
0
 
LVL 9

Accepted Solution

by:
xenon_je earned 120 total points
ID: 11700625
PS:
the 2 statements
select * into temp_ToInsert from Current_table join Dimension_table on.. where ...
select * into temp_ToUpdate from Current_table join Dimension_table on.. where ...


replace them with:

select * into temp_ToInsert from Current_table where --a condition like field1 not in (select fildX from Dimension_table)
select * into temp_ToUpdate from Current_table  where --a condition like field1 not in (select fildX from Dimension_table)

xenon
0
 
LVL 34

Expert Comment

by:arbert
ID: 11700757
Agree with xenon_je and actually, on the update portion, you might find that it's even faster to do a delete and reinsert the entire record--just depends on your data...
0
 
LVL 1

Author Comment

by:acwng
ID: 11711861
Thanks all for the support, I'll try out the performance and will let you know the result soon.
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I wrote this interesting script that really help me find jobs or procedures when working in a huge environment. I could I have written it as a Procedure but then I would have to have it on each machine or have a link to a server-related search that …
Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

823 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question