Detailed explanation of MySQL-sub-database and sub-table (6)

♥️Author : Xiao Liu at Station C

♥️Personal homepage:  Xiao Liu's homepage 

♥️Efforts may not necessarily pay off, but there will be gains! Come on! Work hard together for a better life!

♥️ Learn the operation and maintenance experience summed up in two years, and a full set of network experiment tutorials for Cisco simulators. Column: Cloud Computing Technology

♥️Xiao Liu private message can ask casually, as long as you know it, you will not be stingy, thank you CSDN for letting you and me meet!

foreword

The last chapter talked about MySQL-detailed explanation of sub-database and sub-table (5)            

Table of contents

5.3.4 Enumeration shards

1 Introduction          

 2). Configuration

 3). Test

5.3.5 Applying the specified algorithm

1 Introduction

​edit

 2). Configuration

 Example explanation:

3). Test

5.3.6 Fixed Fragmentation Hash Algorithm

1 Introduction

Features:

2). Configuration

 Constraints:

3). Test

5.3.7 String hash parsing algorithm

1 Introduction

 2). Configuration

 Example explanation:

 3). Test


    

5.3.4 Enumeration Fragmentation

1 Introduction          

By configuring possible enumerated values ​​in the configuration file , the specified data is distributed to different data nodes . This rule applies to
Separate, state split data and other services.
<!-- 枚举 -->
<table name="tb_user" dataNode="dn4,dn5,dn6" rule="sharding-by-intfile-enumstatus"
/>

 2). Configuration

Logic table configuration in schema.xml :
 
<!-- 枚举 -->
<table name="tb_user" dataNode="dn4,dn5,dn6" rule="sharding-by-intfile-enumstatus"

Data node configuration in schema.xml :
<dataNode name="dn4" dataHost="dhost1" database="itcast" />
<dataNode name="dn5" dataHost="dhost2" database="itcast" />
<dataNode name="dn6" dataHost="dhost3" database="itcast" />
Fragmentation rule configuration in rule.xml :
<tableRule name="sharding-by-intfile">
<rule>
<columns>sharding_id</columns>
<algorithm>hash-int</algorithm>
</rule>
</tableRule>
<!-- 自己增加 tableRule -->
<tableRule name="sharding-by-intfile-enumstatus">
<rule>
<columns>status</columns>
<algorithm>hash-int</algorithm>
</rule>
</tableRule>
<function name="hash-int" class="io.mycat.route.function.PartitionByFileMap">
<property name="defaultNode">2</property>
<property name="mapFile">partition-hash-int.txt</property>
</function>
partition-hash-int.txt , the content is as follows :
1=0
2=1
3=2
Fragmentation rule attribute meaning:

 3). Test

After the configuration is complete, restart MyCat , and then in the command line of mycat , execute the following SQL to create a table, insert data, and check the data distribution.

CREATE TABLE tb_user (
id bigint(20) NOT NULL COMMENT 'ID',
username varchar(200) DEFAULT NULL COMMENT '姓名',
status int(2) DEFAULT '1' COMMENT '1: 未启用, 2: 已启用, 3: 已关闭',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
insert into tb_user (id,username ,status) values(1,'Tom',1);
insert into tb_user (id,username ,status) values(2,'Cat',2);
insert into tb_user (id,username ,status) values(3,'Rose',3);
insert into tb_user (id,username ,status) values(4,'Coco',2);
insert into tb_user (id,username ,status) values(5,'Lily',1);
insert into tb_user (id,username ,status) values(6,'Tom',1);
insert into tb_user (id,username ,status) values(7,'Cat',2);
insert into tb_user (id,username ,status) values(8,'Rose',3);
insert into tb_user (id,username ,status) values(9,'Coco',2);
insert into tb_user (id,username ,status) values(10,'Lily',1);

5.3.5 Application Specific Algorithms

1). Introduction

In the running phase, the application decides which shard to route to , and calculates the shard number directly based on the character substring (must be a number).

 2). Configuration

Logic table configuration in schema.xml :
<!-- 应用指定算法 -->
<table name="tb_app" dataNode="dn4,dn5,dn6" rule="sharding-by-substring" /> 
Data node configuration in schema.xml :
<dataNode name="dn4" dataHost="dhost1" database="itcast" />
<dataNode name="dn5" dataHost="dhost2" database="itcast" />
<dataNode name="dn6" dataHost="dhost3" database="itcast" /> 
Fragmentation rule configuration in rule.xml :
<tableRule name="sharding-by-substring">
<rule>
<columns>id</columns>
<algorithm>sharding-by-substring</algorithm>
</rule>
</tableRule>
<function name="sharding-by-substring"
class="io.mycat.route.function.PartitionDirectBySubString">
<property name="startIndex">0</property> <!-- zero-based -->
<property name="size">2</property>
<property name="partitionCount">3</property>
<property name="defaultPartition">0</property>
</function>
Fragmentation rule attribute meaning:

 Example explanation :

id=05-100000002, in this configuration, it means starting from startIndex=0 in the id , intercepting siz=2 digits
05 , 05 is the obtained partition, if no corresponding partition is found, it will be assigned to defaultPartition by default .

3). Test

