MySQL database library sub-table of the points scheme

First, the database bottleneck
whether IO bottleneck, or a CPU bottleneck, will eventually lead to increase in the number of active connections to the database, the database can then approach even to carry the threshold number of active connections. Service is in the business point of view, little or no connection available database connection is available. Then you can imagine it (concurrency, throughput, collapse).

  1. IO bottleneck
    first: disk read IO bottleneck, too hot data, the database cache does not fit, each will generate a lot of IO query, reduce the query speed -> sub-libraries and vertical sub-table .

    The second: network IO bottleneck, much requested data, network bandwidth is not enough -> sub-libraries .

  2. CPU bottleneck
    first: SQL problem, as contained in the SQL join, group by, order by, non-indexed fields query conditions, increased CPU arithmetic operation -> SQL optimization, establish an appropriate index for service in a service layer calculation Service .

    The second: single table data is too big, too many line scan query, SQL is low efficiency, CPU bottlenecks lead -> level points table.

Second, the sub-library sub-table
1. The level of the sub-library
Here Insert Picture Description
1) Concept: In the field basis, according to a certain policy (hash, range, etc.), split the data into a database of multiple libraries.
2) Results: The
structure of each library are the same;
the data for each library are not the same, there is no intersection;
all libraries and set the whole amount of data;
3) Scene: System absolute amount of concurrent up, sub-table is difficult to fundamentally solve problem, and no obvious to vesting vertical library.
Analysis: library more, io and cpu relieve pressure on natural can be multiplied.
2, the horizontal part table
Here Insert Picture Description
1) Concept: In the field basis, according to a certain policy (hash, range, etc.), split the data into a table of multiple tables.
2) Results: The
structure of each table are the same;
data in each table is different, there is no intersection;
all tables and the amount of the whole data set;
3) Scenario: the absolute amount of concurrent system does not come, only a single table of data too much volume, affecting the SQL efficiency, increased CPU burden that becomes a bottleneck.
ANALYSIS: Data tables less, and high single SQL execution efficiency, naturally reducing the burden on the CPU.
3, the vertical sub-libraries
Here Insert Picture Description
1) Concept: Table basis, according to the service of their various, different tables split into different libraries.
2) Results: The
structure of each of the library are not the same;
the data for each library are not the same, there is no intersection;
All libraries and the whole set of data amount;
3) Scenario: the absolute amount of concurrent up the system, and may be abstract alone the service module.
Analysis: At this point, you can basically of the service. For example, with the development of more and more business some common configuration tables, dictionary tables, etc., then the tables can be split into a separate library, or even as a service. Furthermore, with the development of a set of business incubator business model, then it can be split into separate tables related to the library, or even as a service.
4, the vertical sub-table
Here Insert Picture Description
1) Concept: In the field basis according to activity fields, the split field in the table to a different table (the primary table and extension table).
2) Results:
The structure of each table is not the same;
the data are not the same in each table, in general, the fields of each table having at least one intersection, generally is the primary key for the associated data;
all tables and sets is the total amount of data;
3) scenario: the absolute amount of concurrent system is not up, the recording sheet is not much, but the multi-field, non-hotspot and the hotspot data and data together, a larger storage space required for a single row. So that the database cache line data reduction, will read the disk data to generate a lot of random queries to read IO, IO bottleneck produce.
Analysis: You can use a list of pages and details pages to help understand. Vertical resolution principle is a hot table data (redundant data may frequently queried together) together as the primary table, the non-hotspot data together as an extension table. Such additional data can be cached hot, thereby reducing random read IO. After demolition, in order to get all the data you need to associate the two tables to fetch data. But remember, do not use join, because the join will not only increase the burden on CPU and speaks two tables coupled together (must be on a database instance). Linked Data Service layer should be an issue in the business, respectively acquire the primary and extended table data and obtain all the data associated with the associated field.

Third, the sub-library sub-table tool
1.sharding-sphere: jar, formerly known as jdbc-Sharding;
2.TDDL: JAR, Taobao Distribute the Data Layer;
3.Mycat: middleware.
Note: The pros and cons of tools, your own research, the official website and community priorities.

Fourth, the step of sub-library sub-table
(the current capacity and the amount of increase) assessment sub-library according to the capacity or the number of sub-table -> selected from the group key (even) -> sub-rules table (hash range or the like) -> performed (typically double write) -> expansion issues (minimize data movement).

Five sub-library sub-table question
1, the problem of non-partition key query (horizontal sub-library sub-table, the hash used to split strategy method)
1. In addition to the end partition key partition key as only a non-query conditions

  • Mapping Method
    Here Insert Picture Description
  • Genes
    Here Insert Picture Description
    NOTE: writing, gene by generating user_id, as shown in FIG. About xbit genes, for example, to points in Table 8, 2 3 = 8, and therefore take x 3, i.e. 3bit gene. The query can be directly user_id when routed to the corresponding modulo sub-library or sub-table. The user_name When the query, generating function generates user_name_code by user_name_code then routed to its corresponding modulo sub-library or sub-table. id Generate common snowflake algorithm .

2. In addition to the ends of more than one partition key partition key as a non-query conditions

  • Mapping Method
    Here Insert Picture Description
  • Redundant law
    Here Insert Picture Description
    3. In addition to the background as well as various non-partition key partition key condition combination inquiry
  • NoSQL law
    Here Insert Picture Description
  • 冗余 method
    Here Insert Picture Description

2, non-cross-table partition key cross-database query paging problem (level sub-library sub-table, split strategy commonly used hash method)
Note: solve (ES, etc.) with NoSQL law.

3, expanding problem (level sub-library sub-table, split strategy commonly used hash method)

  • Horizontal expansion of the library (upgrade from Kufa)
    Here Insert Picture Description
  • Horizontal expansion table (double the migration law)
    Here Insert Picture Description
    the first step :( synchronous dual-write) dual-write application configuration, deployment;
    the second step :( synchronous dual-write) to copy the old library old data to the new database;
    the third step :( simultaneous dual-write) to the old library prevail proofread new library of old data;
    the fourth step :( synchronous dual-write) application to remove the double write, deploy;
    Note: double write is a universal program.

Six sub-library sub-table summary
1. sub-library sub-table, first of all have to know where the bottleneck before you can reasonably split (or sub-library sub-table? Horizontal or vertical? In several?). For sub-library and can not be resolved sub-table.
2. Option key is very important, it is necessary to take into account the split evenly, also taking into account non-partition key queries.
3. As long as meet the demand, splitting rules as simple as possible.
Example GitHub Address: https://github.com/littlecharacter4s/study-sharding

Published 18 original articles · won praise 5 · Views 6712

Guess you like

Origin blog.csdn.net/qq_28687183/article/details/104709070