This algorithm applies when data is routed to shards by year and month. You are advised to use this algorithm together with tbpartition YYYYMM(ShardKey).
The sharding key must be DATE, DATETIME, or TIMESTAMP.
The data route depends on the remainder of the sharding key hash value divided by database shards. Enter the year and month into the hash function to obtain the hash value.
For example, YYYYMM ('2012-12-31 12:12:12') is equivalent to (2012 x 12 + 12) % D. D is the number of database or table shards.
Condition |
Calculation Method |
Example |
---|---|---|
Database sharding key ≠ Table sharding key |
Sharding key: yyyy-MM-dd Database routing result = (yyyy x 12 + MM) % Database shards Table routing result = (yyyy x 12 + MM) % Table shards |
Sharding key: 2012-11-20 Database shard: (2012 x 12 + 11) % 8 = 3 Table shard: (2012 x 12 + 11) % 3 = 2 |
Database sharding key = Table sharding key |
Sharding key: yyyy-MM-dd Table routing result = (yyyy x 12 + MM) % (Database shards x Table shards) Database routing result = Table routing result/Table shards NOTE:
Database routing result is rounded off to the nearest integer. |
Sharding key: 2012-11-20 Table shard: (2012 x 12 + 11) % (8 x 3) = 11 Database shard: 11 % 3 = 3 |
Assume that there are already 8 physical databases in your database instance. Now you want to shard data by year and month and require that data of the same month be stored in one table and each month within two years should correspond to an independent table, so that you can query data from a physical table in a physical database by the sharding key.
In this scenario, you can select the YYYYMM algorithm. Then create 24 physical tables for 24 months of the two years, each month corresponding to one table. Since you already have 8 physical databases, three physical tables should be created in each of them. The following is an example SQL statement for creating a table:
create table test_yyyymm_tb( id int, name varchar(30) DEFAULT NULL, create_time datetime DEFAULT NULL, primary key(id) ) ENGINE = InnoDB DEFAULT CHARSET = utf8 dbpartition by YYYYMM(create_time) tbpartition by YYYYMM(create_time) tbpartitions 3;
Syntax for creating tables when only database sharding is required:
create table YYYYMM( id int, name varchar(30) DEFAULT NULL, create_time datetime DEFAULT NULL, primary key(id) ) ENGINE = InnoDB DEFAULT CHARSET = utf8 dbpartition by YYYYMM(create_time);