Mycat分庫路由規(guī)則

一、Mycat分庫路由分為連續(xù)路由和離散路由。

創(chuàng)新互聯(lián)專注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于成都網(wǎng)站建設(shè)、做網(wǎng)站、駐馬店網(wǎng)絡(luò)推廣、微信小程序開發(fā)、駐馬店網(wǎng)絡(luò)營銷、駐馬店企業(yè)策劃、駐馬店品牌公關(guān)、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運(yùn)營等,從售前售中售后,我們都將竭誠為您服務(wù),您的肯定,是我們最大的嘉獎;創(chuàng)新互聯(lián)為所有大學(xué)生創(chuàng)業(yè)者提供駐馬店建站搭建服務(wù),24小時服務(wù)熱線:18980820575,官方網(wǎng)址:www.muchs.cn

1、連續(xù)路由:

(1)、常用的路由方式:auto-sharding-long、sharding-by-date、sharding-by-month

(2)、優(yōu)點:擴(kuò)容無需遷移數(shù)據(jù);范圍條件查詢消耗資源少。

(3)、缺點:存在數(shù)據(jù)熱點的可能性;并發(fā)訪問能力受限于單一或少量的DataNode

2、離線路由:

(1)、常用的路由方式:sharding-by-intfile、sharding-by-murmur、mod-long(取模)、crc32slot(取模)

(2)、優(yōu)點:并發(fā)訪問能力增強(qiáng)。

(3)、缺點:數(shù)據(jù)擴(kuò)容比較困難,涉及到數(shù)據(jù)遷移問題;數(shù)據(jù)庫鏈接消耗資源多。


二、auto-sharding-long:

1、路由規(guī)則:

<tableRule name="auto-sharding-long-userid">

      <rule>

             <columns>userid</columns>

             <algorithm>rang-long-userid</algorithm>

      </rule>

</tableRule>

 

<function name="rang-long-userid"

        class="io.mycat.route.function.AutoPartitionByLong">

        <property name="mapFile">autopartition-long-userid.txt</property>

</function>

[root@host01 conf]# more autopartition-long-userid.txt

# range start-end ,data node index

# K=1000,M=10000.

0-1000=0

1001-2000=1

2001-3000=2

3001-4000=3

4001-5000=4

5001-6000=5

2、例子:

CREATE TABLE tb_user_detail_t (

  userid bigint not null primary key,

  name varchar(64) DEFAULT NULL,

  createtime datetime DEFAULT CURRENT_TIMESTAMP,

  moditytime datetime DEFAULT CURRENT_TIMESTAMP

) ENGINE=InnoDB DEFAULT CHARSET=utf8;

 

寫入數(shù)據(jù)

insert into tb_user_detail_t(userid,name) values(999,'name999');

insert into tb_user_detail_t(userid,name) values(1999,'name999');

insert into tb_user_detail_t(userid,name) values(2999,'name999');

insert into tb_user_detail_t(userid,name) values(3999,'name999');

insert into tb_user_detail_t(userid,name) values(4999,'name999');

insert into tb_user_detail_t(userid,name) values(5999,'name999');

三、sharding-by-date:

1、路由規(guī)則:

<tableRule name="sharding-by-date-test">

<rule>

       <columns>createtime</columns>

       <algorithm> partbydate </algorithm>

</rule>

</tableRule>

<function name=" partbydate" class="io.mycat.route.function.PartitionByDate">

    <property name="dateFormat"> yyyy-MM-dd HH:mm:ss </property>

    <property name="sBeginDate">2016-01-01 00:00:00</property>

    <property name="sPartionDay">2</property>

</function>

分片日期從2016-01-01開始,每2天一個分片。

2、例子:

CREATE TABLE `tb_user_partbydate` (

  `id` varchar(32) NOT NULL,

  `name` varchar(64) DEFAULT NULL,

  `createtime` varchar(10)

) ENGINE=InnoDB DEFAULT CHARSET=utf8;

 

insert into tb_user_partbydate (id,name,createtime) values('a0001','name1','2016-01-01 00:01:00');
insert into tb_user_partbydate (id,name,createtime) values('a0002','name1','2016-01-02 00:01:00');
insert into tb_user_partbydate (id,name,createtime) values('a0003','name1','2016-01-03 00:01:00');
insert into tb_user_partbydate (id,name,createtime) values('a0004','name1','2016-01-04 00:01:00');
insert into tb_user_partbydate (id,name,createtime) values('a0005','name1','2016-01-05 00:01:00');
insert into tb_user_partbydate (id,name,createtime) values('a0006','name1','2016-01-06 00:01:00');
insert into tb_user_partbydate (id,name,createtime) values('a0007','name1','2016-01-07 00:01:00');
insert into tb_user_partbydate (id,name,createtime) values('a0005','name1','2016-01-08 00:01:00');
insert into tb_user_partbydate (id,name,createtime) values('a0006','name1','2016-01-09 00:01:00');
insert into tb_user_partbydate (id,name,createtime) values('a0007','name1','2016-01-10 00:01:00');

