Home  >  Article  >  There are several ways to split a database

There are several ways to split a database

hzc
hzcOriginal
2020-06-29 13:20:255587browse

Database splitting includes: 1. Divide by function [vertical segmentation] and put tables related to different functions into different databases. The advantage of this is that it is very intuitive; 2. According to a certain part of the table Range division of field values ​​[horizontal segmentation]. When the amount of data in a certain table becomes larger and larger and becomes unbearable, it needs to be further segmented.

There are several ways to split a database

Several ways to split the database:

1. Divide by function (vertical segmentation)

Put tables related to different functions into different databases. The advantage of this is that it is very intuitive. But when the data volume or performance requirements of a certain part of the function exceed the controllable range, it needs to be further segmented.

2. Divide according to the range of a certain field value in the table (horizontal segmentation)

When the amount of data accompanying a certain table becomes larger and larger and becomes unbearable, it needs to be Further segmentation. One option is to segment based on the key range, for example, put the IDs 1-10000 on A, and the IDs 10000~20000 on B. Such expansion is foreseeable. The other is to divide according to a certain field value. For example, according to the first letter of the user name, if it is A-D, it belongs to A, and E-H belongs to B. There is also an imbalance in this. When a certain range exceeds the range that a single point can bear, it needs to continue to be divided. There are also segmentation by date and so on.

Advantages: The size of a single table is controllable, and natural horizontal expansion

Disadvantages: The problem of centralized writing bottleneck cannot be solved.

3. Segmentation based on hash

Generally use mod to split. First determine the number of split databases, and decide which one to use by taking the hash modulus. This method can distribute data evenly, but as the amount of data increases, when expansion is needed, this method cannot achieve online expansion. Every time a node is added, the hash algorithm needs to be recalculated.

Therefore, it is recommended to use the consistent hash mod 2^n using this method.

Taking the Dianping Unified Order Database as an example, the solution for sub-database and sub-table is 32*32, that is The last four digits of userId are divided into 32 libraries by mod 32. At the same time, each library is divided into 32 tables by using the last four digits of userId div 32 mod 32, for a total of 1024 tables. Its online deployment is 8 clusters (master-slave), each cluster has 4 libraries

4. Segmentation based on routing table

The previous methods all determine operations based on application data. Segmentation based on routing tables is a more relaxed method. It maintains a separate routing table and searches the routing table to decide which database to use based on a certain attribute of the user. This method is a more general solution.

Advantages: The mapping algorithm of id and library can be changed at will

Disadvantages: Additional single points may be introduced

The above is the detailed content of There are several ways to split a database. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn