我们已经通过之前的六篇文章,介绍了 DBLE 和 MyCat 常见的六种分片算法,那么我们来做个总结吧!


  • DBLE 与 MyCat 对应分片算法名和异同
    DBLE 分片算法 MyCat分片算法 DBLE MyCat
    hash 固定分片 hash 取模 分片数最大支持 2880 count 和 length 两个向量的点乘积恒等于 1024
    stringhash 截取数字 hash 解析 分片数最大支持 2880 count 和 length 两个向量的点乘积恒等于 1024
    enum 分片枚举
    numberrange 范围约定 写具体的数值 可用 K 或 M 等数量替换符
    patternrange 取模范围约束
    date 按日期(天)分片

第七种 DBLE 分片算法 — jumpStringHash

除了以上六种常见的分片算法之外,DBLE 还独有一种分片算法:跳跃字符串算法

具体配置如下:

#rule.xml
<function name="jumphash"
          class="jumpStringHash">
        <property name="partitionCount">2</property>
        <property name="hashSlice">0:2</property>
</function>

partitionCont:分片数量

hashSlice:分片截取长度

该算法来自于 Google 的一篇文章《A Fast, Minimal Memory, Consistent Hash Algorithm》其核心思想是通过概率分布的方法将一个 hash 值在每个节点分布的概率变成 1/n,并且可以通过更简便的方法可以计算得出,而且分布也更加均匀。

注意事项:分片字段值为NULL时,数据恒落在0号节点之上;当真实存在于mysql的字段值为not null的时候,报错 “Sharding column can’t be null when the table in MySQL column is not null”

至此我们 DBLE 中支持的七种分片算法就介绍完了,相信读者朋友对 DBLE 的使用增加的熟悉,

//往期回顾

系列文章的最后

完整分区实例
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE dble:rule SYSTEM "rule.dtd">
<dble:rule xmlns:dble="http://dble.cloud/" version="9.9.9.9">
    <tableRule name="sharding-by-enum">
        <rule>
            <columns>id</columns>
            <algorithm>enum</algorithm>
        </rule>
    </tableRule>

    <tableRule name="sharding-by-range">
        <rule>
            <columns>id</columns>
            <algorithm>rangeLong</algorithm>
        </rule>
    </tableRule>

    <tableRule name="sharding-by-hash">
        <rule>
            <columns>id</columns>
            <algorithm>hashLong</algorithm>
        </rule>
    </tableRule>

    <tableRule name="sharding-by-hash2">
        <rule>
            <columns>id</columns>
            <algorithm>hashLong2</algorithm>
        </rule>
    </tableRule>

    <tableRule name="sharding-by-hash3">
        <rule>
            <columns>id</columns>
            <algorithm>hashLong3</algorithm>
        </rule>
    </tableRule>

    <tableRule name="sharding-by-mod">
        <rule>
            <columns>id</columns>
            <algorithm>hashmod</algorithm>
        </rule>
    </tableRule>

    <tableRule name="sharding-by-hash-str">
        <rule>
            <columns>id</columns>
            <algorithm>hashString</algorithm>
        </rule>
    </tableRule>

    <tableRule name="sharding-by-date">
        <rule>
            <columns>calldate</columns>
            <algorithm>partbydate</algorithm>
        </rule>
    </tableRule>

    <tableRule name="sharding-by-pattern">
        <rule>
            <columns>id</columns>
            <algorithm>pattern</algorithm>
        </rule>
    </tableRule>


    <!-- enum partition -->
    <function name="enum"
              class="Enum">
        <property name="mapFile">partition-hash-int.txt</property>
        <property name="defaultNode">0</property><!--the default is -1,means unexpected value will report error-->
        <property name="type">0</property><!--0 means key is a number, 1 means key is a string-->
    </function>

    <!-- number range partition -->
    <function name="rangeLong" class="NumberRange">
        <property name="mapFile">autopartition-long.txt</property>
        <property name="defaultNode">0</property><!--he default is -1,means unexpected value will report error-->
    </function>

    <!-- Hash partition,when partitionLength=1, it is a mod partition-->
    <!--MAX(sum(count*length[i]) must not more then 2880-->
    <function name="hashLong" class="Hash">
        <property name="partitionCount">8</property>
        <property name="partitionLength">128</property>
        <!-- <property name="partitionCount">2,3</property>
        <property name="partitionLength">4,5</property>-->
    </function>

    <!-- Hash partition,when partitionLength=1, it is a mod partition-->
    <!--MAX(sum(count*length[i]) must not more then 2880-->
    <function name="hashLong2" class="Hash">
        <property name="partitionCount">2</property>
        <property name="partitionLength">512</property>
        <!-- <property name="partitionCount">2,3</property>
        <property name="partitionLength">4,5</property>-->
    </function>

    <!-- Hash partition,when partitionLength=1, it is a mod partition-->
    <!--MAX(sum(count*length[i]) must not more then 2880-->
    <function name="hashLong3" class="Hash">
        <property name="partitionCount">2,1</property>
        <property name="partitionLength">256,512</property>
        <!-- <property name="partitionCount">2,3</property>
        <property name="partitionLength">4,5</property>-->
    </function>

    <!-- eg:  mod 4 -->
    <function name="hashmod" class="Hash">
        <property name="partitionCount">4</property>
        <property name="partitionLength">1</property>
    </function>

    <!-- Hash partition for string-->
    <function name="hashString" class="StringHash">
        <property name="partitionCount">8</property>
        <property name="partitionLength">128</property>
        <property name="hashSlice">0:2</property>
        <!--<property name="hashSlice">-4:0</property> -->
    </function>

    <!-- date partition -->
    <!-- 4 case:
    1.set sEndDate and defaultNode: input <sBeginDate ,router to defaultNode; input>sEndDate ,mod the period
    2.set sEndDate, but no defaultNode:input <sBeginDate report error; input>sEndDate ,mod the period
    3.set defaultNode without sEndDate: input <sBeginDate router to defaultNode;input>sBeginDate + (node size)*sPartionDay-1 will report error(expected is defaultNode,but can't control now)
    4.sEndDate and defaultNode are all not set: input <sBeginDate report error;input>sBeginDate + (node size)*sPartionDay-1 will report error
     -->
    <function name="partbydate" class="Date">
        <property name="dateFormat">yyyy-MM-dd</property>
        <property name="sBeginDate">2015-01-01</property>
        <property name="sEndDate">2015-01-31
        </property> <!--if not set sEndDate,then in fact ,the sEndDate = sBeginDate+ (node size)*sPartionDay-1 -->
        <property name="sPartionDay">10</property>
        <property name="defaultNode">0</property><!--the default is -1-->
    </function>

    <!-- pattern partition -->
    <!--mapFile must contains all value of 0~patternValue-1,key and value must be Continuous increase-->
    <function name="pattern"
              class="PatternRange">
        <property name="mapFile">partition-pattern.txt</property>
        <property name="patternValue">1024</property>
        <property name="defaultNode">0</property><!--contains string which is not number,router to default node-->
    </function>
</dble:rule>

第三期 社区技术内容征稿

所有稿件,一经采用,均会为作者署名。

征稿主题:MySQL、分布式中间件DBLE、数据传输组件DTLE相关的技术内容

活动时间:2019年6月11日 – 7月11日

本期投稿奖励

投稿成功:京东卡200元*1

优秀稿件:京东卡200元*1+社区定制周边(包含:定制文化衫、定制伞、鼠标垫)

优秀稿件评选,文章获得“好看”数量排名前三的稿件为本期优秀稿件。