四、sharding-by-month:

1、路由規(guī)則:

<tableRule name="sharding-by-month">

        <rule>

                <columns>createtime</columns>

                <algorithm>partbymonth</algorithm>

        </rule>

</tableRule>

<function name="partbymonth"

        class="io.mycat.route.function.PartitionByMonth">

        <property name="dateFormat">yyyy-MM-dd HH:mm:ss</property>

        <property name="sBeginDate">2015-01-01 00:00:00</property>

</function>

dateFormat為日期格式,sBeginDate為開始日期。

2、例子:

CREATE TABLE ` tb_partbymonth ` (

  `id` varchar(32) NOT NULL,

  `name` varchar(64) DEFAULT NULL,

  `createtime` datetime DEFAULT CURRENT_TIMESTAMP

) ENGINE=InnoDB DEFAULT CHARSET=utf8;

 

寫入數(shù)據(jù)(注意這里不能使用now函數(shù))

insert into tb_partbymonth(id,name,createtime) values('a0001','name1','2015-01-01 10:00:00');

insert into tb_partbymonth(id,name,createtime) values('a0002','name1','2015-02-02 10:00:00');

insert into tb_partbymonth(id,name,createtime) values('a0003','name1','2015-03-01 00:00:00');

insert into tb_partbymonth(id,name,createtime) values('a0004','name1','2015-04-01 00:00:00');

insert into tb_partbymonth(id,name,createtime) values('a0005','name1','2015-05-01 10:00:00');

insert into tb_partbymonth(id,name,createtime) values('a0006','name1','2015-06-02 10:00:00');

insert into tb_partbymonth(id,name,createtime) values('a0007','name1','2015-07-01 00:00:00');

insert into tb_partbymonth(id,name,createtime) values('a0008','name1','2015-08-01 00:00:00');

insert into tb_partbymonth(id,name,createtime) values('a0009','name1','2015-09-01 10:00:00');

insert into tb_partbymonth(id,name,createtime) values('a0010','name1','2015-10-02 10:00:00');

insert into tb_partbymonth(id,name,createtime) values('a0011','name1','2015-11-01 00:00:00');

insert into tb_partbymonth(id,name,createtime) values('a0012','name1','2015-12-01 00:00:00');

insert into tb_partbymonth(id,name,createtime) values('a0013','name1','2016-01-01 00:00:00');

五、sharding-by-intfile(枚舉):

1、路由規(guī)則:

<tableRule name="sharding-by-intfile-provcode">

        <rule>

                <columns>provcode</columns>

                <algorithm>hash-int-provcode</algorithm>

        </rule>

</tableRule>

 

<function name="hash-int-provcode"

        class="io.mycat.route.function.PartitionByFileMap">

        <property name="mapFile">partition-hash-int-provcode.txt</property>

        <property name="type">0</property>

</function>

type=0代表×××

type=1代表字符串類型

[root@host01 conf]# more partition-hash-int-provcode.txt

1=0

2=1

3=2

4=3

5=4

6=5

7=0

8=1

9=2

10=3

11=4

12=5

DEFAULT_NODE=0 ##找不到省份匹配的情況下,默認(rèn)放到數(shù)據(jù)庫1

這里是6個庫,序號0-5,將不同的省份映射到對應(yīng)的庫。所有的省份和庫哦對應(yīng)關(guān)系都要枚舉出來。

2、例子:

CREATE TABLE `tb_user_t` (

  id bigint auto_increment not null primary key,

  `name` varchar(64) DEFAULT NULL,

   provcode int ,

  `createtime` datetime DEFAULT CURRENT_TIMESTAMP,

  `moditytime` datetime DEFAULT CURRENT_TIMESTAMP

) ENGINE=InnoDB DEFAULT CHARSET=utf8;

發(fā)現(xiàn)分庫情況下定義自動增長的id不管用,因為每個庫都有自己的自增長id,通過mycat查詢的話會有重復(fù)的id.

如下:

MySQL> select * from tb_user_t order by id;

+----+----------+----------+---------------------+---------------------+