After the configuration is complete, restart MyCat , and then in the command line of mycat , execute the following SQL to create a table, insert data, and check the data distribution.
CREATE TABLE tb_app (
id varchar(10) NOT NULL COMMENT 'ID',
name varchar(200) DEFAULT NULL COMMENT '名称',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
insert into tb_app (id,name) values('0000001','Testx00001');
insert into tb_app (id,name) values('0100001','Test100001');
insert into tb_app (id,name) values('0100002','Test200001');
insert into tb_app (id,name) values('0200001','Test300001');
insert into tb_app (id,name) values('0200002','TesT400001');

5.3.6 Fixed Fragmentation Hash Algorithm

1). Introduction

This algorithm is similar to the decimal modulo operation, but it is a binary operation. For example, take the lower 10 binary bits of the id and
perform the bit & operation with 1111111111. The minimum value of the bit AND operation is 0000000000, and the maximum value is 1111111111, which is converted to
decimal , which is between 0-1023.

Features:

If it is modulo, continuous values ​​are assigned to different slices; but this algorithm may assign continuous values ​​to the same
Fragmentation reduces the difficulty of transaction processing.
It can be distributed evenly or unevenly.
Fragment fields must be of numeric type.

2). Configuration

Logic table configuration in schema.xml :
<!-- 固定分片hash算法 -->
<table name="tb_longhash" dataNode="dn4,dn5,dn6" rule="sharding-by-long-hash" />
Data node configuration in schema.xml :
<dataNode name="dn4" dataHost="dhost1" database="itcast" />
<dataNode name="dn5" dataHost="dhost2" database="itcast" />
<dataNode name="dn6" dataHost="dhost3" database="itcast" />
Fragmentation rule configuration in rule.xml :
<tableRule name="sharding-by-long-hash">
<rule>
<columns>id</columns>
<algorithm>sharding-by-long-hash</algorithm>
</rule>
</tableRule>
<!-- 分片总长度为1024,count与length数组长度必须一致; -->
<function name="sharding-by-long-hash"
class="io.mycat.route.function.PartitionByLong">
<property name="partitionCount">2,1</property>
<property name="partitionLength">256,512</property>
</function>
Fragmentation rule attribute meaning:

 Constraints :

1). Fragment length : the default maximum is 2^10, which is 1024;
2). The array lengths of count and length must be consistent ;
The above is divided into three partitions : 0-255, 256-511, 512-1023
Example explanation :

3). Test

After the configuration is complete, restart MyCat , and then in the command line of mycat , execute the following SQL to create a table, insert data, and check the data distribution.
CREATE TABLE tb_longhash (
id int(11) NOT NULL COMMENT 'ID',
name varchar(200) DEFAULT NULL COMMENT '名称',
firstChar char(1) COMMENT '首字母',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
insert into tb_longhash (id,name,firstChar) values(1,'七匹狼','Q');
insert into tb_longhash (id,name,firstChar) values(2,'八匹狼','B');
insert into tb_longhash (id,name,firstChar) values(3,'九匹狼','J');
insert into tb_longhash (id,name,firstChar) values(4,'十匹狼','S');
insert into tb_longhash (id,name,firstChar) values(5,'六匹狼','L');
insert into tb_longhash (id,name,firstChar) values(6,'五匹狼','W');
insert into tb_longhash (id,name,firstChar) values(7,'四匹狼','S');
insert into tb_longhash (id,name,firstChar) values(8,'三匹狼','S');
insert into tb_longhash (id,name,firstChar) values(9,'两匹狼','L');

5.3.7 String hash parsing algorithm

1). Introduction

Intercept the substring at the specified position in the string , perform the hash algorithm, and calculate the fragmentation.

 2). Configuration

Logic table configuration in schema.xml :
<!-- 字符串hash解析算法 -->
<table name="tb_strhash" dataNode="dn4,dn5" rule="sharding-by-stringhash" />
Data node configuration in schema.xml :
<dataNode name="dn4" dataHost="dhost1" database="itcast" />
<dataNode name="dn5" dataHost="dhost2" database="itcast" />
Fragmentation rule configuration in rule.xml :
<tableRule name="sharding-by-stringhash">
<rule>
<columns>name</columns>
<algorithm>sharding-by-stringhash</algorithm>
</rule>
</tableRule>
<function name="sharding-by-stringhash"
class="io.mycat.route.function.PartitionByString">
<property name="partitionLength">512</property> <!-- zero-based -->
<property name="partitionCount">2</property>
<property name="hashSlice">0:2</property>
</function>
Fragmentation rule attribute meaning:

 Example explanation:

 3). Test

After the configuration is complete, restart MyCat , and then in the command line of mycat , execute the following SQL to create a table, insert data, and check the data distribution.
create table tb_strhash(
name varchar(20) primary key,
content varchar(100)
)engine=InnoDB DEFAULT CHARSET=utf8mb4;
INSERT INTO tb_strhash (name,content) VALUES('T1001', UUID());
INSERT INTO tb_strhash (name,content) VALUES('ROSE', UUID());
INSERT INTO tb_strhash (name,content) VALUES('JERRY', UUID());
INSERT INTO tb_strhash (name,content) VALUES('CRISTINA', UUID());
INSERT INTO tb_strhash (name,content) VALUES('TOMCAT', UUID());

♥️Following is the driving force for my creation

♥️Like, is the greatest recognition for me

♥️This is Xiaoliu, I am inspiring to do every article well, thank you everyone

Guess you like

Origin blog.csdn.net/lzl10211345/article/details/131561349