00:00
我们来做分片算法的配置,那么分片算法这一块呢,我们分为水平分库和水平分表两个方面来讲,我们先来看水平分库,那在水平分库当中呢,我们应该如何进行配置呢?我们先找到官方文档,官方文档当中首先我们找到。第五章用户手册starter规则配置当中的数据分片,那么就是这个文档,接下来呢,我们再来找到。内置算法当中的分片算法。就是这个文档好,接下来呢,我们基于这两个文档当中的内容呢,来进行配置,那么数据分片当中呢,我们先找到。分库策略,在分库策略当中呢,我们先把上面这一部分内容呢,给它复制过来。
01:00
好,所以这块呢,就是分库策略。那这个位置呢,我们要写一个逻辑表,也就是说你现在正在针对哪一个逻辑表呢,进行一个分配置O啊。然后呢,就是分片分法称分片呢,这个指定是分分。然后后面呢,我们要设置一个分片算法,分片算法呢有很多,那么假设说呢,我们先根据曲模运算的方式呢来进行分片,我偶当我们user ID的是基数的时候,那我们就把它分配在serve这台服务器当中,所以呢我们在这个位置。
02:14
分片名称呢,就是user ID了。好,这是分片列名称。然后接下来呢,就是分片算法名称,分片算法名称我们要配置的话呢,我们必须得先定义一个命名的分片算法,所以接下来呢,我们来定义分片算法。在这个位置我们把它复制过来。好,那么分片算法类型呢,我们。在这个位置来找,这里面有很多分片算法类型,我们呢,先找到这个最基本的行表达式分片算法,那么行表达式分片算法它的类型呢,就叫音,我们把它配置在这个位置。
03:10
好,我们把分片算法类型的说明呢粘到上面。那么这个行表达式分片算法,具体它的这个行表达式是什么呢?在下面的这个属性当中进行配置。把它配到这个位置好,那这个呢,就是分片算法属性配置。那刚才我们提到呢,这个分片算法呢,我们是想根据UCIID呢来进行分片,当UCIID的值是偶数的时候,我们想这记插入到。它是零还是一,它和我们的UCID的值是有关系的,所以这个地方呢,我们也可以写一个行表达式,那么我们写。
04:12
Dollar。箭头,那么它和user ID的值是有关系的,所以UCID对二模好,这是我们对分片算法的一个配置,然后呢,我们给这个分片算法起一个名字。比如名叫a LG in u I吧。那这是我们起的名字,同样把这个名字呢,也放在这个位置,然后这块呢,就指定我们分片算法的名称。这样的话呢,我们的这个分库策略呢,就配置完成了,那么这个分库策略配置完成之后呢,我们先来简单的做一个测试。
05:00
好。我们写一个测试用例public wide。然后呢,我们这个呢,是一个分库策略的一个测试,所以呢,我们把这个粘过去吧,叫分库策略这个单词。那在这块呢,我们写一个。呃,也是的一个插入,但是呢,我们打算写一个循环,比如说我们插入四条记录好。然后这块呢,我们用I来表示。
06:00
嗯,这个I呢,我们给它写成的形式,然后呢,这个ID呢,假设说我们从一开始吧,好。然后这块我们先临时这样写。那我们呢,在这个位置呢,右键直行。好,很显然我们会得到一个错误的结果,为什么呢?刚才我们指定了分库策略,但是呢,没有指定分表策略,所以呢,他又报告了这样的一个错误,叫insert支持把这条记录呢路由到多个数据节点上。因为我们只由到了数据库上,并没有到具体的表上,所以呢,我们的呢,它依然不知道你由到的是零表还是ORDER1表啊,那他只能知道我路由到ORDER0数据库还是ORDER1数据库哈,所以呢,怎么办呀,在配置分表策略之前呢,我们先让表固定下来。
07:15
那怎么让表固定下来呢?就是这个位置我们先临时呢,比如说写一个啊这样的话呢,因为我们已经指定了数据库了,通过哪个地方指定的呢?通过这个位置指定的哪个数据库呢?指定到了这个数据库,这是策略,所以呢,目前为止我们只有分库策略,并没有分表策略,那么我们需要。硬编码一个具体的表明好,这样的话呢,才能进行测试,那么接下来呢,我们再次对这个测试用例呢进行执行。好,这一次呢就成功了,那么我们来看一下。
08:01
这是我们的逻辑circle,逻辑circle当中呢,它是向逻辑表T当中插入了记录,那具体的记录它是插入到了哪个物理表当中呢?我们来找一下,我们先来看一下逻辑色口啊,一共有四条,对不对啊,四条逻辑色口,因为我们插了四条记录嘛,然后呢,针对这四条呢,我们再来看的实际,实际呢,第一个它是插入到了serve。插第插ID数啊,然后最后一个它插入到了S0的T0中,因为我们的ID呢是一个偶数,所以呢,它插入到了偶数库中。
09:10
这是我们的水平分库的配置,以及它其中所涉及到的分片算法。我们最后再来看一下数据库中的情况,我们来看SERVER0 T0中确实呢有两条记录,他们的UID呢,分别是二和四。那么我们再看一。在T中呢,也有两条记录被插入了他们的UID,分别是一和三。
我来说两句