| id | name     | provcode | createtime          | moditytime          |

+----+----------+----------+---------------------+---------------------+

|  1 | name0005 |        5 | 2017-08-09 10:54:44 | 2017-08-09 10:54:44 |

|  1 | name0001 |        1 | 2017-08-09 10:54:44 | 2017-08-09 10:54:44 |

|  1 | name0004 |        4 | 2017-08-09 10:54:44 | 2017-08-09 10:54:44 |

|  1 | name0002 |        2 | 2017-08-09 10:54:44 | 2017-08-09 10:54:44 |

|  1 | name0003 |        3 | 2017-08-09 10:54:44 | 2017-08-09 10:54:44 |

|  1 | name0006 |        6 | 2017-08-09 10:54:44 | 2017-08-09 10:54:44 |

|  2 | name0011 |       11 | 2017-08-09 10:54:53 | 2017-08-09 10:54:53 |

|  2 | name0007 |        7 | 2017-08-09 10:54:53 | 2017-08-09 10:54:53 |

|  2 | name0010 |       10 | 2017-08-09 10:54:53 | 2017-08-09 10:54:53 |

|  2 | name0008 |        8 | 2017-08-09 10:54:53 | 2017-08-09 10:54:53 |

|  2 | name0009 |        9 | 2017-08-09 10:54:53 | 2017-08-09 10:54:53 |

|  2 | name0012 |       12 | 2017-08-09 10:54:53 | 2017-08-09 10:54:53 |

|  3 | name0013 |       13 | 2017-08-09 11:12:17 | 2017-08-09 11:12:17 |

+----+----------+----------+---------------------+---------------------+

六、sharding-by-murmur:

murmur算法是將字段進(jìn)行hash后分發(fā)到不同的數(shù)據(jù)庫,字段類型支持int和varchar.

1、路由規(guī)則:

<tableRule name="sharding-by-murmur-userid">

        <rule>

                <columns>userid</columns>

                <algorithm>murmur</algorithm>

        </rule>

</tableRule>

 

<function name="murmur"

class="io.mycat.route.function.PartitionByMurmurHash">

<property name="seed">0</property><!--默認(rèn)是0 -->

<property name="count">6</property><!--要分片的數(shù)據(jù)庫節(jié)點數(shù)量,必須指定,否則沒法分片 -->

<property name="virtualBucketTimes">160</property><!--一個實際的數(shù)據(jù)庫節(jié)點被映射為這么多虛擬節(jié)點,默認(rèn)是160倍,也就是虛擬節(jié)點數(shù)是物理節(jié)點數(shù)的160倍 -->

<!-- <property name="weightMapFile">weightMapFile</property>節(jié)點的權(quán)重,沒有指定權(quán)重的節(jié)點默認(rèn)是1。以properties文件

的格式填寫,以從0開始到count-1的整數(shù)值也就是節(jié)點索引為key,以節(jié)點權(quán)重值為值。所有權(quán)重值必須是正整數(shù),否則以1代替 -->

<!-- <property name="bucketMapPath">/etc/mycat/bucketMapPath</property>

用于測試時觀察各物理節(jié)點與虛擬節(jié)點的分布情況,如果指定了這個屬性,會把虛擬節(jié)點的murmur hash值與物理節(jié)點的映

射按行輸出到這個文件,沒有默認(rèn)值,如果不指定,就不會輸出任何東西 -->

</function>

2、例子:

CREATE TABLE `tb_user_murmur_string_t` (

  `userid` varchar(32) NOT NULL,

  `name` varchar(64) DEFAULT NULL,

  `createtime` datetime DEFAULT CURRENT_TIMESTAMP,

  `moditytime` datetime DEFAULT CURRENT_TIMESTAMP,

  PRIMARY KEY (`userid`)

) ENGINE=InnoDB DEFAULT CHARSET=utf8;

 

 

寫入數(shù)據(jù)

insert into tb_user_murmur_string_t(userid,name) values('user002','name002');

insert into tb_user_murmur_string_t(userid,name) values('user003','name003');

insert into tb_user_murmur_string_t(userid,name) values('user004','name004');

insert into tb_user_murmur_string_t(userid,name) values('user005','name005');

insert into tb_user_murmur_string_t(userid,name) values('user006','name006');

insert into tb_user_murmur_string_t(userid,name) values('user007','name007');

insert into tb_user_murmur_string_t(userid,name) values('user008','name008');

insert into tb_user_murmur_string_t(userid,name) values('user009','name009');

