返回首页

gbase数据、南大通用产品文档:GBase8a

更新日期:2024年09月11日

添加/删除自增列
功能说明
通过ALTER TABLE 创建增加自增列。对于非空表增加自增列时,会自动对添加
的自增列进行数据填充。

GBase 8a MPP Cluster 产品手册
5 数据库管理指南
文档版本953(2022-04-10)
南大通用数据技术股份有限公司
1021
语法格式
alter table [vc_name.][database_name.]table_name alter_specification
[,alter_specification] ...
alter_specification:
|add [column] col_name column_definition
| drop [column] col_name
示例
示例1:空表添加自增列
gbase>
CREATE TABLE t1 (a VARCHAR(10));
Query OK, 0 rows affected (Elapsed: 00:00:00.07)
gbase>
ALTER
TABLE
t1 ADD
COLUMN
id
INT
NOT
NULL
AUTO_INCREMENT PRIMARY KEY;
Query OK, 0 rows affected (Elapsed: 00:00:00.10)
Records: 0
Duplicates: 0
Warnings: 0
gbase> SHOW CREATE TABLE t1\G
*************************** 1. row ***************************
Table: t1
Create Table: CREATE TABLE "t1" (
"a" varchar(10) DEFAULT NULL,
"id" int(11) NOT NULL AUTO_INCREMENT,
PRIMARY KEY ("id")
)
ENGINE=EXPRESS
DEFAULT
CHARSET=utf8
TABLESPACE='sys_tablespace'
1 row in set (Elapsed: 00:00:00.00)
示例2:非空表添加自增列

GBase 8a MPP Cluster 产品手册
5 数据库管理指南
文档版本953(2022-04-10)
南大通用数据技术股份有限公司
1022
gbase>
CREATE TABLE t1 (a VARCHAR(10));
Query OK, 0 rows affected (Elapsed: 00:00:00.07)
gbase>
INSERT INTO t1 VALUES('a'),('b'),('c');
Query OK, 3 rows affected (Elapsed: 00:00:00.07)
Records: 3
Duplicates: 0
Warnings: 0
gbase>
ALTER
TABLE
t1 ADD
COLUMN
id
INT
NOT
NULL
AUTO_INCREMENT PRIMARY KEY;
Query OK, 3 rows affected (Elapsed: 00:00:00.10)
Records: 3
Duplicates: 3
Warnings: 0
base>
SHOW CREATE TABLE t1\G
*************************** 1. row ***************************
Table: t1
Create Table: CREATE TABLE "t1" (
"a" varchar(10) DEFAULT NULL,
"id" int(11) NOT NULL AUTO_INCREMENT,
PRIMARY KEY ("id")
)
ENGINE=EXPRESS
DEFAULT
CHARSET=utf8
TABLESPACE='sys_tablespace'
1 row in set (Elapsed: 00:00:00.00)
gbase> SELECT * FROM t1;
+------+----+
| a
| id |
+------+----+

GBase 8a MPP Cluster 产品手册
5 数据库管理指南
文档版本953(2022-04-10)
南大通用数据技术股份有限公司
1023
| a
|
2 |
| b
|
6 |
| c
|
10 |
+------+----+
3 rows in set (Elapsed: 00:00:00.02)
示例3:删除自增列
gbase> CREATE TABLE t1 (a1 INT AUTO_INCREMENT PRIMARY
KEY,b1 INT ,c1 INT) ;
Query OK, 0 rows affected (Elapsed: 00:00:00.10)
gbase>
INSERT INTO t1(b1,c1) VALUES(2,3),(8,9);
Query OK, 2 rows affected (Elapsed: 00:00:00.09)
Records: 2
Duplicates: 0
Warnings: 0
gbase>
SELECT * FROM t1;
+----+------+------+
| a1 | b1
| c1
|
+----+------+------+
|
2 |
2 |
3 |
|
6 |
8 |
9 |
+----+------+------+
2 rows in set (Elapsed: 00:00:00.03)
gbase>
SHOW CREATE TABLE t1 \G
*************************** 1. row ***************************
Table: t1
Create Table: CREATE TABLE "t1" (

GBase 8a MPP Cluster 产品手册
5 数据库管理指南
文档版本953(2022-04-10)
南大通用数据技术股份有限公司
1024
"a1" int(11) NOT NULL AUTO_INCREMENT,
"b1" int(11) DEFAULT NULL,
"c1" int(11) DEFAULT NULL,
PRIMARY KEY ("a1")
)
ENGINE=EXPRESS
DEFAULT
CHARSET=utf8
TABLESPACE='sys_tablespace'
1 row in set (Elapsed: 00:00:00.00)
gbase>
ALTER TABLE t1
DROP COLUMN a1;
Query OK, 2 rows affected (Elapsed: 00:00:00.10)
Records: 2
Duplicates: 2
Warnings: 0
gbase>
SHOW CREATE TABLE t1 \G
*************************** 1. row ***************************
Table: t1
Create Table: CREATE TABLE "t1" (
"b1" int(11) DEFAULT NULL,
"c1" int(11) DEFAULT NULL
)
ENGINE=EXPRESS
DEFAULT
CHARSET=utf8
TABLESPACE='sys_tablespace'
1 row in set (Elapsed: 00:00:00.01)
gbase>
SELECT * FROM t1;
+------+------+
| b1
| c1
|
+------+------+
|
2 |
3 |

