If you need to perform the INSERT
, UPDATE
, and DELETE
operations on a transactional table or a Delta table, you can encapsulate the operations in one MERGE INTO
statement. Then, you can execute the MERGE INTO statement to perform these operations on the table based on the join condition with a source table. This way, all data in the table is scanned only once and execution efficiency is improved.
Prerequisites
To execute the MERGE INTO
operation, you must have the Select and Update permissions on the destination transactional table. For more information about how to grant permissions, see MaxCompute permissions.
Feature description
MaxCompute allows you to perform the DELETE
or UPDATE
operation on a table to delete data from or update data in the table. If you want to perform INSERT
, UPDATE
, and DELETE
operations on the table at the same time, you must write and execute a statement for each operation. In this case, full table scan operations are performed multiple times. To improve execution efficiency, MaxCompute allows you to execute a MERGE INTO
statement to perform INSERT, UPDATE, and DELETE operations on a table at the same time. This way, full table scan operations are performed only once. This method is more efficient than the method in which you separately perform the INSERT
, UPDATE
, and DELETE
operations.
The MERGE INTO
statement ensures the atomicity of operations. A job is successful only after all the INSERT
, UPDATE
, and DELETE
operations in the job are successful. If an operation fails, the job also fails.
If you separately perform the INSERT
, UPDATE
, and DELETE
operations, specific operations may fail. The data on which successful operations are performed cannot be restored. To prevent this issue, you can execute the MERGE INTO
statement.
Limits
You cannot perform multiple INSERT
or UPDATE
operations on the same rows in a table by using a single MERGE INTO
statement.
Syntax
MERGE INTO <target_table> AS <alias_name_t> USING <source expression|table_name> AS <alias_name_s>
-- The ON clause specifies the JOIN conditions of the source table and the destination table.
ON <BOOLEAN expression1>
-- The WHEN MATCHED...THEN clause specifies the operation to be performed when the result of the ON clause is True. The operations of multiple WHEN MATCHED...THEN clauses cannot be performed on the same data.
WHEN matched [AND <BOOLEAN expression2>] THEN UPDATE SET <set_clause_list>
WHEN matched [AND <BOOLEAN expression3>] THEN DELETE
-- The WHEN MATCHED...THEN clause specifies the operation to be performed when the result of the ON clause is False.
WHEN NOT matched [AND <BOOLEAN expression4>] THEN INSERT VALUES <value_list>
Parameters
Parameter | Required | Description |
target_table | Yes | The name of the destination table, which must be an existing table. |
alias_name_t | No | The alias of the destination table. |
source expression|table_name | Yes | The name of the source table, view, or subquery that you want to join with the destination table. |
alias_name_s | No | The alias of the source table, view, or subquery that you want to join with the destination table. |
BOOLEAN expression1 | Yes | A condition that returns a value of the BOOLEAN type. The value must be True or False. |
BOOLEAN expression2, BOOLEAN expression3, BOOLEAN expression4 | No | You can specify a condition for each of the
|
set_clause_list | No | If you want to perform the |
value_list | No | If you want to perform the |
Examples
Example 1: Create a destination table named acid_address_book_base1 and a source table named tmp_table1, and insert data into the tables. Execute the
MERGE INTO
statement on the destination table. The data entries in the source table that meet the specifiedON
join condition are used to update the joined data entries in the destination table. The data entries that do not meet the specifiedON
join condition and whose value of the event_type column is I are inserted into the destination table. Sample statements:-- Create a destination table named acid_address_book_base1. CREATE TABLE IF NOT EXISTS acid_address_book_base1 (id BIGINT,first_name STRING,last_name STRING,phone STRING) PARTITIONED BY(year STRING, month STRING, day STRING, hour STRING) tblproperties ("transactional"="true"); -- Create a source table named tmp_table1. CREATE TABLE IF NOT EXISTS tmp_table1 (id BIGINT, first_name STRING, last_name STRING, phone STRING, _event_type_ STRING); -- Insert test data into the acid_address_book_base1 table. INSERT OVERWRITE TABLE acid_address_book_base1 PARTITION(year='2020', month='08', day='20', hour='16') VALUES (4, 'nihaho', 'li', '222'), (5, 'tahao', 'ha', '333'), (7, 'djh', 'hahh', '555'); -- Query data from the destination table and check whether the INSERT operation is successfully performed. SET odps.sql.allow.fullscan=true; SELECT * FROM acid_address_book_base1; -- Return results +------------+------------+------------+------------+------------+------------+------------+------------+ | id | first_name | last_name | phone | year | month | day | hour | +------------+------------+------------+------------+------------+------------+------------+------------+ | 4 | nihaho | li | 222 | 2020 | 08 | 20 | 16 | | 5 | tahao | ha | 333 | 2020 | 08 | 20 | 16 | | 7 | djh | hahh | 555 | 2020 | 08 | 20 | 16 | +------------+------------+------------+------------+------------+------------+------------+------------+ -- Insert test data into the tmp_table1 table. INSERT OVERWRITE TABLE tmp_table1 VALUES (1, 'hh', 'liu', '999', 'I'), (2, 'cc', 'zhang', '888', 'I'), (3, 'cy', 'zhang', '666', 'I'),(4, 'hh', 'liu', '999', 'U'), (5, 'cc', 'zhang', '888', 'U'),(6, 'cy', 'zhang', '666', 'U'); -- Query data from the source table and check whether the INSERT operation is successfully performed. SET odps.sql.allow.fullscan=true; SELECT * FROM tmp_table1; -- Return results +------------+------------+------------+------------+--------------+ | id | first_name | last_name | phone | _event_type_ | +------------+------------+------------+------------+--------------+ | 1 | hh | liu | 999 | I | | 2 | cc | zhang | 888 | I | | 3 | cy | zhang | 666 | I | | 4 | hh | liu | 999 | U | | 5 | cc | zhang | 888 | U | | 6 | cy | zhang | 666 | U | +------------+------------+------------+------------+--------------+ -- Execute the MERGE INTO statement. MERGE INTO acid_address_book_base1 AS t USING tmp_table1 as s ON s.id = t.id AND t.year='2020' AND t.month='08' AND t.day='20' AND t.hour='16' WHEN MATCHED THEN UPDATE SET t.first_name = s.first_name, t.last_name = s.last_name, t.phone = s.phone WHEN NOT MATCHED AND (s._event_type_='I') THEN INSERT VALUES(s.id, s.first_name, s.last_name,s.phone,'2020','08','20','16'); -- Query data from the destination table and check whether the MERGE INTO operation is successfully performed. SET odps.sql.allow.fullscan=true; SELECT * FROM acid_address_book_base1; -- Return results +------------+------------+------------+------------+------------+------------+------------+------------+ | id | first_name | last_name | phone | year | month | day | hour | +------------+------------+------------+------------+------------+------------+------------+------------+ | 4 | hh | liu | 999 | 2020 | 08 | 20 | 16 | | 5 | cc | zhang | 888 | 2020 | 08 | 20 | 16 | | 7 | djh | hahh | 555 | 2020 | 08 | 20 | 16 | | 1 | hh | liu | 999 | 2020 | 08 | 20 | 16 | | 2 | cc | zhang | 888 | 2020 | 08 | 20 | 16 | | 3 | cy | zhang | 666 | 2020 | 08 | 20 | 16 | +------------+------------+------------+------------+------------+------------+------------+------------+
Example 2: Create a destination table named merge_acid_dp and a source table named merge_acid_source. Insert data into the tables. Then, execute the
MERGE INTO
statement without specifying a partition to update data in or insert data into the destination table. The MERGE INTO operation takes effect on all partitions.-- Create a destination table named merge_acid_dp. CREATE TABLE IF NOT EXISTS merge_acid_dp(c1 BIGINT NOT NULL, c2 BIGINT NOT NULL) PARTITIONED BY (dd STRING, hh STRING) tblproperties ("transactional" = "true"); -- Create a source table named merge_acid_source. CREATE TABLE IF NOT EXISTS merge_acid_source(c1 BIGINT NOT NULL, c2 BIGINT NOT NULL, c3 STRING, c4 STRING) lifecycle 30; -- Insert test data into the destination table merge_acid_dp. INSERT OVERWRITE TABLE merge_acid_dp PARTITION (dd='01', hh='01') VALUES (1, 1), (2, 2); INSERT OVERWRITE TABLE merge_acid_dp PARTITION (dd='02', hh='02') VALUES (4, 1), (3, 2); -- Query data from the destination table and check whether the INSERT operation is successfully performed. SET odps.sql.allow.fullscan=true; SELECT * FROM merge_acid_dp; -- Return results +------------+------------+----+----+ | c1 | c2 | dd | hh | +------------+------------+----+----+ | 1 | 1 | 01 | 01 | | 2 | 2 | 01 | 01 | | 4 | 1 | 02 | 02 | | 3 | 2 | 02 | 02 | +------------+------------+----+----+ -- Insert test data into the source table merge_acid_source. INSERT OVERWRITE TABLE merge_acid_source VALUES(8, 2, '03', '03'), (5, 5, '05', '05'), (6, 6, '02', '02'); -- Query data from the source table to check whether the INSERT operation is successfully performed. SET odps.sql.allow.fullscan=true; SELECT * FROM merge_acid_source; -- Return results +------------+------------+----+----+ | c1 | c2 | c3 | c4 | +------------+------------+----+----+ | 8 | 2 | 03 | 03 | | 5 | 5 | 05 | 05 | | 6 | 6 | 02 | 02 | +------------+------------+----+----+ -- Execute the MERGE INTO statement. SET odps.sql.allow.fullscan=true; MERGE INTO merge_acid_dp tar USING merge_acid_source src ON tar.c2 = src.c2 WHEN MATCHED THEN UPDATE SET tar.c1 = src.c1 WHEN NOT MATCHED THEN INSERT VALUES(src.c1, src.c2, src.c3, src.c4); -- Query data from the destination table to check whether the MERGE INTO operation is successfully performed. SET odps.sql.allow.fullscan=true; SELECT * FROM merge_acid_dp; -- Return results +------------+------------+----+----+ | c1 | c2 | dd | hh | +------------+------------+----+----+ | 6 | 6 | 02 | 02 | | 5 | 5 | 05 | 05 | | 8 | 2 | 02 | 02 | | 8 | 2 | 01 | 01 | | 1 | 1 | 01 | 01 | | 4 | 1 | 02 | 02 | +------------+------------+----+----+
Example 3: Create a destination table named merge_acid_sp and a source table named merge_acid_source. Insert data into the tables. Execute the
MERGE INTO
statement to update data in or insert data into a specified partition of the destination table.-- Create a destination table named merge_acid_sp. CREATE TABLE IF NOT EXISTS merge_acid_sp(c1 BIGINT NOT NULL, c2 BIGINT NOT NULL) PARTITIONED BY (dd STRING, hh STRING) tblproperties ("transactional" = "true"); -- Create a source table named merge_acid_source. CREATE TABLE IF NOT EXISTS merge_acid_source(c1 BIGINT NOT NULL, c2 BIGINT NOT NULL, c3 STRING, c4 STRING) lifecycle 30; -- Insert test data into the destination table merge_acid_sp. INSERT OVERWRITE TABLE merge_acid_sp PARTITION (dd='01', hh='01') VALUES (1, 1), (2, 2); INSERT OVERWRITE TABLE merge_acid_sp PARTITION (dd='02', hh='02') VALUES (4, 1), (3, 2); -- Query data from the destination table and check whether the INSERT operation is successfully performed. SET odps.sql.allow.fullscan=true; SELECT * FROM merge_acid_sp; -- Return results +------------+------------+----+----+ | c1 | c2 | dd | hh | +------------+------------+----+----+ | 1 | 1 | 01 | 01 | | 2 | 2 | 01 | 01 | | 4 | 1 | 02 | 02 | | 3 | 2 | 02 | 02 | +------------+------------+----+----+ -- Insert test data into the source table merge_acid_source. INSERT OVERWRITE TABLE merge_acid_source VALUES(8, 2, '03', '03'), (5, 5, '05', '05'), (6, 6, '02', '02'); -- Query data from the source table to check whether the INSERT operation is successfully performed. SET odps.sql.allow.fullscan=true; SELECT * FROM merge_acid_source; -- Return results +------------+------------+----+----+ | c1 | c2 | c3 | c4 | +------------+------------+----+----+ | 8 | 2 | 03 | 03 | | 5 | 5 | 05 | 05 | | 6 | 6 | 02 | 02 | +------------+------------+----+----+ -- Specify the dd = '01' and hh = '01' partitions in the ON clause of the MERGE INTO statement and execute the statement to update or insert data in the specified partitions of the destination table. SET odps.sql.allow.fullscan=true; MERGE INTO merge_acid_sp tar USING merge_acid_source src ON tar.c2 = src.c2 AND tar.dd = '01' AND tar.hh = '01' WHEN MATCHED THEN UPDATE SET tar.c1 = src.c1 WHEN NOT MATCHED THEN INSERT VALUES(src.c1, src.c2, src.c3, src.c4); -- Query data from the destination table to check whether the MERGE INTO operation is successfully performed. SET odps.sql.allow.fullscan=true; SELECT * FROM merge_acid_sp; +------------+------------+----+----+ | c1 | c2 | dd | hh | +------------+------------+----+----+ | 5 | 5 | 05 | 05 | | 6 | 6 | 02 | 02 | | 8 | 2 | 01 | 01 | | 1 | 1 | 01 | 01 | | 4 | 1 | 02 | 02 | | 3 | 2 | 02 | 02 | +------------+------------+----+----+
Example 4: Create a Delta table named mf_tt6 as the destination table and create a source table named mf_delta. Insert data into the source table and destination table. Specify partitions in the
MERGE INTO
statement and execute the statement to perform the UPDATE, INSERT, or DELETE operation on the specified partitions of the destination table.-- Create a Delta table named mf_tt6 as the destination table. CREATE TABLE IF NOT EXISTS mf_tt6 (pk BIGINT NOT NULL PRIMARY key, val BIGINT NOT NULL) PARTITIONED BY (dd STRING, hh STRING) tblproperties ("transactional"="true"); -- Insert test data into the destination table mf_tt6. INSERT OVERWRITE TABLE mf_tt6 PARTITION (dd='01', hh='02') VALUES (1, 1), (2, 2), (3, 3); INSERT OVERWRITE TABLE mf_tt6 PARTITION (dd='01', hh='01') VALUES (1, 10), (2, 20), (3, 30); -- Enable a full table scan only for the current session. Execute the SELECT statement to query data from the mf_tt6 table. SET odps.sql.allow.fullscan=true; SELECT * FROM mf_tt6; -- Return results +------------+------------+----+----+ | pk | val | dd | hh | +------------+------------+----+----+ | 1 | 10 | 01 | 01 | | 3 | 30 | 01 | 01 | | 2 | 20 | 01 | 01 | | 1 | 1 | 01 | 02 | | 3 | 3 | 01 | 02 | | 2 | 2 | 01 | 02 | +------------+------------+----+----+ -- Create a source table named mf_delta and insert test data into the table. CREATE TABLE IF NOT EXISTS mf_delta AS SELECT pk, val FROM VALUES (1, 10), (2, 20), (6, 60) t (pk, val); -- Query data from the source table and confirm the result of the INSERT operation. SELECT * FROM mf_delta; -- Return results +------+------+ | pk | val | +------+------+ | 1 | 10 | | 2 | 20 | | 6 | 60 | +------+------+ -- Specify the dd = '01' and hh = '02' partitions in the ON clause of the MERGE INTO statement and execute the statement to perform the UPDATE, INSERT, or DELETE operation on the specified partitions in the destination table mf_tt6. MERGE INTO mf_tt6 USING mf_delta ON mf_tt6.pk = mf_delta.pk AND mf_tt6.dd='01' AND mf_tt6.hh='02' WHEN MATCHED AND (mf_tt6.pk > 1) THEN UPDATE SET mf_tt6.val = mf_delta.val WHEN MATCHED THEN DELETE WHEN NOT MATCHED THEN INSERT VALUES (mf_delta.pk, mf_delta.val, '01', '02'); -- Query data from the destination table and check whether the MERGE INTO operation is successfully performed. SET odps.sql.allow.fullscan=true; SELECT * FROM mf_tt6; -- Return results +------------+------------+----+----+ | pk | val | dd | hh | +------------+------------+----+----+ | 1 | 10 | 01 | 01 | | 3 | 30 | 01 | 01 | | 2 | 20 | 01 | 01 | | 3 | 3 | 01 | 02 | | 6 | 60 | 01 | 02 | | 2 | 20 | 01 | 02 | +------------+------------+----+----+