insert into tb_user_murmur_string_t(userid,name) values('user010','name010');

七、crc32slot:

crs32算法,分庫字段類型支撐int和varchar.

1、路由規(guī)則:

<tableRule name="crc32slot">

       <rule>

           <columns>id</columns>

           <algorithm>crc32slot</algorithm>

       </rule>

 </tableRule>

 <function name="crc32slot" class="io.mycat.route.function.PartitionByCRC32PreSlot">

        <property name="count">6</property><!--要分片的數(shù)據(jù)庫節(jié)點數(shù)量,必須指定,否則沒法分片 -->

 </function>

count=6指定需要分庫的個數(shù).

2、例子:

CREATE TABLE `tb_user_crc32slot_t` (

  `id` varchar(32) NOT NULL,

  `name` varchar(64) DEFAULT NULL,

  `createtime` datetime DEFAULT CURRENT_TIMESTAMP,

  `moditytime` datetime DEFAULT CURRENT_TIMESTAMP

) ENGINE=InnoDB DEFAULT CHARSET=utf8;

 

寫入數(shù)據(jù):

insert into tb_user_crc32slot_t(id,name) values('a0002','name1');

insert into tb_user_crc32slot_t(id,name) values('a0003','name1');

insert into tb_user_crc32slot_t(id,name) values('a0004','name1');

insert into tb_user_crc32slot_t(id,name) values('a0005','name1');

insert into tb_user_crc32slot_t(id,name) values('a0006','name1');

insert into tb_user_crc32slot_t(id,name) values('a0007','name1');

insert into tb_user_crc32slot_t(id,name) values('a0008','name1');

insert into tb_user_crc32slot_t(id,name) values('a0009','name1');

insert into tb_user_crc32slot_t(id,name) values('a0010','name1');

insert into tb_user_crc32slot_t(id,name) values('a0011','name1');

insert into tb_user_crc32slot_t(id,name) values('a0012','name1');

insert into tb_user_crc32slot_t(id,name) values('a0013','name1');

insert into tb_user_crc32slot_t(id,name) values('a0014','name1');

insert into tb_user_crc32slot_t(id,name) values('a0015','name1');


八、mod-long:

1、路由規(guī)則:對十進(jìn)制數(shù)進(jìn)行按照節(jié)點取模。

<tableRule name="mod-long">

       <rule>

           <columns>id</columns>

           <algorithm>mod-long</algorithm>

       </rule>

 </tableRule>

 <function name="mod-long" class="io.mycat.route.function.PartitionByMod">

        <property name="count">3</property>

         <!-- 要分片的數(shù)據(jù)庫節(jié)點數(shù)量,必須指定,否則沒法分片,即對十進(jìn)制數(shù)進(jìn)行按照節(jié)點取模,將數(shù)據(jù)離散的分散到各個數(shù)據(jù)節(jié)點上 -->

 </function>

九、mycat分庫規(guī)則E/R規(guī)則 :

1、路由規(guī)則:

E/R規(guī)則通過childTable設(shè)定之后,父子表相同的Id會落在相同的庫,這樣的避免關(guān)聯(lián)的時候跨庫進(jìn)行關(guān)聯(lián).

joinKey="order_id" 是子表的order_id字段

parentKey="id"     是父表的id字段

即子表通過order_id字段跟父表的id字段進(jìn)行關(guān)聯(lián)

Mycat分庫路由規(guī)則

2、例子

(2.1)、創(chuàng)建表語句:

create table orders

(

  idint not null,

  order_name varchar(64),

  createtime datetime DEFAULT CURRENT_TIMESTAMP,

  moditytime datetime DEFAULT CURRENT_TIMESTAMP,

  PRIMARY KEY (id)

);

create table orders_cargo

(

  order_idint not null,

  cargo_name varchar(64),

  createtime datetime DEFAULT CURRENT_TIMESTAMP,

  moditytime datetime DEFAULT CURRENT_TIMESTAMP,

 PRIMARY KEY (order_id)  

);

(2.2)、客戶Custermer和訂單Order

    每個客戶和每個客戶的訂單最好在同一個庫中。

    Mycat分庫路由規(guī)則

3、如果把父表最為全局表也能解決join的效率問題。

Mycat分庫路由規(guī)則

分享題目:Mycat分庫路由規(guī)則
分享鏈接:http://www.muchs.cn/article20/piehco.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站策劃、自適應(yīng)網(wǎng)站、手機(jī)網(wǎng)站建設(shè)、微信公眾號、靜態(tài)網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站建設(shè)網(wǎng)站維護(hù)公司