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

在 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. partition by hash(id) partitions 8
  22. obclient> show create table t1_copy\G
  23. *************************** 1. row ***************************
  24. Table: t1_copy
  25. Create Table: CREATE TABLE `t1_copy` (
  26. `id` bigint(20) DEFAULT NULL,
  27. `name` varchar(50) DEFAULT NULL,
  28. `gmt_create` timestamp NULL DEFAULT NULL
  29. ) 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
  30. 1 row in set (0.00 sec)
  31. obclient> show create table t1\G
  32. *************************** 1. row ***************************
  33. Table: t1
  34. Create Table: CREATE TABLE `t1` (
  35. `id` bigint(20) NOT NULL,
  36. `name` varchar(50) NOT NULL,
  37. `gmt_create` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  38. PRIMARY KEY (`id`)
  39. ) 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
  40. partition by hash(id) partitions 8
  41. 1 row in set (0.00 sec)

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

  • 示例:通过 SQL 直接访问分区表的分区
  1. obclient> select o_id,o_c_id,o_carrier_id,o_ol_cnt,o_all_local,o_entry_d from ordr partition (p1) where o_w_id=1 and o_d_id=2 and o_id=2100;
  2. +------+--------+--------------+----------+-------------+------------+
  3. | o_id | o_c_id | o_carrier_id | o_ol_cnt | o_all_local | o_entry_d |
  4. +------+--------+--------------+----------+-------------+------------+
  5. 1 row in set (0.01 sec)
  6. 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;
  7. +---------+-----------+---------------+-----------+---------+----------------+-------------+
  8. | ol_o_id | ol_number | ol_delivery_d | ol_amount | ol_i_id | ol_supply_w_id | ol_quantity |
  9. +---------+-----------+---------------+-----------+---------+----------------+-------------+
  10. | 2100 | 1 | 2020-02-15 | 0.00 | 87133 | 1 | 5 |
  11. | 2100 | 2 | 2020-02-15 | 0.00 | 47413 | 1 | 5 |
  12. | 2100 | 3 | 2020-02-15 | 0.00 | 9115 | 1 | 5 |
  13. | 2100 | 4 | 2020-02-15 | 0.00 | 42985 | 1 | 5 |
  14. | 2100 | 5 | 2020-02-15 | 0.00 | 43621 | 1 | 5 |
  15. | 2100 | 6 | 2020-02-15 | 0.00 | 5787 | 1 | 5 |
  16. | 2100 | 7 | 2020-02-15 | 0.00 | 62576 | 1 | 5 |
  17. | 2100 | 8 | 2020-02-15 | 0.00 | 91592 | 1 | 5 |
  18. | 2100 | 9 | 2020-02-15 | 0.00 | 34452 | 1 | 5 |
  19. | 2100 | 10 | 2020-02-15 | 0.00 | 13792 | 1 | 5 |
  20. | 2100 | 11 | 2020-02-15 | 0.00 | 94326 | 1 | 5 |
  21. +---------+-----------+---------------+-----------+---------+----------------+-------------+
  22. 11 rows in set (0.01 sec)

注意:

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

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

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

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

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

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

  • 示例:创建复制表。
  1. 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)) COMPRESS FOR QUERY pctfree=0 BLOCK_SIZE=16384