DELETE 算子用于删除数据表中满足指定条件的数据行。

OceanBase 数据库支持的 DELETE 算子包括 DELETE 和 MULTI PARTITION DELETE。

DELETE

DELETE 算子用于删除数据表单个分区中的数据。

如下例所示,Q1 查询删除了表 t1 中所有满足 c2>'100' 的行。

  1. obclient>CREATE TABLE t1 (c1 INT PRIMARY KEY, c2 VARCHAR2(10));
  2. Query OK, 0 rows affected (0.12 sec)
  3. obclient>CREATE TABLE t2 (c1 INT PRIMARY KEY, c2 VARCHAR2(10)) PARTITION BY
  4. HASH(c1) PARTITIONS 10;
  5. Query OK, 0 rows affected (0.12 sec)
  6. obclient>CREATE TABLE t3 (c1 INT PRIMARY KEY, c2 VARCHAR2(10));
  7. Query OK, 0 rows affected (0.12 sec)
  8. obclient>CREATE INDEX IDX_t3_c2 ON t3 (c2) PARTITION BY HASH(c2) PARTITIONS 3;
  9. Query OK, 0 rows affected (0.12 sec)
  10. Q1:
  11. obclient>EXPLAIN DELETE FROM t1 WHERE c2 > '100'\G;
  12. *************************** 1. row ***************************
  13. Query Plan:
  14. ======================================
  15. |ID|OPERATOR |NAME|EST. ROWS|COST |
  16. --------------------------------------
  17. |0 |DELETE | |10000 |118697|
  18. |1 | TABLE SCAN|T1 |10000 |108697|
  19. ======================================
  20. Outputs & filters:
  21. -------------------------------------
  22. 0 - output(nil), filter(nil), table_columns([{T1: ({T1: (T1.C1, T1.C2)})}])
  23. 1 - output([T1.C1], [T1.C2]), filter([T1.C2 > '100']),
  24. access([T1.C1], [T1.C2]), partitions(p0)

上述示例中,执行计划展示中的 outputs & filters 详细列出了 DELETE 算子的输出信息如下:

信息名称

含义

output

该算子输出的表达式。

filter

该算子上的过滤条件。由于示例中 DELETE 算子没有设置 filter,所以为 nil。对于删除语句,WHERE 中的谓词会下推到基表上,比如 Q1 查询中的 c2>’100’ 被下推到了 1 号算子上。

table_columns

删除操作涉及的数据表的列。

更多 DELETE 算子的示例如下:

  • Q2 查询删除 t1 中的所有数据行。

  • Q3 查询删除分区表 t2 中满足 c1 = 1 的数据行。

  • Q4 查询删除分区表 t2 中满足 c2 > '100' 的数据行。从执行计划中可以看到,DELETE 算子分配在 EXCHANGE 算子下面,因此 2 号和 3 号算子会作为一个 task 以分区的粒度进行调度。在计划执行时, 3 号算子扫描出 t2 一个分区中满足 c2 > '100' 的数据,2 号算子 DELETE 则只会删除相应分区下扫描出的数据。

  1. Q2:
  2. obclient>EXPLAIN DELETE FROM t1\G;
  3. *************************** 1. row ***************************
  4. Query Plan:
  5. ======================================
  6. |ID|OPERATOR |NAME|EST. ROWS|COST |
  7. --------------------------------------
  8. |0 |DELETE | |100000 |161860|
  9. |1 | TABLE SCAN|T1 |100000 |61860 |
  10. ======================================
  11. Outputs & filters:
  12. -------------------------------------
  13. 0 - output(nil), filter(nil), table_columns([{T1: ({T1: (T1.C1, T1.C2)})}])
  14. 1 - output([T1.C1], [T1.C2]), filter(nil),
  15. access([T1.C1], [T1.C2]), partitions(p0)
  16. Q3:
  17. obclient>EXPLAIN DELETE FROM t2 WHERE c1 = 1\G;
  18. *************************** 1. row ***************************
  19. Query Plan:
  20. ===================================
  21. |ID|OPERATOR |NAME|EST. ROWS|COST|
  22. -----------------------------------
  23. |0 |DELETE | |1 |53 |
  24. |1 | TABLE GET|T2 |1 |52 |
  25. ===================================
  26. Outputs & filters:
  27. -------------------------------------
  28. 0 - output(nil), filter(nil), table_columns([{T2: ({T2: (T2.C1, T2.C2)})}])
  29. 1 - output([T2.C1], [T2.C2]), filter(nil),
  30. access([T2.C1], [T2.C2]), partitions(p5)
  31. Q4:
  32. obclient>EXPLAIN DELETE FROM t2 WHERE c2 > '100'\G;
  33. *************************** 1. row ***************************
  34. Query Plan:
  35. ===============================================
  36. |ID|OPERATOR |NAME |EST. ROWS|COST |
  37. -------------------------------------------------------
  38. |0 |PX COORDINATOR | |100000 |1186893|
  39. |1 | EXCHANGE OUT DISTR |:EX10000|100000 |1186893|
  40. |2 | PX PARTITION ITERATOR| |100000 |1186893|
  41. |3 | DELETE | |100000 |1186893|
  42. |4 | TABLE SCAN |T2 |100000 |1086893|
  43. ==================================================
  44. Outputs & filters:
  45. -------------------------------------
  46. 0 - output(nil), filter(nil)
  47. 1 - output(nil), filter(nil), dop=1
  48. 2 - output(nil), filter(nil)
  49. 3 - output(nil), filter(nil), table_columns([{T2: ({T2: (T2.C1, T2.C2)})}])
  50. 4 - output([T2.C1], [T2.C2]), filter([T2.C2 > '100']),
  51. access([T2.C1], [T2.C2]), partitions(p[0-9])

MULTI PARTITION DELETE

MULTI PARTITION DELETE 算子用于删除数据表多个分区中的数据。

如下例所示,Q5 查询删除了表 t3 中所有满足 c2 > '100' 的数据行。虽然 t3 本身是一个非分区表,但因为 t3 上存在全局索引 idx_t3_c2,因此每一条数据行会存在于多个分区中。

  1. Q5:
  2. obclient>EXPLAIN DELETE FROM t3 WHERE c2 > '100'\G;
  3. *************************** 1. row ***************************
  4. Query Plan:
  5. ========================================================
  6. |ID|OPERATOR |NAME |EST. ROWS|COST |
  7. -----------------------------------------------------------
  8. |0 |MULTI PARTITION DELETE | |10001 |27780|
  9. |1 | PX COORDINATOR | |10001 |17780|
  10. |2 | EXCHANGE OUT DISTR |:EX10000 |10001 |14941|
  11. |3 | PX PARTITION ITERATOR| |10001 |14941|
  12. |4 | TABLE SCAN |T3(IDX_T3_C2)|10001 |14941|
  13. ===========================================================
  14. Outputs & filters:
  15. -------------------------------------
  16. 0 - output(nil), filter(nil), table_columns([{T3: ({T3: (T3.C1, T3.C2)}, {IDX_T3_C2: (T3.C2, T3.C1)})}])
  17. 1 - output([T3.C1], [T3.C2]), filter(nil)
  18. 2 - output([T3.C2], [T3.C1]), filter(nil), dop=1
  19. 3 - output([T3.C2], [T3.C1]), filter(nil)
  20. 4 - output([T3.C2], [T3.C1]), filter(nil),
  21. access([T3.C2], [T3.C1]), partitions(p[0-2])

上述示例的执行计划展示中的 outputs & filters 详细列出了 MULTI PARTITION DELETE 算子的信息,字段的含义与 DELETE 算子相同。