In this article we will talk about the Merge operation in HP Vertica.
to speedup a bit the insert and update operations we can make use of the MERGE operation.
MERGE statement combines insert and update operations into a single operation.
MERGE [/*+ direct */]
... INTO [[db-name.]schema.]target-table [ alias ]
... USING [[db-name.]schema.]source-table [ alias ]
... ON ( condition )
... [ WHEN MATCHED THEN UPDATE SET
column1 = value1 [, column2 = value2 ... ] ]
... [ WHEN NOT MATCHED THEN INSERT ( column1 [, column2 ...])
VALUES ( value1 [, value2 ... ] ) ]
Examples of merge operations
create table target_tbl (id int, name varchar(10));
insert into target_tbl values(1,'Adi');
insert into target_tbl values(2,'Adi');
insert into target_tbl values(3,'Adi');
commit;
create table source_tbl (id2 int , name2 varchar(10));
insert into source_tbl values(4,'Ju');
insert into source_tbl values(5,'Ju');
insert into source_tbl values(6,'Ju');
commit;
MERGE INTO target_tbl
USING source_tbl b
ON id = b.id2
WHEN MATCHED THEN
UPDATE SET id = b.id2, name = b.name2
WHEN NOT MATCHED THEN
INSERT (id, name) VALUES (b.id2, b.name2);
(dbadmin@:5433) [dbadmin] select * from target_tbl ;
id | name
----+------
1 | Adi
2 | Adi
3 | Adi
(dbadmin@:5433) [dbadmin] MERGE INTO target_tbl
USING source_tbl b
ON id = b.id2
WHEN MATCHED THEN
UPDATE SET id = b.id2, name = b.name2
WHEN NOT MATCHED THEN
INSERT (id, name) VALUES (b.id2, b.name2);
OUTPUT
--------
3
(1 row)
(dbadmin@:5433) [dbadmin] * select * from target_tbl;
id | name
----+------
1 | Adi
2 | Adi
3 | Adi
4 | Ju
5 | Ju
6 | Ju