Example of master-slave separation of go sub database and sub table


There are many articles on sub database and sub table on the Internet. There are many methods:

Partition table segmentation
vertical partitioning 
horizontal partitioning 
Interval segmentation
Mold cutting

I won’t elaborate here

It is simple to divide the database and table, but it will bring a series of problems in the later stage:



Example of master-slave separation of go sub database and sub table

Master and slave are a master-slave architecture
imagespider_ DB: [imagespider] the data collected by the project does not need to deploy master-slave separation.
The structure as like as two peas in four branches. Note that the number of sub databases should be the n-th power of 2, otherwise the gene modeling algorithm may fail)

t_ Users user table:

When users log in and register, they need to use name to locate the sub database; When viewing user data, you need to use uid to locate the sub database,
Therefore, add the gene of name to the uid. The last 2bit of uid is the last 2bit of name's CRC32. In this way, both uid and name can locate the sub library.

t_ Images picture table:

Divide the database by ID% 4, and use "page skipping prohibited" when paging.

t_ Comments comment form:

The last 2bit of ID is the 2bit of imgid, which ensures that all comments and pictures of a picture are segmented into the same database.
When users comment, t_ Comment of the images table_ Add 1 to the count field, because it is the same database, transaction processing can still be used;
All comments are segmented into the same database, and traditional methods can be used for paging.

t_ Likes likes:

And t_ Like the comments table, the last 2bit of imgid is also used as the sub library gene.
In some scenarios, the gene segmentation method is used to segment the related tables into the same database. In later operations, transactions, joins and other operations can still be used,
However, it may lead to unbalanced database data.
Imageshow is just an example of horizontal segmentation. Its functions and code are very simple. It certainly can not cover all application scenarios of sub database and sub table. Source code