Selecting a Sharding Key and a Sharding Algorithm
For details, see Table 1.
| Sharding Algorithm | Hash | Range | ||
|---|---|---|---|---|
| Sharding Key | Table field | Table field+date function | Table field | Table field+date function |
| Description | Evenly distributes data to shards by the specified table field. | Evenly distributes data to shards by the specified table field and date function. The table field must be of the date type such as date, datetime, or timestamp. | Distributes data to specific shards according to the rules defined in the algorithm metadata. | Distributes data to shards by the specified table field and date function according to the rules defined in the algorithm metadata. The table field must be of the date type such as date, datetime, or timestamp). |
| Application Scenario | Applicable to scenarios requiring even data distribution. For example, for banking applications, the service logical entities are customers. In this case, use the table field corresponding to customers (for example, customer numbers) as the sharding key. | Applicable to scenarios requiring data splitting by time (year, month, day, week, and their combinations). For example, for game applications, use the table field corresponding to players (for example, player registration time) as the sharding key. Sharding by day, month, or year enables you to easily collect and query operation statistics of players for a specified day or month. This helps game vendors to conduct big data analysis. | Applicable to scenarios with a large number of range operations. For example, for e-commerce applications, if service scenarios focus on promotional activities and the service logical entities are activity dates, use the table field corresponding to activity dates (for example, activity name and date range) as the sharding key. This helps you collect statistics about the sales volume for a specified. | For example, when performing log analysis, you can select the time field as the sharding key because the log system may contain various types of complicated information. You can then shard data using the date function. To facilitate log clearing and dumping, select the range algorithm and convert the time field value into year using the date function so that logs are stored in shards by year. For details, see examples in the following passages. |
Selecting a Sharding Algorithm
A sharding algorithm shards data in logical tables to multiple shards. DDM supports hash and range algorithms.
- Hash
This algorithm evenly distributes data across shards.
It is applicable to SQL query conditions that frequently use the operators = and IN.
- Range
This algorithm stores records in tables based on the value range specified in the algorithm metadata.
It is applicable to SQL query conditions that frequently use the operators greater (>), less (<), and BETWEEN ... AND ...
Select an appropriate algorithm based on your service requirements to improve efficiency.
During a seamless schema scale-out:
- Data of logical tables sharded by the hash algorithm is migrated and redistributed evenly across all shards.
- By default, data of logical tables sharded by the range algorithm is not migrated. If you modify the splitting rule and redefine the range, connect to the RDS shards to complete data migration.
Selecting a Sharding Key
A sharding key is a data table field used for generating route results during horizontal sharding of logical tables. After specifying a table field, you can further select a date function. Alternatively, enter "date function (field name)". The data table field must be of the date type, such as date, datetime, or timestamp. A date function is applied to data table sharding by time (year, month, day, week, or their combinations).
DDM calculates the route results based on the sharding key and sharding algorithm, executes horizontal sharding for the data, and distributes the data to shards.
- Ensure that data is evenly distributed to each shard.
- The sharding key should be the most frequently used or the most important query condition.
- You are advised to select the primary key as the sharding key. The query speed is the fastest when the primary key is used as the query condition.
Service Scenarios with a Clear Entity
Generally, the data volume of a sharded table reaches tens of million entries. Therefore, selecting an appropriate sharding key and a sharding algorithm is important. If the service entity can be identified and most database operations are performed on data of the entity, the table field corresponding to the entity can be selected as the sharding key for horizontal sharding.
The service logical entity depends on actual application scenarios. In the following scenarios, a clear service logical entity exists.
- For banking applications, the service logical entities are customers. In this case, use the table field corresponding to customers (for example, customer numbers) as the sharding key. Service scenarios of some systems are based on bank cards or accounts. In such cases, select the bank card or account as the sharding key.
- For e-commerce applications, if service scenarios are based on products, the service logical entities are products. In this case, use the table field corresponding to products (for example, product code) as the sharding key.
- Game applications mainly focus on player data, and the service logical entities are players. In this case, use the table field corresponding to players (for example, player ID) as the sharding key.
For example, for a bank service, a SQL statement for table creation is as follows:
CREATE TABLE PERSONALACCOUNT ( ACCOUNT VARCHAR(20) NOT NULL PRIMARY KEY, NAME VARCHAR(60) NOT NULL, TYPE VARCHAR(10) NOT NULL, AVAILABLEBALANCE DECIMAL(18,2) NOT NULL, STATUS CHAR(1) NOT NULL, CARDNO VARCHAR(24) NOT NULL, CUSTOMID VARCHAR(15) NOT NULL ) ENGINE=INNODB DEFAULT CHARSET=UTF8;
Figure 1 shows the example SQL statement for table creation.
Service Scenarios Without a Clear Entity
If no suitable entity is found in a service scenario, select the table field corresponding to attributes with even data distribution as the sharding key.
For example, in a log analysis scenario, the log system may contain complex information. In this case, select the time field as the sharding key.
When the time field is selected as the sharding key, you can further specify a date function for the sharding key for data sharding.
To facilitate log clearing and dumping, select the range algorithm and convert the time field value into year using the date function so that logs are stored in shards by year. For details, see Figure 2.
SQL statement for creating a table:
CREATE TABLE LOG ( LOGTIME DATETIME NOT NULL, LOGSOURCESYSTEM VARCHAR(100), LOGDETAIL VARCHAR(10000) );
Last Article: Determining the Number of Shards in a Schema
Next Article: Formulating Sharding Rules


Did this article solve your problem?
Thank you for your score!Your feedback would help us improve the website.