例如数据是序列递增,则有可能数据全部都集中一个region上 ,或者集中在一个节点
shard_row_id_bits 和 pre_split_regions
create table t(c int) shard_row_id_bits = 4;
alter table t shard_row_id_bits =4 ;
auto_random
create table t(a bigint primary key auto_random,b varchar(255));
split table table_name index idx_name between () and () regions n
tidb_scatter_region: 作用域是global,默认值是off. 它的作用是预先在不同的节点上分配空region
步骤1: TiDB Dashboard流量可视化页面,观测写流量的情况
步骤2: Dashboard慢查询页面& SQL语句分析,确认对应数据库对象DML的操作类型
步骤3: show create table …
步骤4: 热点打散
insert
数据类型 | 操作类型 | 自增主键 | 打散方式 |
---|---|---|---|
行数据 | insert | 无 | alter table tbl_name shared_row_id_bits=n; |
行数据 | insert | 有 | alter table t modify a bigint auto_random(5); v4.0.13起支持 |
update/delete
数据类型 | 操作类型 | 打散方式 |
---|---|---|
行数据 | update/delete | 如下 |
索引
数据类型 | 打散方式 |
---|---|
索引数据 | 如下 |
步骤1: TiDB Dashboard 流量可视化页面观测读流量的情况
解决方案 load base ,它会自动打散,通常不需要人工干预
mysql> set config tikv split.qps-threshold=4000;
Query OK, 0 rows affected (0.02 sec)
mysql> show config where name like '%qps-threshold%';
+------+---------------------+---------------------+-------+
| Type | Instance | Name | Value |
+------+---------------------+---------------------+-------+
| tikv | 192.168.16.13:20160 | split.qps-threshold | 4000 |
+------+---------------------+---------------------+-------+
1 row in set (0.01 sec)
curl -X POST "http://192.168.16.13:20180/config" -H "accept: application/json" -d '{"split.qps-threshold":"3000"}'
# 20180 : 修改是tikv参数,这个对应的是是TiKV的status portcurl -X POST "http://192.168.16.13:20180/config" -H "accept: application/json" -d '{"split.byte-threshold":"30"}'
# 20180 : 修改是tikv参数,这个对应的是是TiKV的status port[root@tidb2 ~]# curl -X POST "http://192.168.16.13:20180/config" -H "accept: application/json" -d '{"split.byte-threshold":"40"}'
mysql> show config where name like '%byte-threshold%';
+------+---------------------+----------------------+-------+
| Type | Instance | Name | Value |
+------+---------------------+----------------------+-------+
| tikv | 192.168.16.13:20160 | split.byte-threshold | 40 |
+------+---------------------+----------------------+-------+
1 row in set (0.02 sec)
顺序增长属性字段索引范围扫描
方式1
qps超过3000或者流量超过30M/s 自动分裂 自动打散
curl -X POST "http://192.168.16.13:20180/config" -H "accept: application/json" -d '{"split.qps-threshold":"3000"}'
# 20180 : 修改是tikv参数,这个对应的是是TiKV的status portcurl -X POST "http://192.168.16.13:20180/config" -H "accept: application/json" -d '{"split.byte-threshold":"30"}'
# 20180 : 修改是tikv参数,这个对应的是是TiKV的status port方式2
使用sysbench 工具模拟数据的并发插入,并采用auto_random选项来尝试打散热点
1、 安装sysbench
wget https://codeload.github.com/akopytov/sysbench/tar.gz/1.0.17
mv 1.0.17 sysbench-1.0.17.tar.gz
tar -zxvf sysbench-1.0.17.tar.gz -C /usr/local/
yum install automake libtool –y
cd /usr/local/sysbench-1.0.17/
[root@tidb2 sysbench-1.0.17]# ./autogen.sh
[root@tidb2 sysbench-1.0.17]# ./configure
[root@tidb2 sysbench-1.0.17]# make -j
[root@tidb2 sysbench-1.0.17]# make install
[root@tidb2 sysbench-1.0.17]# sysbench --version
sysbench 1.0.17
2、 配置sysbench 配置文件
[root@tidb2 ~]# more config_new
mysql-host=192.168.16.13
mysql-port=4000
mysql-user=root
mysql-db=sbtest
time=600
threads=32
report-interval=10
db-driver=mysql
3、创建sysbench测试数据库sbtest
[root@tidb2 ~]# mysql -uroot -p -h192.168.16.13 -P 4000
mysql> create database sbtest;
4、采用sysbench模拟并发批量写入数据,此过程时间较长(15 min)左右,如下:
[root@tidb2 ~]# sysbench /usr/local/share/sysbench/oltp_common.lua --config-file=config_new --tables=1 --table-size=20000000 --mysql-password=Aa123ab! prepare
5、实验用的表是自增主键的聚簇索引表
mysql> show create table sbtest1;
+---------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+---------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| sbtest1 | CREATE TABLE `sbtest1` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`k` int(11) NOT NULL DEFAULT '0',
`c` char(120) NOT NULL DEFAULT '',
`pad` char(60) NOT NULL DEFAULT '',
PRIMARY KEY (`id`) /*T![clustered_index] CLUSTERED */
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin AUTO_INCREMENT=2000001 |
+---------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.11 sec)
6、 查看热力图,流量可视化
7、修改测试脚本,将自增主键调整为auto_random
mysql> show create table sbtest1;
+---------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+---------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| sbtest1 | CREATE TABLE `sbtest1` (
`id` int(11) NOT NULL AUTO_RANDOM(5),
`k` int(11) NOT NULL DEFAULT '0',
`c` char(120) NOT NULL DEFAULT '',
`pad` char(60) NOT NULL DEFAULT '',
PRIMARY KEY (`id`) /*T![clustered_index] CLUSTERED */
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin |
+---------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.11 sec)
8、清理之前实验数据
[root@tidb2 ~]# sysbench /usr/local/share/sysbench/oltp_common.lua --config-file=config_new --mysql-password=Aa123ab! cleanup
9、再次采用sysbench模拟并发并批量写入数据
[root@tidb2 ~]# sysbench /usr/local/share/sysbench/oltp_common.lua --config-file=config_new --tables=1 --table-size=20000000 --mysql-password=Aa123ab! prepare