- 下面示例使用 CREATE TABLE 语句创建订单表 ware 和 cust 表。
在 MySQL 租户里,可以使用 CREATE TABLE AS SELECT 复制表的数据,但是结构并不完全一致,会丢失约束、索引、默认值、分区等信息。使用 CREATE TABLE LIKE 可以复制表结构,但是不包括数据。
- 示例:MySQL租户的 CREATE TABLE 复制表结构和数据的区别
id bigint not null primary KEY
, name varchar(50) not NULL
, gmt_create timestamp not null default current_timestamp
) partition by hash(id) partitions 8;
Query OK, 0 rows affected (0.10 sec)
obclient> insert into t1(id,name) values(1,'A'),(2,'B'),(3,'C');
Query OK, 3 rows affected (0.03 sec)
Records: 3 Duplicates: 0 Warnings: 0
obclient> create table t1_like like t1;
Query OK, 0 rows affected (0.11 sec)
obclient> create table t1_copy as select * from t1;
Query OK, 3 rows affected (0.12 sec)
obclient> show create table t1_like\G
*************************** 1. row ***************************
Table: t1_like
Create Table: CREATE TABLE `t1_like` (
`id` bigint(20) NOT NULL,
`name` varchar(50) NOT NULL,
`gmt_create` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
partition by hash(id) partitions 8
obclient> show create table t1_copy\G
*************************** 1. row ***************************
Table: t1_copy
Create Table: CREATE TABLE `t1_copy` (
`id` bigint(20) DEFAULT NULL,
`name` varchar(50) DEFAULT NULL,
`gmt_create` timestamp NULL DEFAULT NULL
) 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
1 row in set (0.00 sec)
obclient> show create table t1\G
*************************** 1. row ***************************
Table: t1
Create Table: CREATE TABLE `t1` (
`id` bigint(20) NOT NULL,
`name` varchar(50) NOT NULL,
`gmt_create` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) 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
partition by hash(id) partitions 8
1 row in set (0.00 sec)
通常分区对用户的应用是透明的,应用只需要使用 SQL 读写表即可。只有某些场景下,为了提升分区表的查询性能,应用也可以使用 SQL 直接访问某个具体的分区,SQL 语法格式是:
- 示例:通过 SQL 直接访问分区表的分区
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;
+------+--------+--------------+----------+-------------+------------+
| o_id | o_c_id | o_carrier_id | o_ol_cnt | o_all_local | o_entry_d |
+------+--------+--------------+----------+-------------+------------+
1 row in set (0.01 sec)
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;
+---------+-----------+---------------+-----------+---------+----------------+-------------+
| ol_o_id | ol_number | ol_delivery_d | ol_amount | ol_i_id | ol_supply_w_id | ol_quantity |
+---------+-----------+---------------+-----------+---------+----------------+-------------+
| 2100 | 1 | 2020-02-15 | 0.00 | 87133 | 1 | 5 |
| 2100 | 2 | 2020-02-15 | 0.00 | 47413 | 1 | 5 |
| 2100 | 3 | 2020-02-15 | 0.00 | 9115 | 1 | 5 |
| 2100 | 4 | 2020-02-15 | 0.00 | 42985 | 1 | 5 |
| 2100 | 5 | 2020-02-15 | 0.00 | 43621 | 1 | 5 |
| 2100 | 6 | 2020-02-15 | 0.00 | 5787 | 1 | 5 |
| 2100 | 7 | 2020-02-15 | 0.00 | 62576 | 1 | 5 |
| 2100 | 8 | 2020-02-15 | 0.00 | 91592 | 1 | 5 |
| 2100 | 9 | 2020-02-15 | 0.00 | 34452 | 1 | 5 |
| 2100 | 10 | 2020-02-15 | 0.00 | 13792 | 1 | 5 |
| 2100 | 11 | 2020-02-15 | 0.00 | 94326 | 1 | 5 |
+---------+-----------+---------------+-----------+---------+----------------+-------------+
11 rows in set (0.01 sec)
注意:
如果是组合分区,可以访问更细粒度的分区,详细描述请参考“分区路由”章节。
- 示例:使用 SQL Hint 实现读写分离。
弱一致读的 Hint 语法是 /*+ read_consistency(weak) */ 。通常的读默认是强一致性读,就不用 Hint 了。
复制表是分布式数据库 OceanBase 的高级优化手段。
通常 OceanBase 集群是三副本架构,默认每个表的每个分区在 OceanBase 中会有三个副本数据,角色上分为一个主副本(Leader 副本)和两个备副本(Follower副本),默认提供读写服务的是主副本。
复制表的语法是在 CREATE TABLE 语句后增加 DUPLICATE_SCOPE
选项。
- 示例:创建复制表。
create table item (i_id int
, i_name varchar(24)
, i_price decimal(5,2)
, i_data varchar(50)
, i_im_id int
, primary key(i_id)) COMPRESS FOR QUERY pctfree=0 BLOCK_SIZE=16384