垂直拆分:
顾名思义是将表垂直着给拆掉,即:(下面是省略掉字段的一个表)
+--------+---------+--------+--------+-------+---------+---------+--------+-----+-------------+--------+-----------+------+--------+
| userid | groupid | areaid | amount | point | modelid | message | islock | vip | overduedate | siteid | connectid | from | mobile |
+--------+---------+--------+--------+-------+---------+---------+--------+-----+-------------+--------+-----------+------+--------+
| 1 | 5 | 0 | 0.00 | 50 | 10 | 0 | 0 | 1 | 0 | 1 | | | |
+--------+---------+--------+--------+-------+---------+---------+--------+-----+-------------+--------+-----------+------+--------+
比如说一个用户表有很多的属性,关联了很多数据,如果放到同一个表里面的话查询是方便了,但是效率不行,所以这里就是用到了垂直拆表:
拆成如下:
+--------+---------+--------+--------+-------+---------+---------+
| userid | groupid | areaid | amount | point | modelid | message |
+--------+---------+--------+--------+-------+---------+---------+
| 1 | 5 | 0 | 0.00 | 50 | 10 | 0 |
+--------+---------+--------+--------+-------+---------+---------+
和
+--------+--------+-----+-------------+--------+-----------+------+--------+
| userid | islock | vip | overduedate | siteid | connectid | from | mobile |
+--------+--------+-----+-------------+--------+-----------+------+--------+
| 1 | 0 | 1 | 0 | 1 | | | |
+--------+--------+-----+-------------+--------+-----------+------+--------+
把常用的字段放一个表,不常用的放一个表
把字段比较大的比如text的字段拆出来放一个表里面
使用的话是根据具体业务来拆,查询时使用多表联查,可以再配合redis存储
水平拆分:
顾名思义是将表数据水平的拆掉,即:
表0 user_0
+--------+---------+--------+--------+-------+---------+---------+
| userid | groupid | areaid | amount | point | modelid | message |
+--------+---------+--------+--------+-------+---------+---------+
| 1 | 5 | 0 | 0.00 | 50 | 10 | 0 |
+--------+---------+--------+--------+-------+---------+---------+
表1 user_1
+--------+---------+--------+--------+-------+---------+---------+
| userid | groupid | areaid | amount | point | modelid | message |
+--------+---------+--------+--------+-------+---------+---------+
| 1 | 5 | 0 | 0.00 | 50 | 10 | 0 |
+--------+---------+--------+--------+-------+---------+---------+
表2 user_2
+--------+---------+--------+--------+-------+---------+---------+
| userid | groupid | areaid | amount | point | modelid | message |
+--------+---------+--------+--------+-------+---------+---------+
| 1 | 5 | 0 | 0.00 | 50 | 10 | 0 |
+--------+---------+--------+--------+-------+---------+---------+
.
.
.
表9 user_9
+--------+---------+--------+--------+-------+---------+---------+
| userid | groupid | areaid | amount | point | modelid | message |
+--------+---------+--------+--------+-------+---------+---------+
| 1 | 5 | 0 | 0.00 | 50 | 10 | 0 |
+--------+---------+--------+--------+-------+---------+---------+
当然这里不一定要0-9一共10张表来表示,通常情况下使用"取模"的形式来将数据进行表的存储,如果用4张表那么就是id%4 结果会是0,1,2,3四种,user_0,user_1,user_2,user_3就够了,具体这里就要看表的数据量了.
对水平分表的数据进行CURD操作也是一样,之前根据id取模算出当前数据在哪张表中,然后再select * from user_"取的模",这里有人要问了,我添加数据之前都不知道数据库的id,更不能进行取模了,怎么找到对应的表添加啊,对了,这里就需要一张临时表,临时表的作用就是提供数据插入的自增id,得到自增id后再通过取模进行分表插入.
水平分表的表结构是一样的,只是去掉了自增的属性.
这里不得不说水平分表的另一种形式,就是不是通过取模计算的分表,而是user_0存数10w条数据,存满创建新表user_1,继续存储在user_1,存满创建user_2一直存储并新建下去,个人建议这种分表使用场景是user_1的数据为历史数据,访问需求量会慢慢减小,而新表的数据访问量是很高的.
在这里我想说的就是:"根据业务需求进行分表,不为业务服务的架构都是耍流氓".
原文地址:https://www.cnblogs.com/peteremperor/p/9689822.html