1. Commonly used fragmentation rules
1, Tori model
This rule is a modulation operation on the fragmented field. It is also the most commonly used rule for horizontal sub-tables.
2. Shard enumeration
Configure sharding by configuring possible enumeration IDs in the configuration file. This rule is suitable for specific scenarios. For example, some businesses need to be saved according to provinces or districts and counties, and provinces, districts and counties across the country are fixed. This type of business Use this rule.
This is achieved as follows:
2.1. Modify the schema.xml configuration file
The test table is orders_ware_info, which is configured on the dn1 and dn2 nodes. The rule is to add a sharding_by_intfile
2.2. Modify the rule.xml configuration file
2.3. Modify the partition-hash-int.txt configuration file
It means that if the areacode is 110, it will be stored in the first data node, and if it is 120, it will be stored in the second data node.
2.4. Restart Mycat to make the configuration take effect.
Use mycat to create an order ownership area information table:
CREATE TABLE orders_ware_info (
`id` INT AUTO_INCREMENT comment '编号',
`order_id` INT comment '订单编号',
`address` VARCHAR(200) comment '地址',
`areacode` VARCHAR(20) comment '区域编号',
PRIMARY KEY(id)
);
Insert data:
INSERT INTO ORDERS_WARE_INFO(id, order_id,address,areacode) VALUES (1,1,'北京','110');
INSERT INTO ORDERS_WARE_INFO(id, order_id,address,areacode) VALUES (2,2,'天津','120');
Direct query through the database:
spdb_pzex:
spdb_portal_wechat:
3. Scope Agreement
This sharding is suitable for planning in advance which shard a certain range of sharding fields belongs to.
3.1. Modify the schema.xml configuration file
The above defines the table and the stored data nodes and sharding rules , etc.
3.2. Modify the rule.xml configuration file
3.3. Modify the autopartition-long.txt configuration file
3.4. Restart Mycat to make the configuration take effect.
Create the payment information table payment_info in mycat:
CREATE TABLE payment_info(
`id` INT AUTO_INCREMENT comment '编号',
`order_id` INT comment '订单编号',
`payment_status` INT comment '支付状态',
PRIMARY KEY(id)
);
Insert data:
INSERT INTO PAYMENT_INFO (id,order_id,payment_status) VALUES (1,101,0);
INSERT INTO PAYMENT_INFO (id,order_id,payment_status) VALUES (2,102,1);
INSERT INTO PAYMENT_INFO (id,order_id,payment_status) VALUES (3,103,0);
INSERT INTO PAYMENT_INFO (id,order_id,payment_status) VALUES (4,104,1);
View the data of the two host data nodes separately:
spdb_pzex:
spdb_portal_wechat:
4. Segmentation by date (day)
This rule is sharding by day. Set time format and range
4.1. Modify the schema.xml configuration file
4.2. Modify the rule.xml configuration file
You need to customize a sharding function shardingByDate:
4.3. Restart Mycat to make the configuration take effect and restart.
Create user information table login_info in mycat:
CREATE TABLE login_info(
`id` INT AUTO_INCREMENT comment '编号',
`user_id` INT comment '用户编号',
`login_date` date comment '登录日期',
PRIMARY KEY(id)
);
Insert data:
INSERT INTO LOGIN_INFO(id,user_id,login_date) VALUES (1,101,'2019-01-01');
INSERT INTO LOGIN_INFO(id,user_id,login_date) VALUES (2,102,'2019-01-02');
INSERT INTO LOGIN_INFO(id,user_id,login_date) VALUES (3,103,'2019-01-03');
INSERT INTO LOGIN_INFO(id,user_id,login_date) VALUES (4,104,'2019-01-04');
INSERT INTO LOGIN_INFO(id,user_id,login_date) VALUES (5,103,'2019-01-05');
INSERT INTO LOGIN_INFO(id,user_id,login_date) VALUES (6,104,'2019-01-06');
View the data of the two host data nodes separately:
spdb_pzex:
spdb_portal_wechat: