GBase 8a
其他
文章

GBase 8a集群自增列介绍

发表于2025-01-25 10:10:02242次浏览0个评论

集群拓扑结果

[gbase@localhost ~]$ gcadmin showdistribution vc vc1

                               Distribution ID: 1 | State: new | Total segment num: 2

           Primary Segment Node IP                   Segment ID                 Duplicate Segment node IP
========================================================================================================================
|                 192.168.255.3                  |         1          |                 192.168.255.2                  |
------------------------------------------------------------------------------------------------------------------------
|                 192.168.255.2                  |         2          |                 192.168.255.3                  |
========================================================================================================================

自增列功能

GBase 8a集群自增列是指定 auto_increment 属性的列,自增列的列值单调递增(不保证连续)。自增列支持在以下数据类型的列上使用:tinyint、smallint、int、bigint。自增列可以唯一识别表中每一条记录,可方便用于查询、修改、删除等操作。
- 自增起始值:空表插入时,自增列的第一个值。
- 自增基值:自增列前一次插入的最大值。
- 自增步长:自增列数据入库时,下一行数据针对前一行数据的增量值。

 集群自增列不允许指定自增列起始值,基值,步长参数,由集群自动维护。

创建自增列

gbase> create table test_auto_increment(test_col int auto_increment primary key);
Query OK, 0 rows affected (Elapsed: 00:00:00.07)
gbase> show create table test_auto_increment;
+---------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table               | Create Table                                                                                                                                                                    |
+---------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| test_auto_increment | CREATE TABLE "test_auto_increment" (
"test_col" int(11) NOT NULL AUTO_INCREMENT,
PRIMARY KEY ("test_col")
) ENGINE=EXPRESS DEFAULT CHARSET=utf8 TABLESPACE='sys_tablespace' |
+---------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (Elapsed: 00:00:00.00)

非tinyint、smallint、int、bigint类型创建自增列报错

  gbase> create table test_auto_increment(test_col varchar(10) auto_increment);
  ERROR 1063 (42000): Incorrect column specifier for column 'test_col'
  gbase> create table test_auto_increment(test_col decimal(10) auto_increment);
  ERROR 1063 (42000): Incorrect column specifier for column 'test_col'

没有带primary key关键字报错

 gbase> create table test_auto_increment(test_col int auto_increment);
  ERROR 1702 (HY000): gcluster table error: Incorrect table definition; there can be only one auto column and it must be defined as a key.

显示设置test_col列default值为null,不会报错,但会创建成not null属性的类型

gbase> create table test_auto_increment1(test_col int default null auto_increment primary key);
  Query OK, 0 rows affected (Elapsed: 00:00:00.14)
  
  gbase> show create table test_auto_increment1;
  +----------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  | Table                | Create Table                                                                                                                                                                     |
  +----------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  | test_auto_increment1 | CREATE TABLE "test_auto_increment1" (
  "test_col" int(11) NOT NULL AUTO_INCREMENT,
  PRIMARY KEY ("test_col")
  ) ENGINE=EXPRESS DEFAULT CHARSET=utf8 TABLESPACE='sys_tablespace' |
  +----------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  1 row in set (Elapsed: 00:00:00.00)

与表类型无关

 gbase> create table test_auto_increment(col1 int, t_id int auto_increment primary key);
  Query OK, 0 rows affected (Elapsed: 00:00:00.05)
  
  gbase> create table test_auto_increment1(col1 int, t_id int auto_increment primary key) replicated;
  Query OK, 0 rows affected (Elapsed: 00:00:00.12)
  
  gbase> create table test_auto_increment2(col1 int, t_id int auto_increment primary key) distributed by ('col1');
  Query OK, 0 rows affected (Elapsed: 00:00:00.06)

  使用限制:
- data_type 为 tinyint、smallint、int、bigint;
- 自增列的属性必须为 not null;
- 自增列必须定义 primary key;
- 每个表最多只能有一个自增列。

insert values测试:

gbase> insert into test_auto_increment2(col1) values (1);
Query OK, 1 row affected (Elapsed: 00:00:00.08)

