DELETE operators delete table rows that meet the specified criteria.
OceanBase Database supports the following types of DELETE operators: DELETE and DISTRIBUTED DELETE.
DELETE
The DELETE operator deletes data from a single partition of a table.
In the following example, query Q1 deletes all rows in table t1 that satisfy the condition c2>'100'.
-- In Oracle mode:
obclient> CREATE TABLE t1 (c1 INT PRIMARY KEY, c2 VARCHAR2(10));
Query OK, 0 rows affected
obclient> CREATE TABLE t2 (c1 INT PRIMARY KEY, c2 VARCHAR2(10)) PARTITION BY
HASH(c1) PARTITIONS 10;
Query OK, 0 rows affected
obclient> CREATE TABLE t3 (c1 INT PRIMARY KEY, c2 VARCHAR2(10));
Query OK, 0 rows affected
obclient> CREATE INDEX IDX_t3_c2 ON t3 (c2) PARTITION BY HASH(c2) PARTITIONS 3;
Query OK, 0 rows affected
-- In MySQL mode:
obclient> CREATE TABLE t1 (c1 INT PRIMARY KEY, c2 INT);
Query OK, 0 rows affected
obclient> CREATE TABLE t2 (c1 INT PRIMARY KEY, c2 INT) PARTITION BY
HASH(c1) PARTITIONS 10;
Query OK, 0 rows affected
obclient> CREATE TABLE t3 (c1 INT PRIMARY KEY, c2 INT);
Query OK, 0 rows affected
obclient> CREATE INDEX IDX_t3_c2 ON t3 (c2) PARTITION BY HASH(c2) PARTITIONS 3;
Query OK, 0 rows affected
Q1:
obclient> EXPLAIN DELETE FROM t1 WHERE c2 > '100';
Query Plan:
======================================
|ID|OPERATOR |NAME|EST. ROWS|COST |
--------------------------------------
|0 |DELETE | |10000 |118697|
|1 | TABLE SCAN|T1 |10000 |108697|
======================================
Outputs & filters:
-------------------------------------
0 - output(nil), filter(nil), table_columns([{T1: ({T1: (T1.C1, T1.C2)})}])
1 - output([T1.C1], [T1.C2]), filter([T1.C2 > '100']),
access([T1.C1], [T1.C2]), partitions(p0)
In the preceding example, the Outputs & filters section shows in detail the output information of the DELETE operator.
| Field | Description |
|---|---|
| output | The output expression of the operator. |
| filter | The filter conditions of the operator. In this example, filter is set to nil because no filter condition is configured for the DELETE operator. For DELETE statements, the predicate in the WHERE clause is pushed down to the base table. For example, c2>'100' in query Q1 is pushed down to Operator 1. |
| table_columns | The table columns involved in the delete operation. |
More examples of the DELETE operator are as follows:
Query Q2 deletes all data rows in
t1.Query Q3 deletes data rows in a partitioned table
t2that satisfy the conditionc1 = 1.Query Q4 deletes data rows in a partitioned table
t2that satisfy the conditionc2 > '100'. As shown in the execution plan, theDELETEoperator is assigned under theEXCHANGEoperator. So, the operators 2 and 3 are scheduled as one task performed on a partition-by-partition basis. When executed, Operator 3 scans a partition oft2for rows satisfyingc2 > '100'. Operator 2DELETE, on the other hand, only deletes the data found by the scan from the corresponding partition.
Q2:
obclient> EXPLAIN DELETE FROM t1;
Query Plan:
======================================
|ID|OPERATOR |NAME|EST. ROWS|COST |
--------------------------------------
|0 |DELETE | |100000 |161860|
|1 | TABLE SCAN|T1 |100000 |61860 |
======================================
Outputs & filters:
-------------------------------------
0 - output(nil), filter(nil), table_columns([{T1: ({T1: (T1.C1, T1.C2)})}])
1 - output([T1.C1], [T1.C2]), filter(nil),
access([T1.C1], [T1.C2]), partitions(p0)
Q3:
obclient> EXPLAIN DELETE FROM t2 WHERE c1 = 1;
Query Plan:
===================================
|ID|OPERATOR |NAME|EST. ROWS|COST|
-----------------------------------
|0 |DELETE | |1 |53 |
|1 | TABLE GET|T2 |1 |52 |
===================================
Outputs & filters:
-------------------------------------
0 - output(nil), filter(nil), table_columns([{T2: ({T2: (T2.C1, T2.C2)})}])
1 - output([T2.C1], [T2.C2]), filter(nil),
access([T2.C1], [T2.C2]), partitions(p5)
Q4:
obclient> EXPLAIN DELETE FROM t2 WHERE c2 > '100';
Query Plan:
===============================================
|ID|OPERATOR |NAME |EST. ROWS|COST |
-------------------------------------------------------
|0 |PX COORDINATOR | |100000 |1186893|
|1 | EXCHANGE OUT DISTR |:EX10000|100000 |1186893|
|2 | PX PARTITION ITERATOR| |100000 |1186893|
|3 | DELETE | |100000 |1186893|
|4 | TABLE SCAN |T2 |100000 |1086893|
==================================================
Outputs & filters:
-------------------------------------
0 - output(nil), filter(nil)
1 - output(nil), filter(nil), dop=1
2 - output(nil), filter(nil)
3 - output(nil), filter(nil), table_columns([{T2: ({T2: (T2.C1, T2.C2)})}])
4 - output([T2.C1], [T2.C2]), filter([T2.C2 > '100']),
access([T2.C1], [T2.C2]), partitions(p[0-9])
DISTRIBUTED DELETE
The DISTRIBUTED DELETE operator deletes data from multiple partitions of a table.
In the following example, query Q5 deletes all rows that satisfy the condition c2 > '100' from table t3. Although t3 is a non-partitioned table, it has a global index idx_t3_c2. Therefore, each data row exists in multiple partitions.
Q5:
obclient> EXPLAIN DELETE FROM t3 WHERE c2 > '100';
Query Plan:
=================================================
|ID|OPERATOR |NAME|EST. ROWS|COST |
-------------------------------------------------
|0 |DISTRIBUTED INSERT | |2 |1 |
|1 | EXPRESSION | |2 |1 |
==================================================
Outputs & filters:
-------------------------------------
0 - output([column_conv(NUMBER,PS:(38,0),NOT NULL,__values.C1)], [column_conv(VARCHAR2,utf8mb4_bin,length:10,NULL,__values.C2)]), filter(nil),
columns([{T2: ({T2: (T2.C1, T2.C2)})}]), partitions(p0, p6)
1 - output([__values.C1], [__values.C2]), filter(nil)
values({2, '200'}, {3, '300'})
In the preceding example, the Outputs & filters section shows in detail the output information of the DISTRIBUTED DELETE operator. The fields of the operator have the same meaning as those of the DELETE operator.