Thursday 24 January 2013

Multiload UPSERT in Teradata with Example

UPSERT is an operation in DBMS, where if the new Delta contains updates for existing rows, they are Updated first. The remaining new rows in the Delta file will be inserted.

Multiload is well-equipped to handle such requests.


The following example illustrates how you can use Multiload to perform an UPSERT operation:


Note: This is different from the SQL UPSERT (UPDATE ... ELSE INSERT...)


Sample script:


.LOGTABLE WORK_TBLS.USER_INTERACT_ID_LOG;
.LOGON DWTEST1/USER1,PWD1;
.BEGIN IMPORT MLOAD
TABLES DW_TBLS.USER_INTERACT_ID
WORKTABLES WORK_TBLS.USER_INTERACT_ID_WK
ERRORTABLES WORK_TBLS.USER_INTERACT_ID_ET
WORK_TBLS.USER_INTERACT_ID_UV;

.LAYOUT DATAIN_LAYOUT;
.FIELD INTERACT_DESC 1 VARCHAR(30);
.FIELD INTERACT_TYP * varchar(10);

.DML LABEL UPDATE_DML
DO INSERT FOR MISSING UPDATE ROWS;
UPDATE DW_TBLS.USER_INTERACT_ID
SET
INTERACT_DESC = :INTERACT_DESC
WHERE
INTERACT_TYP = :INTERACT_TYP
;
INSERT INTO DW_TBLS.USER_INTERACT_ID
(
INTERACT_TYP
,INTERACT_DESC
)
values(
:INTERACT_TYP
,:INTERACT_DESC
);
.IMPORT INFILE /tmp/InteractionTypes.csv.tmp1
FORMAT vartext ','
LAYOUT DATAIN_LAYOUT
APPLY UPDATE_DML
;
.END MLOAD;
.LOGOFF;

DO INSERT FOR MISSING UPDATE ROWS - This keyword gives a HINT to multiload that an UPSERT is to be performed.
Otherwise, all rows will qualify for both UPDATE and INSERT, and the UPDATE rows go to _UV table.

Keep Reading to understand how to run a multiload, restart etc.!!

Subscribe with us if you like our Blog. Help us understand what topics should we cover.


5 comments:

  1. Thanks Rajesh. Please let us know if there are specific topics that we can cover in our upcoming posts.

    ReplyDelete
  2. Thanks. Please post different ways of creating surrogate key and best solution among them. This topic is not clear till now.

    ReplyDelete
    Replies
    1. Hi Rajesh, for Teradata you can explore the use of ROW_NUMBER() function to generate surrogate keys.

      Example:
      select row_number() over (order by tab1.custname) +
      (select coalesce(max(tab2.id),0) from tbl_customer as tab2)
      from tbl_customer as tab1 ;

      Avoid using CSUM(1,1) as Teradata generates a very skewed explain plan.

      If you are exploring Mysql, you can use the AUTO_INCREMENT feature of MySql tables to auto generate surrogate keys.

      Delete
  3. This comment has been removed by a blog administrator.

    ReplyDelete

Please share your thoughts and let us know the topics you want covered