1. 程式人生 > >mycat ER關係 取模分片試驗

mycat ER關係 取模分片試驗

                       Mycat  商品表ER分片

1. 業務場景

   使用者查詢商品資訊

   要求:把商品表以及子表均勻分片
   分片規則:

   商品表commodity --- 根據commodity_id取模

2. 

   主表:
    commodity

   子表:
   commodity_additional_attributes
   commodity_commodity_category

   commodity_attachment
   commodity_issuance_station

3. 模擬伺服器
  本地虛擬機器
   192.168.190.11 testdb   db1
                        db2
                        db3
  配置

 記憶體 2G

                2

4)全域性序列號
   採用本地檔案方式 配置server.xml   0代表本地檔案方式
 <property name="sequnceHandlerType">0</property>

   屬性檔案 /usr/local/mycat/conf/sequence_conf.properties
     #commodity

COMMODITY.HISIDS=

COMMODITY.MAXID=9999999999

COMMODITY.MINID=1000

COMMODITY.CURID=1325358

4. 配置分片規則

5.1 配置function標籤,找到function標籤name="mod-long" 的function 配置, 改總點數為3(和使用的mysql 點數一致)。

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

<!-- how many data nodes -->

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

</function>

5.2 配置表分片(name 要全域性唯一),rule.xml 配置新增如下配置

  <!-- commodity分片規則 -->

        <tableRule name="tb_commodity_mod-long">

                <rule>

                        <!--  分片使用的欄位  -->

  <columns>commodity_id</columns>

<!--  分片使用的方法,對應function 名稱  -->

                        <algorithm>mod-long</algorithm>

                </rule>

         </tableRule>

5. 配置schema.xml
    <!--  配置ER 分片,子表的儲存依賴於主表,並且物理上緊鄰存放-->

 <table name="commodity" primaryKey="commodity_id" dataNode="dn1,dn2,dn3"

   rule="tb_commodity_mod-long">

<childTable name="commodity_additional_attributes" primaryKey="commodity_id" joinKey="commodity_id"

parentKey="commodity_id" />

<childTable name="commodity_attachment" primaryKey="attachment_id" joinKey="commodity_id"

parentKey="commodity_id" />

<childTable name="commodity_commodity_category" primaryKey="commodity_id" joinKey="commodity_id"

parentKey="commodity_id" />

<childTable name="commodity_issuance_station" primaryKey="commodity_id" joinKey="commodity_id"

parentKey="commodity_id" />

</table>

6. 測試
   mycat邏輯庫TESTDB下執行SELECT * from commodity 查詢指令碼


    

   commondity_id = 866878
   求模運算:


     
   物理庫儲存於第2個節點
  db2節點物理庫執行 SELECT * from commodity WHERE commodity_id = '866878' 查詢指令碼


在屋裡庫db2 查詢子表:

   SELECT * from commodity_additional_attributes WHERE commodity_id = '866878'


由此可見 主表子表ER關係資料鄰近儲存

分別查詢db1 db2  db3testdbcommodity表資料為76413 76414  76415

資料分佈均勻

 

7. 效能對比分析


8.資料遷移

  8.1 其他表處理
      1)有ER關係 根據commodity表定義子表資訊 用取模分片演算法均勻分配節點

      2)沒有ER關係
          根據計劃資料量大小,可用取模分片演算法均勻分配節點,也可定義一個節點,單節點分配儲存

      3)全域性表 字典性質的表  定義schema.xml 中 table屬性type=gloabal
      4)有固定性質欄位可均勻分配的表 比如有地區區分的表 按具體省份採用分片規則進行分片儲存
  8.2 資料來源更換
    由於mycat配置了邏輯資料庫 資料表 以及mycat定義了自己的訪問埠,應用程式配置檔案資料庫   連線要更改8066埠並改稱mycat定義的邏輯資料庫名稱。
  8.3 全域性序列號
      mycat進行了物理庫的分庫,原來自定義的自增長主鍵ID不可用,推薦使用本地檔案方式的全域性序 列號。
      應用程式程式碼裡 新增資料時需要更改insert 語句:
        1)指定 MYCATSEQ_GLOBAL  必須大寫 GLOBAL為自定義名稱,可為對應表名。
        2)insert語句必須指定欄位名。
       例如:

insert into company(id,name,addr,src_id) values(next value for MYCATSEQ_GLOBAL,'test_seq4','test_seq4','2');

  8.4 遷移方案
      1)匯出資料 進行全庫表備份匯出sql指令碼檔案;

      2)匯入mycat邏輯資料庫
         mysql -uroot -p -h192.168.190.11 --local-infile=1 -P8066 TESTDB < /data_my/commodity_attachment.sql
        也可以8066埠登入mycat資料庫 source命令匯入
  8.5注意事項
      1)全域性序列號的設定 必須設定以現實庫裡的ID為當前最新id;

      2) 配置ip黑白名單 使用者讀寫許可權
      3)遷移庫需要在mycat中先建立表結構去掉主鍵ID自增長

      4)去掉所有表外來鍵關係