SSIS Design Pattern - Incremental Loads: Andy Leonard
SSIS Design Pattern - Incremental Loads: Andy Leonard
is Blog
Andy Leonard
Home
About
SSIS and ETL
Thoughts about Database and Software Development, and the tools of the trade. Email
Links
News
Follow me on
Once the project loads, open Solution Explorer and rename Package1.dtsx to SSISIncrementalLoad.dtsx
Archives
When prompted to rename the package object, click the Yes button. From theOctober
toolbox, drag a Data Flow
Control Flow canvas: 2010 (3)
Septembe
r 2010
(12)
August
2010 (14)
July 2010
(18)
June
2010 (8)
May
2010 (12)
April
2010 (3)
March
2010 (16)
February
2010 (20)
January
2010 (22)
Decembe
r 2009
(14)
Novembe
r 2009 (5)
October
2009 (4)
Septembe
r 2009 (7)
August
2009 (4)
July 2009
(5)
June
2009 (6)
May
2009 (1)
April
2009 (2)
March
2009 (5)
February
2009 (5)
January
2009 (3)
Decembe
r 2008 (3)
Novembe
r 2008 (4)
October
2008 (3)
Septembe
r 2008 (3)
August
2008 (4)
July 2008
(9)
June
2008 (7)
May
2008 (12)
April
2008 (22)
March
2008 (10)
February
2008 (8)
January
2008 (3)
Decembe
r 2007
(12)
Novembe
r 2007
Double-click the Data Flow task to edit it. From the toolbox, drag and drop an(13)
OLE DB Source onto the
canvas: October
2007 (3)
Septembe
r 2007 (3)
August
2007 (5)
July 2007
(7)
Drag and drop a Lookup Transformation from the toolbox onto the Data Flow canvas. Connect the
adapter to the Lookup transformation by clicking on the OLE DB Source and dragging the green arrow o
and dropping it. Right-click the Lookup transformation and click Edit (or double-click the Lookup transfo
When the editor opens, click the New button beside the OLE DB Connection Manager dropdown (as you
the OLE DB Source Adapter). Define a new Data Connection - this time to the SSISIncrementalLoad_De
After setting up the new Data Connection and Connection Manager, configure the Lookup transformatio
"dbo.tblDest":
Click the Columns tab. On the left side are the columns currently in the SSIS data flow pipeline (from
SSISIncrementalLoad_Source.dbo.tblSource). On the right side are columns available
just configured (from SSISIncrementalLoad_Dest.dbo.tblDest). Follow the following steps:
1. We'll need all the rows returned from the destination table, so check all the checkboxes beside the ro
destination. We need these rows for our WHERE clauses and for our JOIN ON clauses.
2. We do not want to map all the rows between the source and destination - we only want to map the c
ColID between the database tables. The Mappings drawn between the Available Input Columns and Ava
Columns define the JOIN ON clause. Multi-select the Mappings between ColA, ColB, and ColC by clicking
holding the Ctrl key. Right-click any of them and click "Delete Selected Mappings" to delete these colum
JOIN ON clause.
3. Add the text "Dest_" to each column's Output Alias. These rows are being appended to the data flow
so we can distinguish between Source and Destination rows farther down the pipeline:
Next we need to modify our Lookup transformation behavior. By default, the Lookup operates as an INN
we need a LEFT (OUTER) JOIN. Click the "Configure Error Output" button
On the "Lookup Output" row, change the Error column from "Fail component" to "Ignore failure". This te
transformation "If you don't find an INNER JOIN match in the destination table for the Source table's Co
fail." - which also effectively tells the Lookup "Don't act like an INNER JOIN, behave like a LEFT JOIN":
Click OK to complete the Lookup transformation configuration.
From the toolbox, drag and drop a Conditional Split Transformation onto the Data Flow canvas. Connec
the Conditional Split as shown. Right-click the Conditional Split and click Edit to open the Conditional Sp
Expand the NULL Functions folder in the upper right of the Conditional Split Transformation Editor. Expa
folder in the upper left side of the Conditional Split Transformation Editor. Click in the "Output Name" c
"New Rows" as the name of the first output. From the NULL Functions folder, drag and drop the
"ISNULL( <<expression>> )" function to the Condition column of the New Rows condition:
Next, drag Dest_ColID from the columns folder and drop it onto the "<<expression>>" text in the Cond
"New Rows" should now be defined by the condition "ISNULL( [Dest_ColID] )". This
rows - setting it to "WHERE Dest_ColID Is NULL".
Type "Changed Rows" into a second Output Name column. Add the expression "(ColA != Dest_ColA) ||
Dest_ColB) || (ColC != Dest_ColC)" to the Condition column for the Changed Rows output. This defines
clause for detecting changed rows - setting it to "WHERE ((Dest_ColA != ColA) OR (Dest_ColB != ColB)
= ColC))". Note "||" is used to convey "OR" in SSIS Expressions:
Change the "Default output name" from "Conditional Split Default Output" to "Unchanged Rows":
Click the OK button to complete configuration of the Conditional Split transformation.
Drag and drop an OLE DB Destination connection adapter and an OLE DB Command transformation ont
canvas. Click on the Conditional Split and connect it to the OLE DB Destination. A dialog will display pro
select a Conditional Split Output (those outputs you defined in the last step). Select the New Rows outp
Next connect the OLE DB Command transformation to the Conditional Split's "Changed Rows" output:
Click the OK button to complete configuring the OLE DB Destination connection adapter.
Double-click the OLE DB Command to open the "Advanced Editor for OLE DB Command" dialog. Set the
Manager column to your SSISIncrementalLoad_Dest connection manager:
Click on the "Component Properties" tab. Click the elipsis (button with "...")
The String Value Editor displays. Enter the following parameterized T-SQL statement into the String Va
UPDATE dbo.tblDest
SET
ColA = ?
,ColB = ?
,ColC = ?
WHERE ColID = ?
The question marks in the previous parameterized T-SQL statement map by ordinal to columns named
through "Param_3". Map them as shown below - effectively altering the UPDATE statement
UPDATE SSISIncrementalLoad_Dest.dbo.tblDest
SET
ColA = SSISIncrementalLoad_Source.dbo.ColA
,ColB = SSISIncrementalLoad_Source.dbo.ColB
,ColC = SSISIncrementalLoad_Source.dbo.ColC
WHERE ColID = SSISIncrementalLoad_Source.dbo.ColID
Your Data Flow canvas should look like that pictured below:
If you execute the package with debugging (press F5), the package should succeed and appear as show
Note one row takes the "New Rows" output from the Conditional Split, and one row takes the "Changed
from the Conditional Split transformation. Although not visible, our third source row doesn't change, an
to the "Unchanged Rows" output - which is simply the default Conditional Split output renamed. Any ro
meet any of the predefined conditions in the Conditional Split is sent to the default output.
That's all! Congratulations - you've built an incremental database load! [:)]
:{> Andy
Published Monday, July 09, 2007 3:13 PM by andyleonard
Filed under: Design Pattern, Incremental, SSIS
Comment Notification
If you would like to receive an email when updates are made to this post, please register
here
Comments
It is an all-in-one and completely free SSIS component that handles these kind
of situations without the need to cache data in the Lookup. Lookups are nice
but - in real situaton - they may shortly lead to out of memory situations (think
at a hundred million rows table... it simply cannot be cached in memory).
Beware that - for huge table comparison - you will need both TableDifference
AND the FlowSync component that you can find at the same site.
Alberto
:{> Andy
Thank you greatly Andy. This couldn't have come at a better time as I just
started using Integration Services for the first time on Friday to handle eight
different data loads (all for a single client). Four of the data loads are straight
appends, but the other four are incremental.
I also created a script to assist with the creation of the Conditional Split
"Changed Rows" condition which follows (be sure your results aren't being
truncated when you have a table with many columns):
-- ((ISNULL(<ColumnName>)?"":<ColumnName>)!
=(ISNULL(Dest_<ColumnName>)?"":Dest_<ColumnName>)) ||
FROM sys.tables t
ON t.[object_id] = c.[object_id]
AND c.[is_identity] = 0
AND c.[is_rowguidcol] = 0
ORDER BY
c.[column_id]
SELECT @Filter
Again, thanks greatly. I now have 2 SSIS books on there way to me. I am
eager to learn as much as I can.
andyleonard said:
:{> Andy
Hi Andy !! Great work... I was scared because of this Incremental load... and
you saved my weekend... now I can enjoy it .... :-)
Anyone had a problem with the insert and update commands locking each other
out?
Didn't happen at first but does now. Update gets blocked by the insert and it
just hangs.
Steve
Thanks Saul!
Steve, are you sure there's not something more happening on the server that's
causing this?
If this is repeatable, please provide more information and I'll be happy to take a
look at it.
SQL Server does a fair job of detecting and managing deadlocks when they
occur. I haven't personally seen SQL Server "hang" since 1998 - and then it
was due to a failing I/O controller.
:{> Andy
Hi,Andy! I have a same problem with Steve,it is block. When bulk insert and
update happen,Update gets blocked by the insert and it just hangs!Insert's wait
type is ASYNC_NETWORK_IO.
Thx 4 the trick with Fail -> Left Join ! I was thinking how to do it whole day
:o)
Steve,
This most certainly can be the case with larger datasets. In my case, I ran into
this issue with large FACT table loads. Either consider dumping the contents
of the insert into a temp table or SSIS RAW datafile and complete the insert in
a separate dataflow task or modify the isolationlevel of the package. Be
warned, make sure you research the IsolationLevel property thoroughly before
making such a change.
November 26, 2007 12:03 PM
Michael said:
Hi Michael,
A good NULL trap can be tricky because NULL == NULL should never
evaluate to True. I know NULL == NULL can evaluate to True with certain
settings, but these settings also have side-effects. And then there's maintenance
to consider... basically, there's no free lunch.
But again, if ColA is ever -1 this will evaluate as a change and fire an update.
Why does this matter? Some systems include "number of updated rows" as a
validation metric.
:{> Andy
Hi Andy,
Do you have any hints for implementing your design with an Oracle Source. I
am attempting to incrementally update from a table with 7 million rows with
~50 fields. The Lookup Task failed when I attempted to use it like you
described above due to a Duplicate Key error...cache is full. I googled this and
found an article suggesting enabling restrictions and enabling smaller cache
amounts. However it is now extremely slow. Do you have any
experience/advice on tweaking the lookup task for my environment?
Is there a way to speed things up/replace the lookup task by using a SQL
Execution Task which calls a left outer join?
Thanks Again
Now that our 5-month old son - Riley Cooper - is on the mend , I am hitting the
speaking trail again!
Hi AndY looks great and work also great but if there are more records to
update than it just hangs while doing insert and update so what should i do ..is
there any workaround by which we can avoid hanging od SSIS pacage. Please
Suggest
Thanks
Jigu
Although I mention set-based updates here I did not demonstrate the principle
because I felt the post was already too long - my apologies.
:{> Andy
Hi Andy
Thanks you did great help to understand data update through SSIS
package
Hi Andy,
I have a hard time following your instructions. Can you send me your sample
project
Thank You
Kenneth
aspken@msn.com
Hi Kenneth,
One of the last instructions is a link at the bottom of the page called "Get the
code". It points to this URL:
http://vsteamsystemcentral.com/dnn/Demos/IncrementalLoads/tabid/94/Default
.aspx.
Andy
Not sure posted same question few places….May be you gurus can explain
In SSIS Fuzzy grouping objects creates some temp tables and does the Fuzzy
logic. I ran the trace to see how it does in one cursor it is taking very long time
to process 150000 records. Same executes fine in any other test environments.
The cursor is simple and I can post if needed. Any thoughts ?
I have a similar package I am trying to create and this was a big help. The new
rows write properly however I am getting an error on the changed rows because
the SQL table i am writing to has an auto incremented identity spec column.
The changes won't write to the SQL table. If I uncheck "keep identity" it writes
new rows instead of updating existing. What am I missing?
Rajesh said:
Hi Andy..
Welll done...
I don't see the code handling the deleted rows in source (asume that there is)
in your lookup, you can split out the match and non-match rows.
non match means new record and you can do an insert directly after the lookup.
you can elimninate the 'new row' in your condition in 'conditional split'
However, overall, your sample package is the best (at far as I have searched)
sample on the net ( I love it, honestly).
Ken
Hi Ken,
:{> Andy
Hi Andy,
Thanks a lot for this article. It proved to be a great help for me.
I was wondering if you can provide some solution to handle deleted rows from
source table using lookup. I need this because I have to keep the historical data
in the data warehouse.
Thanks in advance,
RVS
ranvijay.sahay@gmail.com
Andy, thanks for your help and effort. This is definitely more elegant than
staging over to one database and then doing ExecuteSQLs to execute
incremental loads.
And re ranvijay's question, I would assume that when the row exists in the
destination but not the source, the source RowID would show up as null, so you
could do that as another split on the conditional.
RVS, Charlie answered your question before I could get to it! I love this
community!
I need to write more on this very topic. New features in SQL Server 2008
change this and make the Deletes as simple as New and Updated rows.
I didn't mention Deletes in this post because the main focus was to get folks
thinking about leveraging the data flow instead of T-SQL-based solutions
(Charlie, in regards to your first comment). There's nothing wrong with T-SQL.
But a data flow is built to buffer (or "paginate") rows. It bites off small chunks,
acts on them, and then takes another bite. This greatly reduces the need to swap
to disk - and we all know the impact of disk I/O on SQL Server performance.
Typically, I stage Deletes and Updates in a staging table near the table to be
Deleted / Updated. Immediately after the data flow, I add an Execute SQL Task
to perform a correlated (inner joined) update or delete with the target table. I do
this because my simplest option inside a data flow is row-based Updates /
Deletes using the OLE DB Command transformation. A set-based Update /
Delete is a lot faster.
:{> Andy
Andy,
Looks like I have some rewriting to do on the next version of the ETL. It's a
good thing I enjoy working in SSIS!
Of course since its the government we'll probaby have to wait until 3Q 2010
before we're allowed to upgrade to SQL 2008. We were all gung ho about VS
2008 (which we were allowed to get) but imagine my chagrin when I found out
that I couldn't use my beloved BI Studio without SQL 2008... :P So I'll be
using this for the next version... and possibly the version after that as well.
Thanks a bunch!
Me again.
I think I made a mistake. If a row already exists in the destination table and it
no longer exists in the source table, I want it deleted (sent to the deletes staging
table). However, the lookup limits the row set in memory to items that are
already in the source table, so its not really functioning as an outer join. Its
perfect for determining inserts and updates, but I need to do something else to
do deletes...
I'm going to try adding an additional OLE DB source and point that at the same
table the lookup is checking... hmm, maybe try the Merge? I'll see what
happens and let you know.
Andy,
What I wound up doing was creating a second data flow after the one that split
the inserts and updates out. The deletes flow populated a deleted rows staging
table with the deleted row id, which then was joined to the ultimate destination
table in a delete command in an Execute SQL task. I would up reversing the
lookup, but used the same technique by using a conditional split on whether or
not the new column from the lookup was null, and if it was, the output went to
the "deleted records" path, which populated the staging table.
The reason I want to actually remove the data from the table as opposed to
merely marking it as deleted is because the reason a row would disappear
would be because it was a bad reference code in the first place. My big
datawarehouse ETL adds new reference codes to the reference tables (which it
needs to create in the first place because the source reference codes are held in
these five gigantic tables which do not lend themselves to generating NV lists)
for unmatched codes in the data tables (remember there's no validation at the
source).
When the reconciliation stick finally gets swung and the customer replaces the
junk code it disappears from my ETL and I remove it from my table. It is
different from a code that gets obsoleted; there's a reason to track those, but
garbage just needs to be thrown out.
Thanks again, I would have been very annoyed with myself if I wound up
doing row-based IUDs...
Hi Charles,
I wasn't clear in my earlier response but you figured it out anyway - apologies
and kudos. You do need to do the Delete in another Data Flow Task.
Excellent work!
:{> Andy
Andy,
Is there a limit to how many comparisons you can make in the Conditional Split
Transformation Editor? I have a table with 20 columns, and I'm trying to do 19
comparisons. It's telling me that one of the columns doesn't exist in the input
column collection. I can cut the expression and paste it back in and it picks a
different column to complain about. Error 0xC0010009... it says the expression
cannot be parsed, may contain invalid elements or might not be well formed,
and there may also be an out-of-memory error.
I've been looking at it for 1/2 an hour and all the columns it is variously
complaining about are present in the input column collection, so I suspect it's a
memory error. Should I alias the column names to be shorter (ie the problem is
in the text box) or is it a metadata problem? I'm going home now but tomorrow
I will see if splitting the staging table into 4 tables and splitting the conditions
into 4 outputs (to be recombined later by an execute SQL command into the
real staging table) does what I need.
Thanks!
Charlie
I thank you for your comments. I still have a few doubts related to handling
Deleted columns. I have created a solution to handle all three cases(add,update
and delete). I have taken two OLEDB Source(one with source and data and
another with destination table's data) then I have SORTED them and MERGED
them(with FULL OUTER join) and finally used CONDITIONAL SPLIT to
filter New, updated and Deleted data and used the OLEDB Command to do the
required action. I am getting Deleted rows by using full outer join.
I am getting expected result with this solution but I think this is not
performance efficient as it is using sort, merge etc. I wanted to use Lookup as
suggested by Andy. But the solution which you both have given is not fully
clear to me. Will it be possible for you to send me a sketch of the proposed
solution or explain it a bit in detail?
RVS
(ranvijay.sahay@gmail.com)
Actually what was happening was that since the comparison expression was so
long I moved it into WordPad to type it and then copy/pasted into the rather
annoyingly non-resizable condition field in the conditional split transformation
editor. It turns out it doesn't like that. Maybe there were invisible control
characters in the string, so I needed to just bite the bullet and type in the
textbox. It works fine now.
Thanks!
This was the excellent article and Andy illustration style is great.
Thank you
Great tutorial! I'm new to SSIS and I worked through it without a hitch.
Thanks!!!
Hellow,
It's nice to find a way to get your changed and new records in 2 separate
outputs. But how who you get the deleted records? The only solution i found is
to lookup every PK in the source db table and check if it still excists. If it does
it will set the deleted_flag to 1. Do you have any idea to implement the deleted
records into your solution? Mine is in a separete dataflow.
Greetings
Great article! I originally used sort, merge join (with left outer join) and
conditional split transforms to perform incremental load. Unfortunately it did
not work as expected. Your article has simplified my design and it is now
working perfectly. Thanks for sharing. :)
Dear Andy
your solution is great but i have problem. the dimensions are not getting
populated with the default data. does this work on the excel source because i
have an excel source.
Hiya,
I have used the "slowly changing dimension" element in the past to facilitate
the same outcome, ie not using type2s (despite being a fact) - but it is much
slower.
RVA, re: "I am getting expected result with this solution but I think this is not
performance efficient as it is using sort, merge etc"; if the sort(s) are the main
problem, you can do the sort on the database and tell SSIS that the set is sorted
to avoid using two sort dataflow tasks - not sure if that will give you sufficient
gains? The Merge join, as you say, will still be not great within SSIS.
Lastly - has anyone any experience of duplicated KEYS in the source table, that
do not (yet) exist in the destination?
Hi Andy,
I want to load data incrementally from source (MySQL 5.2) to SQL Server
2008, using SSIS 2008, based on modified date. Somehow I am not able do it
as MySQL doesn't support parameters. Need some help on this.
-regards, mandar
Thank you andy for this tutorial. I am using SSIS 2008, the Lookup task
interface has changed a little bit, when you click on edit on the lookup task, the
opening screen is layed out differently.
When I use SCD 2 type when it read record in target the id 3 record is not
avlable in target so it’s treat for insert for second record also same.
So that record insert two time I don’t want like that I want to first record insert
and scoend record of ID 3 Update.
ID Name Date
I need to incrementally load data from Sybase to SQL. There will be several
hundred million rows. Will this approach work OK with this scenario?
Hi Craig,
Maybe, but most likely not. This is one design pattern you can start with. I
would test this, tweak it, and optimize like crazy to get as much performance
out of your server as possible.
:{> Andy
Andy, i am a starter in SSIS and i found this article very useful and
straightforward in explanation with text and images...
Thanks a lot!!
Cheers
Hi Andy
I amended your script to deal with different datatypes (saves a lot of debugging
in the Conditional Split Transformation Editor):
/*
This script assists with the creation of the Conditional Split "Changed Rows"
condition
-- be sure your results aren't being truncated when you have a table with many
columns
*/
USE master
GO
FROM sys.tables t
ON t.[object_id] = c.[object_id]
AND c.[is_identity] = 0
AND c.[is_rowguidcol] = 0
ORDER BY
c.[column_id]
SELECT @Filter
--SELECT
-- c.*
--FROM
-- sys.tables t
--JOIN
-- sys.columns c
-- ON t.[object_id] = c.[object_id]
--WHERE
--AND c.[is_identity] = 0
--AND c.[is_rowguidcol] = 0
--ORDER BY
--c.[column_id]
--SELECT
-- schemas.name AS [Schema]
-- ,tables.name AS [Table]
-- ,columns.name AS [Column]
-- THEN 'byte[]'
-- WHEN columns.system_type_id = 35
-- THEN 'string'
-- WHEN columns.system_type_id = 36
-- THEN 'System.Guid'
-- WHEN columns.system_type_id = 48
-- THEN 'byte'
-- WHEN columns.system_type_id = 52
-- THEN 'short'
-- WHEN columns.system_type_id = 56
-- THEN 'int'
-- WHEN columns.system_type_id = 58
-- THEN 'System.DateTime'
-- WHEN columns.system_type_id = 59
-- THEN 'float'
-- WHEN columns.system_type_id = 60
-- THEN 'decimal'
-- WHEN columns.system_type_id = 61
-- THEN 'System.DateTime'
-- WHEN columns.system_type_id = 62
-- THEN 'double'
-- WHEN columns.system_type_id = 98
-- THEN 'object'
-- WHEN columns.system_type_id = 99
-- THEN 'string'
-- THEN 'bool'
-- THEN 'decimal'
-- THEN 'decimal'
-- THEN 'decimal'
-- THEN 'long'
-- THEN 'byte[]'
-- THEN 'string'
-- THEN 'byte[]'
-- THEN 'string'
-- THEN 'long'
-- THEN 'string'
-- THEN 'string'
-- THEN 'string'
-- END AS [Type]
-- ,columns.is_nullable AS [Nullable]
--FROM
-- sys.tables tables
--INNER JOIN
-- sys.schemas schemas
--INNER JOIN
-- sys.columns columns
--WHERE
--ORDER BY
-- [Schema]
-- ,[Table]
-- ,[Column]
-- ,[Type]
I would not use a join to detect change because in the where clause you need to
handle NULL values. For example if ColA in Source is NULL it doesn't matter
what ColA is in the destination, the where clause will return false and not
detect the change.
union
This returns a distinct set of rows, including handling NULL values. All that is
left is to determine if the ColId appears more than once in the set.
union
)x
group by ColId
Now I have a list of keys which changed. I can take this list and sort it to use in
a merge join in SSIS or I can use it as a subquery to join back to the Source
table. See below.
inner join (
)x
group by ColId
)y
on s.ColId = y.ColId
Thanks for the good explanation and screenshots. I found this website to be
extremly helpful and supportive.
Please let me know if I can learn something more from you and rest of the guys
visiting this website, so that we can become better in SSIS and SQL server
2005 or 2008.
Please provide us similar articles so that we can through them and practice.
Leave a Comment
Name (required)*
Comments (required)*
Remember Me?
Submit
About andyleonard
Andy Leonard is an Architect with Molina Medicaid Solutions, SQL Server database
and Integration Services developer, SQL Server MVP, PASS Regional Mentor
(Southeast US), and engineer. He is a co-author of Professional SQL Server 2005
Integration Services and SQL Server MVP Deep Dives.
©2006-2010 SQLblog.com TM