当前位置:网站首页>Kunlun distributed database sequence function and its implementation mechanism
Kunlun distributed database sequence function and its implementation mechanism
2022-06-22 23:49:00 【Kunlunbase Kunlun database】
Kunlun distributed database ( Kunlun or Kunlun database for short ) The calculation node of is derived from PostgreSQL, So I inherited PostgreSQL Of Sequence function , This paper introduces the design of Kunlun distributed database Sequence Function and usage of 、 Use cases and implementations .
Usage and use cases
Kunlun database Sequence And MySQL Of autoincrement( On the column ) comparison , Its functions are more powerful and flexible . Mainly reflected in the following aspects :
1. Kunlun Sequence And tables are many to many , and MySQL The autoincrement and table of are 1 Yes 1 Relationship
say concretely , Each table of Kunlun database can have any number of sequence Columns use the same or different sequence Generate sequence values ;
And each of these sequence Can be used by any number of columns of any number of tables to generate ID value .
and MySQL Each table can have at most one auto incrementing column, and this auto incrementing column can only be used by this table ( This is bullshit , But for the sake of content symmetry, I still need to mention ).
2. Can be adjusted at any time sequence The initial value of the , Maximum , step , Range and other attributes, and then continue to use , then sequence A new sequence value will be generated according to the new attribute .
3. Don't rely on Index , After clearing the table, the sequence value does not wrap around .
4. Multiple computing nodes in Kunlun database cluster use the same server directly or indirectly sequence Can produce globally unique sequence values .
Let's take an example , First create a table t1,t1 The primary key column of serial Type indicates that it uses an implicitly created sequence To generate field values , So you can insert without specifying the field value .
create table t1(a serial primary key, b int);
Then create sequence seq_b, Ready to use seq_b To generate field values . You can optionally specify sequence Properties of , Use default values without specifying .
create sequence seq_b;First execute this statement to insert 9 That's ok , Explicit call seq_b Generate field values .
insert into t1(b) values(nextval('seq_b'));You can see t1 The data are as follows :
select*from t1;
Then create the table t2, its b and c All columns use seq_b Generate default field values , And its primary key column is also applicable to implicit sequence To generate field values .
create table t2(a serial primary key, b int default nextval('seq_b'), c int default nextval('seq_b'));because t2 All fields have default values , So insert the table with the following statement t2 3 That's ok .
insert into t2 default values;see t2 The data of , You can see each line b and c The field is to use seq_b The field values generated in turn , And from seq_b Last generated 9 Then start generating sequence values .
select*from t2;
Last , You can also use select nextval('seq_b'); Such a statement to directly generate sequence values .
modify sequence Metadata and others
have access to ALTER SEQUENCE Statement to modify sequence Properties of , You can also use ALTER TABLE ... ALTER COLUMN ... SET seqoptions Statement to modify the implicit sequence Properties of .
You can also use the above alter table sentence restart One sequence. And you can use lastval() Function to obtain sequence Last returned value .

sequence Realization
Kunlun database sequence The implementation inherits PostgreSQL The original sequence Mechanism .
In order to make sequence Data has disaster tolerance capability and can be used by any number of computing nodes at the same time , therefore sequence The numerical data related to the distribution of sequence values is stored in the storage node mysql.sequences In the table , Each line corresponds to one sequence.
One sequence In which storage cluster is the metadata stored , Is to create sequence Is dynamically allocated by the computing node .
sequence Other metadata of is stored in the computing node , You can use the following statement to view sequence Metadata in the compute node :
select t2.relname, t2.oid, seqstart, seqincrement, seqmax, seqmin, seqcache, seqcycle from pg_sequence t1, pg_class t2 where t1.seqrelid = t2.oid;
You can see t1 and t2 The implicit of the primary key column sequence Namely t1_a_seq and t2_a_seq, And explicitly created seq_b , these sequence The storage clusters of the numerical metadata are 1,2,1.
meanwhile , You can see sequence The basic metadata of is also stored in pg_class Metadata table , Its unique properties are stored in pg_sequence In the table .
Respectively connected to No 1 and 2 Of shard Check this out 3 individual sequence In these two storage clusters mysql.sequence Numerical metadata in the table , You can see the following information :


When using a for the first time sequence Or when the reserved value range is exhausted , A computing node CN Will pass through its cluster_log_applier Process to this sequence In the same storage cluster reserve (curval, cur_val + max(10, seqcache)) The field value of this range , then CN Use this reserve The scope for this sequence Distribute sequence values , Until it runs out again .
such , Even if multiple compute nodes use the same sequence To distribute sequence values , It can still maintain high performance and keep the sequence values distributed by all computing nodes unique .
Conclusion
Popular speaking , If both reading and writing to the database are operated in the same database server , Business system performance will be degraded .
In order to improve the performance of business system , Optimize the user experience , You can reduce the load of the master database by master-slave replication .
And if the primary database goes down , It can quickly switch the business system to the slave database , Avoid data loss .
The project is open source
【GitHub:】
https://github.com/zettadb
【Gitee:】
https://gitee.com/zettadb
THE END
边栏推荐
- Es5 object extension methods //call, apply and bind
- What does password security mean? What are the password security standard clauses in the ISO 2.0 policy?
- Express、路由(Route)、Request对象、Response对象、中间件、EJS模板
- OJ每日一练——过滤多余的空格
- Bubble sort pointer
- OJ每日一练——病毒的增生
- 为什么大家很少使用外键了?
- 弱电转职业网工难不难?华为售前工程师分享亲身经历
- 在Word中自定义多级列表样式
- swagger2 使用方法
猜你喜欢

Php7.3 error undefined function simplexml_ load_ string()

Canvas generate Poster

XML escape character cross reference table
![[go] getting started with go modules](/img/0a/58c50bb624c91b88a88aea280aa650.jpg)
[go] getting started with go modules

C sqlsugar, hisql, FreeSQL ORM framework all-round performance test vs. sqlserver performance test

Digital data depth | about software self-control, source code left, no code right

考过HCIP依然转行失败,职业网工最看重的到底是什么

canvas生成海报

JSBridge

Reverse proxy haproxy
随机推荐
Tianyi cloud takes advantage of the new infrastructure to build a "4+2" capability system for digital transformation
防抖&节流 加强版
OJ daily practice - spanning 2020
JSBridge
为什么大家很少使用外键了?
OJ每日一练——删除单词后缀
双重跨域:Access-Allow-Origin header contains multiple values“*, *”,but only one is allowed
Smart data won two annual awards at the second isig China Industrial Intelligence Conference
Problèmes rencontrés lors de l'utilisation de redistemplate
再立云计算“昆仑”,联想混合云Lenovo xCloud凭什么?
从类、API、框架三个层面学习设计可复用软件的具体技术学习心得
OJ daily practice - class dining
【ARM】讯为rk3568开发板lvds屏设置横屏显示
Tp5.1 upload excel file and read its contents
Tp5.1 solving cross domain problems
Longest word in output string
【STM32技巧】使用STM32 HAL库的硬件I2C驱动RX8025T实时时钟芯片
Digital data depth | about software self-control, source code left, no code right
flink同步mysql数据到ES
2. interface (calculator)