• 下面示例使用 CREATE TABLE 语句创建订单表 ware 和 cust 表。

说明

由于 ALTER TABLE 语法不支持后期增加主键,所以需要在建表的时候设置主键。

在 MySQL 租户里,可以使用 CREATE TABLE AS SELECT 复制表的数据,但是结构并不完全一致,会丢失约束、索引、默认值、分区等信息。使用 CREATE TABLE LIKE 可以复制表结构,但是不包括数据。

  • 示例:MySQL 租户的 CREATE TABLE 复制表结构和数据的区别
  1. id bigint not null primary KEY
  2. , name varchar(50) not NULL
  3. , gmt_create timestamp not null default current_timestamp
  4. ) partition by hash(id) partitions 8;
  5. Query OK, 0 rows affected (0.10 sec)
  6. obclient> insert into t1(id,name) values(1,'A'),(2,'B'),(3,'C');
  7. Query OK, 3 rows affected (0.03 sec)
  8. Records: 3 Duplicates: 0 Warnings: 0
  9. obclient> create table t1_like like t1;
  10. Query OK, 0 rows affected (0.11 sec)
  11. obclient> create table t1_copy as select * from t1;
  12. Query OK, 3 rows affected (0.12 sec)
  13. obclient> show create table t1_like\G
  14. *************************** 1. row ***************************
  15. Table: t1_like
  16. Create Table: CREATE TABLE `t1_like` (
  17. `id` bigint(20) NOT NULL,
  18. `name` varchar(50) NOT NULL,
  19. `gmt_create` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  20. PRIMARY KEY (`id`)
  21. ) DEFAULT CHARSET = utf8mb4 ROW_FORMAT = DYNAMIC COMPRESSION = 'zstd_1.0' REPLICA_NUM = 3 BLOCK_SIZE = 16384 USE_BLOOM_FILTER = FALSE TABLET_SIZE = 134217728 PCTFREE = 10 PROGRESSIVE_MERGE_NUM = 2
  22. partition by hash(id)
  23. (partition p0,
  24. partition p1,
  25. partition p2,
  26. partition p4,
  27. partition p5,
  28. partition p7)
  29. 1 row in set (0.02 sec)
  30. obclient> show create table t1_copy\G
  31. *************************** 1. row ***************************
  32. Table: t1_copy
  33. Create Table: CREATE TABLE `t1_copy` (
  34. `id` bigint(20) DEFAULT NULL,
  35. `name` varchar(50) DEFAULT NULL,
  36. `gmt_create` timestamp NULL DEFAULT NULL
  37. ) DEFAULT CHARSET = utf8mb4 ROW_FORMAT = DYNAMIC COMPRESSION = 'zstd_1.0' REPLICA_NUM = 3 BLOCK_SIZE = 16384 USE_BLOOM_FILTER = FALSE TABLET_SIZE = 134217728 PCTFREE = 10
  38. 1 row in set (0.00 sec)
  39. obclient> show create table t1\G
  40. *************************** 1. row ***************************
  41. Table: t1
  42. Create Table: CREATE TABLE `t1` (
  43. `id` bigint(20) NOT NULL,
  44. `name` varchar(50) NOT NULL,
  45. `gmt_create` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  46. PRIMARY KEY (`id`)
  47. ) DEFAULT CHARSET = utf8mb4 ROW_FORMAT = DYNAMIC COMPRESSION = 'zstd_1.0' REPLICA_NUM = 3 BLOCK_SIZE = 16384 USE_BLOOM_FILTER = FALSE TABLET_SIZE = 134217728 PCTFREE = 10 PROGRESSIVE_MERGE_NUM = 2
  48. partition by hash(id)
  49. (partition p0,
  50. partition p1,
  51. partition p2,
  52. partition p3,
  53. partition p4,
  54. partition p5,
  55. partition p6,
  56. partition p7)
  57. 1 row in set (0.01 sec)

通常分区对用户的应用是透明的,应用只需要使用 SQL 读写表即可。只有某些场景下,为了提升分区表的查询性能,应用也可以使用 SQL 直接访问某个具体的分区,SQL 语法格式是:

  1. +------+--------+--------------+----------+-------------+------------+
  2. | o_id | o_c_id | o_carrier_id | o_ol_cnt | o_all_local | o_entry_d |
  3. +------+--------+--------------+----------+-------------+------------+
  4. | 2100 | 8 | 8 | 11 | 1 | 2020-02-15 |
  5. +------+--------+--------------+----------+-------------+------------+
  6. 1 row in set (0.01 sec)
  7. obclient> select ol_o_id, ol_number,ol_delivery_d,ol_amount,ol_i_id,ol_supply_w_id,ol_quantity from ordl partition (p1) where ol_w_id=1 and ol_d_id=2 and ol_o_id=2100;
  8. +---------+-----------+---------------+-----------+---------+----------------+-------------+
  9. | ol_o_id | ol_number | ol_delivery_d | ol_amount | ol_i_id | ol_supply_w_id | ol_quantity |
  10. +---------+-----------+---------------+-----------+---------+----------------+-------------+
  11. | 2100 | 1 | 2020-02-15 | 0.00 | 87133 | 1 | 5 |
  12. | 2100 | 2 | 2020-02-15 | 0.00 | 47413 | 1 | 5 |
  13. | 2100 | 3 | 2020-02-15 | 0.00 | 9115 | 1 | 5 |
  14. | 2100 | 4 | 2020-02-15 | 0.00 | 42985 | 1 | 5 |
  15. | 2100 | 5 | 2020-02-15 | 0.00 | 43621 | 1 | 5 |
  16. | 2100 | 6 | 2020-02-15 | 0.00 | 5787 | 1 | 5 |
  17. | 2100 | 7 | 2020-02-15 | 0.00 | 62576 | 1 | 5 |
  18. | 2100 | 8 | 2020-02-15 | 0.00 | 91592 | 1 | 5 |
  19. | 2100 | 9 | 2020-02-15 | 0.00 | 34452 | 1 | 5 |
  20. | 2100 | 10 | 2020-02-15 | 0.00 | 13792 | 1 | 5 |
  21. | 2100 | 11 | 2020-02-15 | 0.00 | 94326 | 1 | 5 |
  22. +---------+-----------+---------------+-----------+---------+----------------+-------------+
  23. 11 rows in set (0.01 sec)

说明

如果是组合分区,可以访问更细粒度的分区,详细描述请参考“分区路由”章节。

  • 示例:使用 SQL Hint 实现读写分离。

弱一致读的 Hint 语法是 /*+ read_consistency(weak) */ 。通常的读默认是强一致性读,就不用 Hint 了。

复制表是分布式数据库 OceanBase 的高级优化手段。

通常 OceanBase 集群是三副本架构,默认每个表的每个分区在 OceanBase 中会有三个副本数据,角色上分为一个主副本(Leader 副本)和两个备副本(Follower副本),默认提供读写服务的是主副本。

复制表的语法是在 CREATE TABLE 语句后增加 DUPLICATE_SCOPE 选项。

  • 示例:创建复制表。
  1. obclient> create table item (i_id int
  2. , i_name varchar(24)
  3. , i_price decimal(5,2)
  4. , i_data varchar(50)
  5. , i_im_id int
  6. , primary key(i_id)) pctfree=0 BLOCK_SIZE=16384
  7. duplicate_scope='cluster' locality='F@zone1,F@zone2,R{all_server}@zone3' primary_zone='zone1';