This topic describes how to create and use multi-master cluster partitioned tables.
Background information
The multi-master cluster partitioned tables of PolarDB for MySQL can automatically split data into multiple physical shards in a cluster according to preset distribution rules, and multiple read/write nodes jointly carry the business load.
Through horizontal splitting, PolarDB can horizontally scale out the processing capability of databases. Meanwhile, multi-master cluster partitioned tables combine the advantage of on-demand resource allocation of shared storage, which helps improve resource utilization.
Scope of application
The product series of the PolarDB for MySQL cluster is Multi-master Cluster (Limitless) Edition, and the kernel version is 8.0.2.
A privileged account has been created. For more information, see Create and manage database accounts.
You have connected to the database cluster. For more information, see Connect to a cluster.
DDL statements
CREATE DATABASE
The CREATE DATABASE statement is used to create a database and allows you to specify the default attributes of the database, such as the default character set and collation. Its syntax is fully compatible with MySQL. In addition, you can use DIST_DB={'Y' | 'N'} to indicate whether the created database supports multi-master cluster partitioned tables in database sharding mode. The default value is 'N'.
Multi-master cluster partitioned tables currently do not support converting a regular database to a database with the DIST_DB attribute.
Syntax
CREATE {DATABASE | SCHEMA} [IF NOT EXISTS] db_name
[create_option] ...
create_option: [DEFAULT] {
CHARACTER SET [=] charset_name
| COLLATE [=] collation_name
| ENCRYPTION [=] {'Y' | 'N'}
| DIST_DB [=] {'Y' | 'N'}
}Examples
Create a database named test1 and allow multi-master cluster partitioned tables to be created in the database.
CREATE DATABASE test1 DIST_DB='Y';CREATE TABLE
Database sharding mode
The CREATE TABLE statement is used to create tables. In a database marked as DIST_DB='Y', you can use DBDISTRIBUTION to specify the distribution method of multi-master cluster partitioned tables. When the number of shards is not explicitly defined, the system automatically determines it based on the cluster configuration.
Syntax
CREATE [TEMPORARY] TABLE [IF NOT EXISTS] tbl_name
(create_definition,...)
[table_options]
[dbdistribution_options]
dbdistribution_options:
DBDISTRIBUTION BY
{ [LINEAR] HASH(expr)
| [LINEAR] KEY [ALGORITHM={1 | 2}] (column_list)
| RANGE{(expr) | COLUMNS(column_list)}
| LIST{(expr) | COLUMNS(column_list)} }
[(dbdistribution_definition [, dbdistribution_definition] ...)]
dbdistribution_definition:
DBDISTRIBUTION distribution_name
[VALUES
{LESS THAN {(expr | value_list) | MAXVALUE}
|
IN (value_list)}]
[[STORAGE] ENGINE [=] engine_name]
[COMMENT [=] 'string' ]
[DATA DIRECTORY [=] 'data_dir']
[INDEX DIRECTORY [=] 'index_dir']
[MAX_ROWS [=] max_number_of_rows]
[MIN_ROWS [=] min_number_of_rows]
[TABLESPACE [=] tablespace_name]Examples:
Regular single table
CREATE database test; CREATE TABLE t1( id bigint NOT NULL, name varchar(30), PRIMARY key(id) );Multi-master partitioned tables supporting database sharding mode
Use a hash function for splitting
Create a multi-master cluster partitioned table and use the
idcolumn for hash splitting. The data shards are distributed across the read/write nodes of the cluster.CREATE TABLE dist1( id bigint NOT NULL, c1 int, c2 varchar(30), PRIMARY key(id) ) dbdistribution BY hash(id);LIST sharding
listis a collection of enumeration value lists, which can meet the requirements of enumeration sharding. Inlistsharding,DBDISTRIBUTION BY LIST(expr)is used for definition, whereexpris the abbreviation of expression, representing the value of a column or an expression based on the column value.expris required to return an integer value. Then, each shard can be defined byVALUES IN (value_list), wherevalue_listis a comma-separated list of integer values.CREATE TABLE t1 ( id INT NOT NULL, store_id INT, partition_no INT, PRIMARY key(id, partition_no) ) DBDISTRIBUTION BY LIST(partition_no) ( DBDISTRIBUTION a VALUES IN (1), DBDISTRIBUTION b VALUES IN (2) );LIST-COLUMNS sharding
list columnsharding is very similar tolistsharding and is defined by usingDBDISTRIBUTION BY LIST COLUMNS(column_list), wherecolumn_listcan be one or more column names instead of expressions. In fact,columns()does not allow expressions other than column names.list columnssharding is no longer limited to integer columns. The applicable types include:All integer types: TINYINT, SMALLINT, MEDIUMINT, INT (INTEGER), and BIGINT.
Other numeric types: for example, DECIMAL and FLOAT are not supported.
Date types: DATE and DATETIME.
String types: CHAR, VARCHAR, BINARY, and VARBINARY. TEXT and BLOB are not supported.
CREATE TABLE t3 ( id INT NOT NULL, store_id INT, partition_no varchar(20), PRIMARY key(id, partition_no) ) DBDISTRIBUTION BY LIST COLUMNS (partition_no) ( DBDISTRIBUTION a values in ('id_1'), DBDISTRIBUTION b values in ('id_2') );
ALTER TABLE
Commonly used MySQL ALTER TABLE syntax is supported. Multi-master cluster partitioned tables do not support modifying distribution rules or some operations that require reorganizing shards. For specific limits, see the development limits in this topic.
CREATE INDEX
Commonly used MySQL CREATE INDEX syntax is supported. When you create a primary key or unique index, the index must contain all distribution keys.
Distributed queries
Multi-master cluster partitioned tables support single-shard and cross-shard queries. For cross-shard read-only queries, the system can combine the distributed query capability with Global read-only nodes to execute them. The actual behavior is subject to the cluster configuration and the supported scope of SQL statements.
Global auto-increment columns
Multi-master cluster partitioned tables support globally unique auto-increment sequences. When multiple read/write nodes write concurrently, gaps may exist in the auto-increment values, and the values are not guaranteed to be consecutive or strictly monotonically increasing. When you design business logic, do not rely on the continuity of the auto-increment values.
CREATE TABLE t1( id int NOT NULL auto_increment, c1 int, c2 int, PRIMARY key(id, c1)) dbdistribution BY RANGE (c1) ( dbdistribution p0 VALUES less than (100), dbdistribution p1 VALUES less than (1000), dbdistribution p2 VALUES less than (10000), dbdistribution p3 VALUES less than (20000) );
Query OK, 0 rows affected (0.26 sec)
INSERT INTO t1 (c1, c2) VALUES (1, 1);
Query OK, 1 row affected (0.04 sec)Limits
The following limits apply only to multi-master cluster partitioned tables:
Operation | Constraint |
Modifying distribution rules or distribution keys | Not supported. Plan the distribution keys properly when you create tables. |
Shard maintenance operations | Directly performing operations such as adding, deleting, merging, or reorganizing shards is not supported. |
Primary keys and unique indexes | They must contain all distribution keys. |
Foreign keys and some special indexes | Currently not supported. Verify the SQL compatibility before you create tables or change table structures. |
Operations related to tablespaces and data directories | Currently not supported. |