1. 创建数据库,表,分区, 及管理数据
1.1 创建数据库
create database `dbname` primary_zone = 'zone1>zone2;zone3';
# 每个租户创建后, 默认都有一个root用户
1.2 创建表
create table `tablename` primary_zone = 'zone1;zone2;zone3'
# 无需制定表空间和存储; OceanBase在数据文件中自动分配并管理存储空间
1.3 分区方式(以MySQL语法为例)
partition by hash(<integer column>) partitions <partition number>;
partition by key(<column list>) partitions <partition number>;
partition by range <range definition>;
partition by list <list definition>;
支持二级分区
- MySQL模式和Oracle模式下, 分别兼容MySQL数据库语法和Oracle数据库语法
- 非分区表, 视为“单分区”表, 即只有一个分区的分区表
# hash/key分区举例(MySQL模式)
create TABLE members_1{
firstname VARCHAR(25) NOT NULL,
lastname varchar(25) not null,
username varchar(16) not null,
email varchar(35),
joined DATE not null
}
PARTITION BY KEY(joined)
PARTITIONS 6;
#range分区举例(MySQL模式)
create table mumber_2{
firstname VARCHAR(25) NOT NULL,
lastname varchar(25) not null,
username varchar(16) not null,
email varchar(35),
joined DATE not null
}
PARTITION BY RANGE(YEAR(joined)){
PARTITION p0 VALUES LESS THAN (1960),
PARTITION p1 VALUES LESS THAN (1970),
PARTITION p2 VALUES LESS THAN (1980),
PARTITION p3 VALUES LESS THAN (1990),
PARTITION p4 VALUES LESS THAN MAXVALUE,
}
#list分区举例(MySQL模式)
CREATE TABLE employees (
id INT NOT NULL,
fname VARCHAR(30),
lname VARCHAR(30),
hired DATE NOT NULL DEFAULT '1970-01-01',
separated DATE NOT NULL DEFAULT '9999-12-31',
job_code INT,
store_id INT
)
PARTITION BY LIST(store_id) (
PARTITION pNorth VALUES IN (3,5,6,9,17),
PARTITION pEast VALUES IN (1,2,10,11,19,20),
PARTITION pWest VALUES IN (4,12,13,14,18),
PARTITION pCentral VALUES IN (7,8,15,16)
);
2. 查看表和分区的分布情况
2.1 表和分区的相关信息储存在这两个系统表中:
- 系统表:
__all_virtual_meta_table
, 关键信息:
tenant_id
table_id
partition_id
svr_ip
role : 1- leader, 2- follower
- 系统表:
__all_virtual_table
, 关键信息
table_id
table_name
2.2 查看表和分区的分布情况
2.2.1 查看租户分区的分布情况
2.2.2 查看各个表的分区分布情况
3. MVCC-分布式环境下保持全局(跨机)一致性
3.1 MVCC实际案例 - 制造测试数据
create table test1(c1 integer, c2 integer);
insert into test1 values(1,11),(2,22),(3,33),(4,44),(5,55);
#准备2个session(分别位于不同的机器), 并调整session配置:
set @@session.ob_query_timeout=900000000;
set @@session.ob_trx_timeout=900000000;
set @@session.ob_trx_idle_timeout=900000000;
set @@session.autocommit=off;
3.2 MVCC实际案例 - 操作步骤
# session 1, 写
begin;
update test1 set c2 = c2 + 100 where c1 = 1; #未提交状态, 脏数据值为 111
# session 2, 读
select c2 from test1 where c1 = 1; # 读到旧数据 11
# session 1 , 写提交
commit;
# session 2, 再次读
select c2 from test1 where c1 = 1; #读到新值 111