任务管理语句是PolarDB-X专有的扩展SQL语句,可用于查看DDL任务的状态、恢复或回滚失败的DDL任务等。本文将详细介绍任务管理语句的语法和用法。
SHOW [FULL] DDL
参数 | 说明 |
---|---|
FULL | 显示DDL任务的所有信息,若不带该参数则结果集中只显示如下常用信息:
|
字段 | 含义 |
---|---|
JOB_ID | DDL任务唯一标识,取值需为64位有符号长整型数值。 |
PARENT_JOB_ID | 该DDL任务的父任务唯一标识,取值需为64位有符号长整型数值。
说明 当目标DDL任务为独立无父任务时,该参数取值为0。
|
SERVER | 执行DDL任务的DRDS节点信息。 |
OBJECT_SCHEMA | DDL任务对象的Schema名称,即当前数据库名称。 |
OBJECT_NAME | DDL任务对象名称,例如当前执行DDL的表名称。 |
NEW_OBJECT_NAME | DDL任务新对象名称。
说明 仅当DDL任务类型为RENAME TABLE时显示该参数,表示目标表的新名称。
|
JOB_TYPE | DDL任务类型。 |
PHASE | DDL任务当前所处的阶段。 |
STATE | DDL任务当前所处的状态。 |
PROGRESS | DDL任务执行进度。 |
START_TIME | DDL任务开始执行的时间。 |
END_TIME | DDL任务结束执行的时间。 |
ELAPSED_TIME | DDL任务截止到任务查看时已经消耗的时间,单位为毫秒。 |
DDL_STMT | 原始的DDL语句。 |
REMARK | DDL任务的备注信息。
说明 当DDL任务状态为PENDING时,该参数会显示DDL任务失败的原因。
|
创建一个既分库又分表的拆分表,执行过程中查看状态。
mysql> create table test_mdb_mtb (c1 int not null auto_increment primary key, c2 varchar(10), c3 date) dbpartition by hash(c1) tbpartition by hash(c1) tbpartitions 64;
mysql> show full ddl\G
*************************** 1. row ***************************
JOB_ID: 1103792075578957824
PARENT_JOB_ID: 0
SERVER: 1:102:10.81.69.55
OBJECT_SCHEMA: ddltest
OBJECT_NAME: test_mdb_mtb
NEW_OBJECT_NAME:
JOB_TYPE: CREATE_TABLE
PHASE: EXECUTE
STATE: RUNNING
PROGRESS: 90%
START_TIME: 2019-08-29 14:29:58.787
END_TIME: 2019-08-29 14:30:07.177
ELAPSED_TIME(MS): 8416
DDL_STMT: create table test_mdb_mtb (c1 int not null auto_increment primary key, c2 varchar(10), c3 date) dbpartition by hash(c1) tbpartition by hash(c1) tbpartitions 64
REMARK:
RECOVER DDL { ALL | <job_id> [ , <job_id> ] ... }
参数 | 说明 |
---|---|
ALL | 恢复所有处于PENDING状态的DDL任务,被恢复的任务会串行执行,请慎用此参数。 |
job_id | 通过SHOW DDL查看到的处于PENDING状态的任务ID。 |
创建一个既分库又分表的拆分表,任务执行过程中被中断,通过SHOW DDL查看状态和job_id
,然后用RECOVER DDL恢复任务,直至该表创建完成。
mysql> create table test_mdb_mtb (c1 int not null auto_increment primary key, c2 varchar(10), c3 date) dbpartition by hash(c1) tbpartition by hash(c1) tbpartitions 64;
^C^C -- query aborted
mysql> show ddl\G
*************************** 1. row ***************************
JOB_ID: 1103796219480006656
OBJECT_SCHEMA: ddltest
OBJECT_NAME: test_mdb_mtb
JOB_TYPE: CREATE_TABLE
PHASE: EXECUTE
STATE: PENDING
PROGRESS: 33%
START_TIME: 2019-08-29 14:46:26.769
END_TIME: 2019-08-29 14:46:29.691
ELAPSED_TIME(MS): 2922
DDL_STMT: create table test_mdb_mtb (c1 int not null auto_increment primary key, c2 varchar(10), c3 date) dbpartition by hash(c1) tbpartition by hash(c1) tbpartitions 64
REMARK: The job has been interrupted unexpectedly
mysql> recover ddl 1103796219480006656;
Query OK, 0 rows affected (7.28 sec)
mysql> check table test_mdb_mtb;
+----------------------------------------+-------+----------+----------+
| TABLE | OP | MSG_TYPE | MSG_TEXT |
+----------------------------------------+-------+----------+----------+
| ddltest_1562056402230oymk.test_mdb_mtb | check | status | OK |
+----------------------------------------+-------+----------+----------+
1 row in set (2.24 sec)
ROLLBACK DDL <job_id> [ , <job_id> ] ...
参数 | 说明 |
---|---|
job_id | 通过SHOW DDL查看到的处于PENDING状态的任务ID。 |
创建一个既分库又分表的拆分表,任务执行过程中被中断,通过SHOW DDL查看状态和job_id
,然后用ROLLBACK DDL回滚任务。
mysql> create table test_mdb_mtb (c1 int not null auto_increment primary key, c2 varchar(10), c3 date) dbpartition by hash(c1) tbpartition by hash(c1) tbpartitions 64;
^C^C -- query aborted
mysql> show ddl\G
*************************** 1. row ***************************
JOB_ID: 1103797850607083520
OBJECT_SCHEMA: ddltest
OBJECT_NAME: test_mdb_mtb
JOB_TYPE: CREATE_TABLE
PHASE: EXECUTE
STATE: PENDING
PROGRESS: 40%
START_TIME: 2019-08-29 14:52:55.660
END_TIME: 2019-08-29 14:52:58.885
ELAPSED_TIME(MS): 3225
DDL_STMT: create table test_mdb_mtb (c1 int not null auto_increment primary key, c2 varchar(10), c3 date) dbpartition by hash(c1) tbpartition by hash(c1) tbpartitions 64
REMARK: The job has been interrupted unexpectedly
mysql> rollback ddl 1103797850607083520;
Query OK, 0 rows affected (6.42 sec)
mysql> show tables like ‘test_mdb_mtb‘;
Empty set (0.00 sec)
取消正在执行中(即非PENDING状态)的DDL任务。
CANCEL DDL <job_id> [ , <job_id> ] ...
参数 | 说明 |
---|---|
job_id | 通过SHOW DDL查看到的处于非PENDING状态的任务ID。 |
创建一个既分库又分表的拆分表,任务执行过程中通过CANCEL DDL取消,通过SHOW DDL查看状态和 job_id
,后续可以恢复或者回滚该任务。
mysql> create table test_mdb_mtb (c1 int not null auto_increment primary key, c2 varchar(10), c3 date) dbpartition by hash(c1) tbpartition by hash(c1) tbpartitions 64;
mysql> show ddl\G
*************************** 1. row ***************************
JOB_ID: 1103798959568478208
OBJECT_SCHEMA: ddltest
OBJECT_NAME: test_mdb_mtb
JOB_TYPE: CREATE_TABLE
PHASE: EXECUTE
STATE: RUNNING
PROGRESS: 26%
START_TIME: 2019-08-29 14:57:20.058
END_TIME: 2019-08-29 14:57:22.284
ELAPSED_TIME(MS): 2243
DDL_STMT: create table test_mdb_mtb (c1 int not null auto_increment primary key, c2 varchar(10), c3 date) dbpartition by hash(c1) tbpartition by hash(c1) tbpartitions 64
REMARK:
mysql> cancel ddl 1103798959568478208;
Query OK, 2 rows affected (0.03 sec)
mysql> show ddl\G
*************************** 1. row ***************************
JOB_ID: 1103798959568478208
OBJECT_SCHEMA: ddltest
OBJECT_NAME: test_mdb_mtb
JOB_TYPE: CREATE_TABLE
PHASE: EXECUTE
STATE: PENDING
PROGRESS: 87%
START_TIME: 2019-08-29 14:57:20.058
END_TIME: 2019-08-29 14:57:28.899
ELAPSED_TIME(MS): 8841
DDL_STMT: create table test_mdb_mtb (c1 int not null auto_increment primary key, c2 varchar(10), c3 date) dbpartition by hash(c1) tbpartition by hash(c1) tbpartitions 64
REMARK: ERR-CODE: [TDDL-4636][ERR_DDL_JOB_ERROR] The job ‘1103798959568478208‘ has been cancelled.
REMOVE DDL { ALL PENDING | <job_id> [ , <job_id> ] ... }
参数 | 说明 |
---|---|
ALL PENDING | 删除所有处于PENDING状态的任务,同时清理内部缓存。 |
job_id | 通过SHOW DDL查看到的处于PENDING状态的任务ID。 |
数据库已有两张表,之间建立了参照完整性关系,当尝试删除父表时报错,因为存在参照完整性约束不允许删除,如果此时不想再执行删除表的操作,那么可以删除该DDL任务。
mysql> show create table test_parent\G
*************************** 1. row ***************************
Table: test_parent
Create Table: CREATE TABLE `test_parent` (
`id` int(11) NOT NULL,
`pkey` int(11) NOT NULL,
`col` int(11) DEFAULT NULL,
PRIMARY KEY (`id`,`pkey`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 dbpartition by hash(`id`)
1 row in set (0.01 sec)
mysql> show create table test_child\G
*************************** 1. row ***************************
Table: test_child
Create Table: CREATE TABLE `test_child` (
`id` int(11) DEFAULT NULL,
`parent_id` int(11) DEFAULT NULL,
KEY `parent_id` (`parent_id`),
CONSTRAINT `test_child_ibfk_1` FOREIGN KEY (`parent_id`) REFERENCES `test_parent` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 dbpartition by hash(`parent_id`)
1 row in set (0.02 sec)
mysql> drop table test_parent;
ERROR 4636 (HY000): [f518265d0066000][10.81.69.55:3306][ddltest]ERR-CODE: [TDDL-4636][ERR_DDL_JOB_ERROR] Not all physical operations have been done successfully: expected 9,
but done 0. Caused by: 1217:DDLTEST_1562056402230OYMK_7WW7_0007:Cannot delete or update a parent row: a foreign key constraint fails on `test_parent`;1217:DDLTEST_15620564022
30OYMK_7WW7_0000:Cannot delete or update a parent row: a foreign key constraint fails on `test_parent`;1217:DDLTEST_1562056402230OYMK_7WW7_0002:Cannot delete or update a pare
nt row: a
mysql> show ddl\G
*************************** 1. row ***************************
JOB_ID: 1103806757547171840
OBJECT_SCHEMA: ddltest
OBJECT_NAME: test_parent
JOB_TYPE: DROP_TABLE
PHASE: EXECUTE
STATE: PENDING
PROGRESS: 0%
START_TIME: 2019-08-29 15:28:19.240
END_TIME: 2019-08-29 15:28:19.456
ELAPSED_TIME(MS): 216
DDL_STMT: drop table test_parent
REMARK: ERR-CODE: [TDDL-4636][ERR_DDL_JOB_ERROR] Not all physical operations have been done successfully: expected 9, but done 0. Caused by: 1217:DDLTEST_1562056402
230OYMK_7WW7_0007:Cannot delete or update a parent row: a foreign key constraint fails on `test_pare ...
mysql> check table test_parent;
+---------------------------------------+-------+----------+----------+
| TABLE | OP | MSG_TYPE | MSG_TEXT |
+---------------------------------------+-------+----------+----------+
| ddltest_1562056402230oymk.test_parent | check | status | OK |
+---------------------------------------+-------+----------+----------+
1 row in set (0.05 sec)
mysql> show tables like ‘test_parent‘;
Empty set (0.00 sec)
mysql> show create table test_parent;
ERROR 4642 (HY000): [f5185a78b066000][10.81.69.55:3306][ddltest]ERR-CODE: [TDDL-4642][ERR_UNKNOWN_TABLE] Unknown table ‘ddltest.test_parent‘
mysql> remove ddl 1103806757547171840;
Query OK, 1 row affected (0.02 sec)
mysql> show tables like ‘test_parent‘;
+-------------------+
| TABLES_IN_DDLTEST |
+-------------------+
| test_parent |
+-------------------+
1 row in set (0.01 sec)
原文:https://www.cnblogs.com/weiyi1314/p/14927768.html