Atlas 分表功能

分表原因

1.数据过多,访问缓慢

2.创建索引时重新排序,创建缓慢,并且占用大量的磁盘空间

分表方式

1.根据数据范围分表

2.根据取模的方式(取余数)

Atlas 分表

分表思路

1.确定分表的库,表,字段
2.确定分表的数量
3.分表的名字,stu_0,stu_1,stu_2
4.配置文件配置分表的规则
5.测试

配置 Atlas

[root@db03 ~]# vim /usr/local/mysql-proxy/conf/test.cnf
#分表设置,此例中school为库名,stu为表名,id为分表字段,3为子表数量,可设置多项,以逗号分隔,若不分表则不需要设置该项
tables = school.stu.id.3



# 重启 Atlas
[root@db03 ~]# /usr/local/mysql-proxy/bin/mysql-proxyd test restart
OK: MySQL-Proxy of test is stopped
OK: MySQL-Proxy of test is started

创建原表

# 连接Atlas负载数据库
[root@db03 ~]# mysql -umha -pmha -h127.0.0.1 -P 1234

# 创建数据库
mysql> create database school;
Query OK, 1 row affected (0.00 sec)

# 创建数据表
mysql> use school
Database changed
mysql> create table stu(id int,name varchar(10));
Query OK, 0 rows affected (0.13 sec)

创建分表

# 创建分表,表名要保证如下格式,否则报错
mysql> create table stu_0 like stu;
Query OK, 0 rows affected (0.08 sec)

mysql> create table stu_1 like stu;
Query OK, 0 rows affected (0.03 sec)

mysql> create table stu_2 like stu;
Query OK, 0 rows affected (0.03 sec)

数据测试

# 插入数据
mysql> insert into stu values(1,'wqh');
Query OK, 1 row affected (0.01 sec)

mysql> insert into stu values(2,'wqh');
Query OK, 1 row affected (0.00 sec)

mysql> insert into stu values(3,'wqh3');
Query OK, 1 row affected (0.01 sec)

mysql> insert into stu values(11,'wqh3');
Query OK, 1 row affected (0.01 sec)

mysql> insert into stu values(12,'wqh3');
Query OK, 1 row affected (0.00 sec)

mysql> insert into stu values(13,'wqh3');
Query OK, 1 row affected (0.02 sec)

# 查看数据
mysql> select * from stu;
Empty set (0.00 sec)

mysql> select * from stu_0;
+------+------+
| id   | name |
+------+------+
|    3 | wqh3 |
|   12 | wqh3 |
+------+------+
2 rows in set (0.03 sec)

mysql> select * from stu_1;
+------+------+
| id   | name |
+------+------+
|    1 | wqh  |
|   13 | wqh3 |
+------+------+
2 rows in set (0.00 sec)

mysql> select * from stu_2;
+------+------+
| id   | name |
+------+------+
|    2 | wqh  |
|   11 | wqh3 |
+------+------+
2 rows in set (0.00 sec)

# 如果想查看原表数据,需要加 where条件
mysql> select * from stu where id=1;
+------+------+
| id   | name |
+------+------+
|    1 | wqh  |
+------+------+
1 rows in set (0.00 sec)
posted @ 2020-07-29 21:53  拨云见日z  阅读(306)  评论(0编辑  收藏  举报