GBase 8a MPP Cluster 产品手册
5 数据库管理指南
文档版本953(2022-04-10)
南大通用数据技术股份有限公司
1025
|
8 |
9 |
+------+------+
2 rows in set (Elapsed: 00:00:00.03)

整数类型。INTEGER 的同义词。
它的范围是-2147483647 到2147483647,INT 占用
4 个字节。

背景信息
在SQL 语句执行性能不符合预期时,
可以查看SQL 语句执行信息,
便于事后分析SQL
语句执行时的行为,从而诊断SQL 语句执行出现的相关问题。
前提条件
数据库实例运行正常。
查询SQL 语句信息,需要正确设置GUC 参数track_stmt_stat_level。
只能用系统管理员和监控管理员权限进行操作。
应用场景

查看数据库实例中慢SQL 语句执行信息,语法格式:
select * from dbe_perf.get_global_slow_sql_by_timestamp(start_timestamp,
end_timestamp);

查看数据库实例中SQL 语句执行信息,语法格式:
select * from dbe_perf.get_global_full_sql_by_timestamp(start_timestamp,
end_timestamp);

GBase 8c 管理员指南
南大通用数据技术股份有限公司
50
例如:
postgres=# select * from DBE_PERF.get_global_full_sql_by_timestamp('2020-12-01
09:25:22', '2020-12-31 23:54:41');
-[ RECORD
1 ]--------+-----------------------------------------------------------------
----------------------------------------------
---------------------------------------------------------------
node_name
| dn_6001_6002_6003
db_name
| postgres
schema_name
| "$user",public
origin_node
| 1938253334
user_name
| user_dj
application_name
| gsql
client_addr
|
client_port
| -1
unique_query_id
| 3671179229
debug_query_id
| 72339069014839210
query
| select name, setting from pg_settings where name in (?)
start_time
| 2020-12-19 16:19:51.216818+08
finish_time
| 2020-12-19 16:19:51.224513+08
slow_sql_threshold
| 1800000000
transaction_id
| 0
thread_id
| 139884662093568
session_id
| 139884662093568
n_soft_parse
| 0
n_hard_parse
| 1
query_plan
| Datanode Name: dn_6001_6002_6003
| Function Scan on pg_show_all_settings a
(cost=0.00..12.50 rows=5 width=64)
|
Filter: (name = '***'::text)
...

查看当前主节点SQL 语句执行信息,语法格式:
select * from statement_history;
例如:
postgres=# select * from statement_history;
-[ RECORD
1 ]--------+-----------------------------------------------------------------
----------------------------------------------

GBase 8c 管理员指南
南大通用数据技术股份有限公司
51
---------------------------------------------------------------
db_name
| postgres
schema_name
| "$user",public
origin_node
| 1938253334
user_name
| user_dj
application_name
| gsql
client_addr
|
client_port
| -1
unique_query_id
| 3671179229
debug_query_id
| 72339069014839210
query
| select name, setting from pg_settings where name in (?)
start_time
| 2020-12-19 16:19:51.216818+08
finish_time
| 2020-12-19 16:19:51.224513+08
slow_sql_threshold
| 1800000000
transaction_id
| 0
thread_id
| 139884662093568
session_id
| 139884662093568
n_soft_parse
| 0
n_hard_parse
| 1
query_plan
| Datanode Name: dn_6001_6002_6003
| Function Scan on pg_show_all_settings a
(cost=0.00..12.50 rows=5 width=64)
|
Filter: (name = '***'::text)
...

查看当前备节点SQL 语句执行信息
select * from dbe_perf.standby_statement_history(is_only_slow, start_timestamp,
end_timestamp);
例如:
postgres=# select * from dbe_perf.standby_statement_history(true, '2022-08-01
09:25:22', '2022-08-31 23:54:41');
db_name
| postgres
schema_name
| "$user",public
origin_node
| 0
user_name
| user_dj
application_name
| gsql
client_addr
|
client_port
| -1
unique_query_id
| 1660376009
debug_query_id
| 281474976710740

GBase 8c 管理员指南
南大通用数据技术股份有限公司
52
query
| select name, setting from pg_settings where name in (?)
start_time
| 2022-08-19 16:19:51.216818+08
finish_time
| 2022-08-19 16:19:51.224513+08
slow_sql_threshold
| 1800000000
transaction_id
| 0
thread_id
| 140058747205376
session_id
| 140058747205376
n_soft_parse
| 0
n_hard_parse
| 1
query_plan
| Datanode Name: sgnode
| Function Scan on pg_show_all_settings a
(cost=0.00..12.50 rows=5 width=64)
|
Filter: (name = '***'::text)
...

GBase 8c 管理员指南
南大通用数据技术股份有限公司
53
5
备份与恢复