TiDB-表结构设计实验
TiDB 数据库表结构设计(实验)
概述
在本课练习中,您将连接到 TiDB 数据库,并创建一个非聚簇表和聚簇索引表,并查看每个 Region 的
key 范围。
环境
练习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
查看每个 Region 的 START_KEY 和 END_KEY 和每个 Region 的 WRITTEN_BYTES 数据
show table noncluster_order regions;
插入 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;
练习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
查看 table Reion 的数量和 Region 的 START_KEY 和 END_KEY
show table cluster_order regions;
只有一个region
将 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 tablecluster_order
region step value should more than 1000, step 250 is invalid
查看 table Reion 的数量和 Region 的 START_KEY 和 END_KEY,记录每个 Region 的 WRITTEN_BYTES 数据
show table cluster_order regions;
插入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;
再次查看每个 Region 的 WRITTEN_BYTES 数据变化
show table cluster_order regions;
从WRITTEN_BYTES 列数据变化情况看,怎么数据都只插入到一个region中???
思考为什么只有 2 个 Region 的 WRITTEN_BYTES 有变化
select id,id>>58 from cluster_order;
批量插入时,同一个insert语句中,其内的主键值是连续。
清理实验数据
drop table cluster_order;
drop table noncluster_order;
附录
问题?
为什么执行 show table cluster_order regions;
命令得到写流量(WRITTEN_BYTES)不同?过一会后变成了0
标签:
TiDB
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)