实战教程第三章3.7:(高级)如何使用 OceanBase 表分组
表分组简介
表分组(TABLE GROUP
)是 OceanBase 作为分布式数据库的一个特色功能。 表分组是表的属性,会影响多个表的分区在 OceanBase 机器上的分布特征。
不同表的分区有可能分布在不同的节点上,当两个表做表连接查询时,OceanBase 会跨节点请求数据,执行时间就跟节点间请求延时有关。在 SQL 调优时,OceanBase 建议对业务上关系密切的表,设置相同的表分组。OceanBase 对于同一个表分组中的表的同号分区会管理为一个分区组。同一个分区组中的分区,OceanBase 会尽可能的分配到同一个节点内部,这样就可以规避跨节点的请求。
创建表分组
创建表分组时,首先要规划好表分组的用途。如果是用于普通表的属性,表分组就不用分区;如果是用于分区表的属性,表分组就要指定分区策略,并且要跟分区表的分区策略保持一致。
示例:创建表分组和查看表分组。
create tablegroup tpcc_group partition by hash partitions 6 ;
MySQL [test]> show tablegroups;
+-----------------+------------+---------------+
| Tablegroup_name | Table_name | Database_name |
+-----------------+------------+---------------+
| oceanbase | NULL | NULL |
| tpcc_group | NULL | NULL |
+-----------------+------------+---------------+
2 rows in set (0.004 sec)
MySQL [test]> show create tablegroup tpcc_group;
+------------+-------------------------------------------------------------------------------------------------+
| Tablegroup | Create Tablegroup |
+------------+-------------------------------------------------------------------------------------------------+
| tpcc_group | CREATE TABLEGROUP IF NOT EXISTS `tpcc_group` BINDING = FALSE
partition by hash partitions 6
|
+------------+-------------------------------------------------------------------------------------------------+
1 row in set (0.001 sec)
查看表分组下的表的语句是:show tablegroups ;
。
有了表分组后,在建表时就可以指定表分组。
create table ordr (
o_w_id int
, o_d_id int
, o_id int
, o_c_id int
, o_carrier_id int
, o_ol_cnt int
, o_all_local int
, o_entry_d date
, index iordr(o_w_id, o_d_id, o_c_id, o_id) local
, primary key ( o_w_id, o_d_id, o_id )
)tablegroup tpcc_group partition by hash(o_w_id) partitions 6;
create table ordl (
ol_w_id int
, ol_d_id int
, ol_o_id int
, ol_number int
, ol_delivery_d date
, ol_amount decimal(6, 2)
, ol_i_id int
, ol_supply_w_id int
, ol_quantity int
, ol_dist_info char(24)
, primary key (ol_w_id, ol_d_id, ol_o_id, ol_number )
)tablegroup tpcc_group partition by hash(ol_w_id) partitions 6;
MySQL [test]> show tablegroups;
+-----------------+------------+---------------+
| Tablegroup_name | Table_name | Database_name |
+-----------------+------------+---------------+
| oceanbase | NULL | NULL |
| tpcc_group | ordl | test |
| tpcc_group | ordr | test |
+-----------------+------------+---------------+
3 rows in set (0.004 sec)
也可以后期将一个表加入到表分组,使用语句:alter tablegroup ... add
。 将表从表分组中移出时,使用语句:alter table ... tablegroup = '';
MySQL [test]> alter table ordl tablegroup = '';
Query OK, 0 rows affected (0.148 sec)
MySQL [test]> alter table ordr tablegroup = '';
Query OK, 0 rows affected (0.018 sec)
MySQL [test]> show tablegroups;
+-----------------+------------+---------------+
| Tablegroup_name | Table_name | Database_name |
+-----------------+------------+---------------+
| oceanbase | NULL | NULL |
| tpcc_group | NULL | NULL |
+-----------------+------------+---------------+
2 rows in set (0.004 sec)
MySQL [test]> alter tablegroup tpcc_group add ordl , ordr ;
Query OK, 0 rows affected (0.016 sec)
MySQL [test]> show tablegroups;
+-----------------+------------+---------------+
| Tablegroup_name | Table_name | Database_name |
+-----------------+------------+---------------+
| oceanbase | NULL | NULL |
| tpcc_group | ordl | test |
| tpcc_group | ordr | test |
+-----------------+------------+---------------+
3 rows in set (0.004 sec)
附录: