Design and practice of horizontal sharding of database: Application in PHP programming
As the amount of data increases, how to better manage and optimize the database has become a problem that every developer needs to pay attention to. Among them, database horizontal sharding is a very common optimization method. This article will introduce the concept, principle, design and practice of horizontal sharding of databases, and will focus on how to use this technology in PHP programming.
1. The concept and principle of horizontal sharding
Horizontal sharding refers to horizontally dispersing the data of a table into multiple physical tables according to a certain field. For example, we divide a user table into multiple tables according to the value range of user ID, such as user_0, user_1, user_2, etc. At this time, when querying user information, you need to first calculate the name of the table through the ID, and then search for data in the corresponding table.
The main function of horizontal sharding is to split a large table into small tables to solve the problem of low query efficiency when a single table is too large. In addition, since the data of different tables are stored independently, distributed deployment and high-availability processing can be easily performed.
2. Design of horizontal sharding
When designing horizontal sharding, we need to consider the following issues:
Let’s solve these problems one by one.
Selection of sharding key is the key in horizontal sharding design, which directly affects the quality of sharding effect. Usually, we will choose the primary key or unique index of a certain table as the sharding key. Which field to choose as the sharding key needs to be decided based on specific business conditions, for example, sharding based on user ID, sharding based on order status, etc.
After selecting the fragmentation field, we also need to consider the granularity of fragmentation. The finer the sharding granularity, the smaller the data volume of a single table will be, and the query efficiency will be improved accordingly. However, the granularity of sharding cannot be too small, and too many physical tables are not conducive to database management and maintenance.
After sharding, we need to consider how to store different data under the same sharding key in different physical tables , while ensuring the consistency of these data. There are two specific implementation methods:
(1) Vertical sharding: Put different fields in different tables. For example, the user's basic information and order information are stored in different tables. The advantage of this method is that there is no need to consider the problem of cross-table joins, and it also avoids the problem of data consistency among cross-table records.
(2) Horizontal sharding: Spread the data of the same table into multiple physical tables. The advantage of this method is that it can make full use of hardware resources while avoiding the problem of excessive data volume in a single table. However, cross-table query and data consistency issues need to be considered, and some special technologies need to be used to avoid these problems.
In cross-shard query, we need to first determine the list of shards involved in the query, and then among these shards Perform query operations. If the query conditions include the shard key, the query can be directly placed in the corresponding physical table; if the query conditions do not include the shard key, the query needs to be performed in all shards, and then the results will be merged and returned to the customer end. Although this can solve the query problem across multiple shards, it requires higher resource costs and time costs.
3. Use PHP to implement horizontal sharding
In PHP applications, we can implement horizontal sharding through frameworks such as ShardingSphere and Doctrine ORM. Here we take ShardingSphere as an example to introduce how to use this framework to implement horizontal sharding.
ShardingSphere is the first-class open source distributed database middleware in China, providing two versions: Java and PHP. The specific implementation is as follows:
"require": {
"php": " ^7.2.5",
"sharding-sphere/sharding-proxy": "^5.3",
"sharding-sphere/sharding-core": "^5.3"
}
use ShardingSphereShardingAutoConfiguration;
$parser = new ShardingSphereParserMySQLMySQLParser();
$executor = new ShardingSphereExecutorExecutorEngine ();
$shardingConfig = new ShardingAutoConfiguration();
$shardingConfig->setDataSource($dataSource); // Set data source
$shardingConfig->setShardingRule($ shardingRuleConfig); // Set sharding rules
$config = new ShardingSphereProxyCommandLineConfig();
$instance = new ShardingSphereProxyShardingInstanceLoader($parser, $executor);
$instance ->load($config, $shardingConfig);
Specific sharding rules need to be formulated based on specific business scenarios. For example, for the sharding rule of user ID, the result of dividing the user ID by 10 can be used as the sharding key, and then dispersed to multiple physical tables based on the result.
At this point, we have completed all code implementations for using ShardingSphere to implement horizontal sharding of the database.
Summary
This article introduces the concept, principle, design and practice of horizontal sharding of databases, and introduces how to use the ShardingSphere framework to implement horizontal sharding in PHP programming. Through horizontal sharding, we can split large tables into small tables, improve query efficiency, and facilitate distributed deployment and high-availability processing.
The above is the detailed content of Design and practice of horizontal sharding of database: application in PHP programming. For more information, please follow other related articles on the PHP Chinese website!