gbase> select * from test_auto_increment2;
+------+------+
| col1 | t_id |
+------+------+
|    1 |    1 |
+------+------+
1 rows in set (Elapsed: 00:00:00.02)

空表插入一条数据,自增列的值为1。

gbase> insert into test_auto_increment2(col1) values (2),(3),(4),(101),(102),(103),(104);
Query OK, 7 rows affected (Elapsed: 00:00:00.08)
Records: 7  Duplicates: 0  Warnings: 0

gbase> select * from test_auto_increment2;
+------+------+
| col1 | t_id |
+------+------+
|    1 |    1 |
|    3 |    2 |
|  103 |    4 |
|    2 |    3 |
|    4 |    5 |
|  101 |    7 |
|  102 |    9 |
|  104 |   11 |
+------+------+
8 rows in set (Elapsed: 00:00:00.02)

第二次插入了7条数据,因目标表为hash分布表,数据会根据hash值计算后,存入指定的分片内,其中:

192.168.255.3的n1分片的数据内容为:

[gbase@localhost gcluster]$ gncli -uroot -h192.168.255.3

GBase client 9.5.3.23.28416dc2. Copyright (c) 2004-2022, GBase.  All Rights Reserved.

gbase> select * from testdb.test_auto_increment2_n1;
+------+------+
| col1 | t_id |
+------+------+
|    1 |    1 |
|    3 |    2 |
|  103 |    4 |
+------+------+
3 rows in set (Elapsed: 00:00:00.00)

192.168.255.2的n2分片的数据内容为:

[gbase@localhost gcluster]$ gncli -uroot -h192.168.255.2

GBase client 9.5.3.23.28416dc2. Copyright (c) 2004-2022, GBase.  All Rights Reserved.

gbase> select * from testdb.test_auto_increment2_n2;
+------+------+
| col1 | t_id |
+------+------+
|    2 |    3 |
|    4 |    5 |
|  101 |    7 |
|  102 |    9 |
|  104 |   11 |
+------+------+
5 rows in set (Elapsed: 00:00:00.00)

自增列生成规则分析

通过以上特点发现,后续的7条数据每个分片的步长为2,进行自增。该步长是根据分片个数计算得出。
简单理解整个执行过程如下

insert into test_auto_increment2(col1) values (2),(3),(4),(101),(102),(103),(104);

第一步:接收的管理节点会先计算出目前最大的自增值:1。
第二步:生产分布式的执行SQL
其中n1分片的执行SQL:

insert into test_auto_increment2_n1(col1) values (3),(103);
set _gbase_auto_increment_base = 1+1; 
set auto_increment_increment = 2;

其中n2分片的执行SQL:

insert into test_auto_increment2_n2(col1) values (2),(101),(102),(104);
set _gbase_auto_increment_base = 1+2;
set auto_increment_increment = 2;

- _gbase_auto_increment_base :自增的基值;
- auto_increment_increment :自增的步长。

  再次插入部分数据:

  insert into test_auto_increment2(col1) values (5),(6),(7),(105),(106),(107),(108);

  再看节点层各分片的数据:

gbase> select * from testdb.test_auto_increment2_n1;
  +------+------+
  | col1 | t_id |
  +------+------+
  |    1 |    1 |
  |    3 |    2 |
  |  103 |    4 |
  |    7 |   12 |
  |  105 |   14 |
  |  107 |   16 |
  +------+------+
  6 rows in set (Elapsed: 00:00:00.01)
  
  gbase> select * from testdb.test_auto_increment2_n2;
  +------+------+
  | col1 | t_id |
  +------+------+
  |    2 |    3 |
  |    4 |    5 |
  |  101 |    7 |
  |  102 |    9 |
  |  104 |   11 |
  |    5 |   13 |
  |    6 |   15 |
  |  106 |   17 |
  |  108 |   19 |
  +------+------+
  9 rows in set (Elapsed: 00:00:00.00)

 

 

 

评论

登录后才可以发表评论