1. 创建数据库,表,分区, 及管理数据

1.1 创建数据库

  1. create database `dbname` primary_zone = 'zone1>zone2;zone3';
  2. # 每个租户创建后, 默认都有一个root用户

1.2 创建表

  1. create table `tablename` primary_zone = 'zone1;zone2;zone3'
  2. # 无需制定表空间和存储; 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数据库语法
  • 非分区表, 视为“单分区”表, 即只有一个分区的分区表
  1. # hash/key分区举例(MySQL模式)
  2. create TABLE members_1{
  3. firstname VARCHAR(25) NOT NULL,
  4. lastname varchar(25) not null,
  5. username varchar(16) not null,
  6. email varchar(35),
  7. joined DATE not null
  8. }
  9. PARTITION BY KEY(joined)
  10. PARTITIONS 6;
  1. #range分区举例(MySQL模式)
  2. create table mumber_2{
  3. firstname VARCHAR(25) NOT NULL,
  4. lastname varchar(25) not null,
  5. username varchar(16) not null,
  6. email varchar(35),
  7. joined DATE not null
  8. }
  9. PARTITION BY RANGE(YEAR(joined)){
  10. PARTITION p0 VALUES LESS THAN (1960),
  11. PARTITION p1 VALUES LESS THAN (1970),
  12. PARTITION p2 VALUES LESS THAN (1980),
  13. PARTITION p3 VALUES LESS THAN (1990),
  14. PARTITION p4 VALUES LESS THAN MAXVALUE,
  15. }
  1. #list分区举例(MySQL模式)
  2. CREATE TABLE employees (
  3. id INT NOT NULL,
  4. fname VARCHAR(30),
  5. lname VARCHAR(30),
  6. hired DATE NOT NULL DEFAULT '1970-01-01',
  7. separated DATE NOT NULL DEFAULT '9999-12-31',
  8. job_code INT,
  9. store_id INT
  10. )
  11. PARTITION BY LIST(store_id) (
  12. PARTITION pNorth VALUES IN (3,5,6,9,17),
  13. PARTITION pEast VALUES IN (1,2,10,11,19,20),
  14. PARTITION pWest VALUES IN (4,12,13,14,18),
  15. PARTITION pCentral VALUES IN (7,8,15,16)
  16. );

2. 查看表和分区的分布情况

2.1 表和分区的相关信息储存在这两个系统表中:

  1. 系统表: __all_virtual_meta_table , 关键信息:
    • tenant_id
    • table_id
    • partition_id
    • svr_ip
    • role : 1- leader, 2- follower
  1. 系统表: __all_virtual_table , 关键信息
    • table_id
    • table_name

2.2 查看表和分区的分布情况

2.2.1 查看租户分区的分布情况

image.png

2.2.2 查看各个表的分区分布情况

image.png

3. MVCC-分布式环境下保持全局(跨机)一致性

3.1 MVCC实际案例 - 制造测试数据

  1. create table test1(c1 integer, c2 integer);
  2. insert into test1 values(1,11),(2,22),(3,33),(4,44),(5,55);
  3. #准备2个session(分别位于不同的机器), 并调整session配置:
  4. set @@session.ob_query_timeout=900000000;
  5. set @@session.ob_trx_timeout=900000000;
  6. set @@session.ob_trx_idle_timeout=900000000;
  7. set @@session.autocommit=off;

3.2 MVCC实际案例 - 操作步骤

  1. # session 1, 写
  2. begin;
  3. update test1 set c2 = c2 + 100 where c1 = 1; #未提交状态, 脏数据值为 111
  1. # session 2, 读
  2. select c2 from test1 where c1 = 1; # 读到旧数据 11
  1. # session 1 , 写提交
  2. commit;
  1. # session 2, 再次读
  2. select c2 from test1 where c1 = 1; #读到新值 111

image.png