<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                >[info] mycat 切片規則 ## **注意:** 每修改 xml 都需要重啟 mycat ### **1. global 全局表規則** **1.1 創建兩個數據庫和兩個全局表** ``` test 數據庫 company 數據表(id name兩個字段) test1 數據庫 company 數據表(id name兩個字段) ``` ![](https://img.kancloud.cn/ce/6e/ce6ecc4e038a31162e9e79530470889a_1047x591.png) **1.2 schema.xml 配置** ~~~ <table name="company" primaryKey="ID" type="global" dataNode="dn1,dn2" /> <dataNode name="dn1" dataHost="localhost1" database="test" /> <dataNode name="dn2" dataHost="localhost1" database="test1" /> ~~~ **1.3 mycat 操作數據** 登錄 mycat: ``` mysql -uroot -p123456 -P 8066 -h 127.0.0.1 ``` 選擇邏輯庫: ``` use TESTDB; ``` 測試表:(test 和 test1庫) ``` CREATE TABLE `company` ( `id` int(10) NOT NULL AUTO_INCREMENT COMMENT '主鍵', `name` varchar(20) DEFAULT '' COMMENT '姓名', `sharding_id` int(10) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4; CREATE TABLE `employee` ( `id` int(10) NOT NULL AUTO_INCREMENT COMMENT '主鍵', `name` varchar(20) DEFAULT '' COMMENT '姓名', `sharding_id` int(10) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4; ``` 分析SQL插入: ``` explain insert into company(name) values ('zhangsan'); ``` ![](https://img.kancloud.cn/f8/71/f871ae9aae3b119469beea7873e9a838_671x156.png) SQL插入: ``` insert into company(name) values ('zhangsan'); ``` ![](https://img.kancloud.cn/0d/72/0d726b798ddbef58482ffbe818599946_585x61.png) 查詢 `company` 全局表數據: ``` select * from company; ``` ![](https://img.kancloud.cn/54/4d/544d3ee9867d5847b2462a87d5ae24e2_374x165.png) 查看 `test` 和 `test1` 數據庫 `company`表都有數據: ![](https://img.kancloud.cn/d9/56/d9569f0aa3de68d498d02700aed958f2_612x368.png) ### **2. hash-int 字段規則** **2.1 創建兩個數據庫和兩個全局表(跟上面一樣)** ``` test 數據庫 employee 數據表(id name兩個字段) test1 數據庫 employee 數據表(id name兩個字段) ``` ![](https://img.kancloud.cn/aa/39/aa398d2870400384a4adbeeb8123c813_400x465.png) **2.2 新增數據** **2.2.1 報錯:** 由于沒有規則字段 `sharding_id` ``` insert into employee(name) values ('zhangsan'); ``` ![](https://img.kancloud.cn/20/71/207123e8e60bf2b80d1df1cd1543805c_1064x148.png) rule.xml 中的定義的字段如下: ~~~ <tableRule name="sharding-by-intfile"> <rule> <columns>sharding_id</columns> <algorithm>hash-int</algorithm> </rule> </tableRule> ~~~ **2.2.2 解決:** 在 `test`和 `test1`庫中 `employee`新增該字段即可 ``` alter table employee add column sharding_id int(10) not null; ``` ![](https://img.kancloud.cn/ee/74/ee7449ea101c7138028b970c3d83f5c0_379x95.png) **2.2.3 再次新增:** 哈希規則文件: `partition-hash-int.txt` ~~~ 10000=0 10010=1 ~~~ 分析SQL: ``` explain insert into employee(name,sharding_id) values ('zhangsan',10000); ``` ![](https://img.kancloud.cn/bf/3e/bf3e501499e61d48b5bff9f0a02aa443_951x354.png) 向 `dn2` 插入SQL: ``` # dn1 屬于 test 數據庫 insert into employee(name,sharding_id) values ('zhangsan',10000); ``` ``` # dn2 屬于 test1數據庫 insert into employee(name,sharding_id) values ('zhangsan',10010); ``` ![](https://img.kancloud.cn/b8/67/b86720684e5a3c1bf2ffdb3ed0a9a5d4_950x260.png) ![](https://img.kancloud.cn/72/7f/727fd94bbd95ce35b276a5dbb0ea16c6_926x689.png) ![](https://img.kancloud.cn/b3/91/b39158c26d7e6be887e2e636cd7e25a9_1098x584.png) ### **3. hash 取模** **3.1 新建 order 數據庫 和 3個 order 表** ``` CREATE TABLE `order_0` ( `order_id` int(10) NOT NULL COMMENT '訂單id', `order_name` varchar(50) NOT NULL COMMENT '訂單名稱', `goods_name` varchar(100) NOT NULL COMMENT '商品名稱', `goods_price` decimal(10,2) unsigned NOT NULL DEFAULT '0.00' COMMENT '商品價格', PRIMARY KEY (`order_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; ``` ![](https://img.kancloud.cn/3b/78/3b78dfbd19fa1f9597d204285eed4880_1314x555.png) **3.2 修改 schema.xml 配置文件** ~~~ <table name="order" primaryKey="order_id" subTables="order_$0-2" dataNode="dn3" rule="jch"/> <dataNode name="dn3" dataHost="localhost1" database="order" /> ~~~ **3.3 修改 rule.xml 配置文件** `rule.xml` 修改配置參數為:3(表數量) ~~~ <function name="jump-consistent-hash" class="io.mycat.route.function.PartitionByJumpConsistentHash"> <property name="totalBuckets">3</property> </function> ~~~ `rule.xml` 修改取模字段 ~~~ <tableRule name="jch"> <rule> <columns>order_id</columns> <algorithm>jump-consistent-hash</algorithm> </rule> </tableRule> ~~~ **3.4 重啟 mycat 并進入 mycat** ``` ./mycat restart mysql -uroot -p123456 -P 8066 -h 127.0.0.1 use TESTDB; select * from order; ``` ![](https://img.kancloud.cn/54/f7/54f7e13d08ec513a3ffe2388600e962d_966x399.png) ``` # 1 % 3 = 1 所以插入到 order_1 表中 insert into order(order_id,order_name,goods_name,goods_price) values (1,'訂單名稱','蘋果手機',4999); # 2 % 3 = 2 所以插入到 order_2 表中 insert into order(order_id,order_name,goods_name,goods_price) values (2,'訂單名稱','蘋果手機',4999); # 3 % 3 = 0 并未插入到 order_0 表中而是插入到 order_1 中(未解決) insert into order(order_id,order_name,goods_name,goods_price) values (3,'訂單名稱','蘋果手機',4999); ``` ![](https://img.kancloud.cn/05/86/05861bef1aa60263ef170bff9b4a252f_1045x231.png)
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看