TiDB表结构设计实验

TiDB 数据库表结构设计(实验)

概述

在本课练习中,您将连接到 TiDB 数据库,并创建一个非聚簇表和聚簇索引表,并查看每个 Region 的
key 范围。

环境

image-20220103214013111

练习1-创建一个非聚簇表并查看每个 Region 的 Key 范围

创建一张非聚簇表 noncluster_order

-- mysql -uroot -P4000 -h2db4 devdb
use devdb;

CREATE TABLE noncluster_order (
id bigint(20) unsigned AUTO_INCREMENT NOT NULL ,
code varchar(30) NOT NULL,
order_no varchar(200) NOT NULL DEFAULT '',
order_status int(4) NOT NULL,
cancle_flag int(4) DEFAULT NULL,
create_user varchar(50) DEFAULT NULL,
update_user varchar(50) DEFAULT NULL,
create_time datetime DEFAULT NULL,
update_time datetime DEFAULT NULL,
PRIMARY KEY (id) NONCLUSTERED
) ENGINE=InnoDB SHARD_ROW_ID_BITS=4 PRE_SPLIT_REGIONS=3;
-- 预分配 2^3 = 8 个region

查看表结构

show create table noncluster_order\G

image-20220103194203909

查看每个 Region 的 START_KEY 和 END_KEY 和每个 Region 的 WRITTEN_BYTES 数据

show table noncluster_order regions;

image-20220103214046598

插入 10 行数据

insert into noncluster_order(code, order_no, order_status, create_user, create_time) values
(concat('CODE_', LPAD(round((rand()*1000)), 5, 0)), uuid(), round((rand()*100)), concat('USERID_',
LPAD(round((rand()*100)), 4, 0)), now());

select count(*) cnt from noncluster_order;

查看每个 Region 的 START_KEY 和 END_KEY 和每个 Region 的 WRITTEN_BYTES 数据

show table noncluster_order regions;

image-20220103213820588

练习2-创建一个聚簇索引的表并查看每个 Region 的 Key 的范围

创建聚簇表 cluster_order

use devdb;

CREATE TABLE cluster_order (
id bigint(20) unsigned AUTO_RANDOM NOT NULL ,
code varchar(30) NOT NULL,
order_no varchar(200) NOT NULL DEFAULT '',
order_status int(4) NOT NULL,
cancle_flag int(4) DEFAULT NULL,
create_user varchar(50) DEFAULT NULL,
update_user varchar(50) DEFAULT NULL,
create_time datetime DEFAULT NULL,
update_time datetime DEFAULT NULL,
PRIMARY KEY (id) CLUSTERED
) ENGINE=InnoDB;

查看表 cluster_order 的表结构

show create table cluster_order\G

image-20220103214134410

查看 table Reion 的数量和 Region 的 START_KEY 和 END_KEY

show table cluster_order regions;

只有一个region

image-20220103214152943

将 cluster_order 的 Region 进行手动 Split

-- maxInt64 :9223372036854775807 均匀切分16个region
split table cluster_order between (0) and (9223372036854775807) regions 16;

-- 
split table cluster_order between (0) and (8000) regions 8;

注意:

mysql> split table cluster_order between (0) and (2000) regions 8;
ERROR 1105 (HY000): Split table cluster_order region step value should more than 1000, step 250 is invalid

image-20220103195624713

image-20220103214446115

查看 table Reion 的数量和 Region 的 START_KEY 和 END_KEY,记录每个 Region 的 WRITTEN_BYTES 数据

show table cluster_order regions;

image-20220103195637354

image-20220103214508341

插入10 行数据

select count(*) cnt from cluster_order;

-- 每次批量插入5行
insert into cluster_order(code, order_no, order_status, create_user, create_time) values (concat('CODE_',
LPAD(round((rand()*1000)), 5, 0)), uuid(), round((rand()*100)), concat('USERID_', LPAD(round((rand()*100)), 4, 0)),
now()), (concat('CODE_', LPAD(round((rand()*1000)), 5, 0)), uuid(), round((rand()*100)), concat('USERID_',
LPAD(round((rand()*100)), 4, 0)), now()), (concat('CODE_', LPAD(round((rand()*1000)), 5, 0)), uuid(),
round((rand()*100)), concat('USERID_', LPAD(round((rand()*100)), 4, 0)), now()), (concat('CODE_',
LPAD(round((rand()*1000)), 5, 0)), uuid(), round((rand()*100)), concat('USERID_', LPAD(round((rand()*100)), 4, 0)),
now()), (concat('CODE_', LPAD(round((rand()*1000)), 5, 0)), uuid(), round((rand()*100)), concat('USERID_',
LPAD(round((rand()*100)), 4, 0)), now());

select count(*) cnt from cluster_order;

image-20220103195657824

再次查看每个 Region 的 WRITTEN_BYTES 数据变化

show table cluster_order regions;

image-20220103195723697

从WRITTEN_BYTES 列数据变化情况看,怎么数据都只插入到一个region中???

image-20220103214612059

思考为什么只有 2 个 Region 的 WRITTEN_BYTES 有变化

select id,id>>58 from cluster_order;

批量插入时,同一个insert语句中,其内的主键值是连续。

image-20220103195914907

清理实验数据

drop table cluster_order;
drop table noncluster_order;

附录

问题?

为什么执行 show table cluster_order regions;命令得到写流量(WRITTEN_BYTES)不同?过一会后变成了0

image-20220103220325157

原文地址:https://www.cnblogs.com/binliubiao/p/15760710.html