All Products
Search
Document Center

PolarDB:Usage instructions

Last Updated:Mar 28, 2026

Multi-master sharded tables in PolarDB for MySQL distribute data across multiple database clusters automatically, enabling each node to handle both reads and writes. This document covers the DDL syntax and behavioral details you need to work with multi-master sharded tables.

When to use multi-master sharded tables

Multi-master sharded tables are designed for workloads that have outgrown a single-node MySQL setup. Consider this architecture when:

  • Write throughput has hit the limits of vertical scaling

  • A single table is large enough that query performance is degrading

  • Your application requires horizontal scale-out with linear throughput gains

Multi-master sharded tables use a shared-nothing architecture combined with on-demand resource allocation from shared storage, so each node operates independently while storage costs remain proportional to actual use.

Prerequisites

Before you begin, ensure that you have:

DDL statements

CREATE DATABASE

Use CREATE DATABASE to create a database and set its default character set and collation. The syntax is fully compatible with MySQL. To enable multi-master sharded tables in database sharding mode, set DIST_DB='Y' when creating the database. The default is 'N'.

Converting a regular database to one with the DIST_DB attribute is not supported. Set DIST_DB at creation time.

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'}
}

Example

The following statement creates a database named test1 with database sharding mode enabled. The default number of database shards is 8.

CREATE DATABASE test1 DIST_DB='Y';

CREATE TABLE

Database sharding mode

In a DIST_DB='Y' database, all tables must be multi-master sharded tables. Use the DBDISTRIBUTION BY clause to specify the distribution method. The number of database shards is fixed at 8.

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

Single database, single table (no sharding)

CREATE DATABASE test;

CREATE TABLE t1 (
    id   BIGINT NOT NULL,
    name VARCHAR(30),
    PRIMARY KEY (id)
);
Hash sharding

Use when you need to distribute a large, write-heavy table evenly across all shards. Data is split into 8 database shards by hashing the specified column.

CREATE TABLE dist1 (
    id BIGINT NOT NULL,
    c1 INT,
    c2 VARCHAR(30),
    PRIMARY KEY (id)
) DBDISTRIBUTION BY HASH(id);
List sharding

Use when your data maps to a known set of discrete integer values and you need explicit control over which values land on which shard.

DBDISTRIBUTION BY LIST(expr) distributes rows by matching the expression value against VALUES IN (value_list). The expression must return an integer; value_list is a comma-separated list of integers.

CREATE TABLE t1 (
    id         INT NOT NULL,
    store_id   INT,
    partion_no INT,
    PRIMARY KEY (id, partion_no)
) DBDISTRIBUTION BY LIST(partion_no) (
    DBDISTRIBUTION a VALUES IN (1),
    DBDISTRIBUTION b VALUES IN (2)
);
List columns sharding

Use when you need to shard by non-integer columns or multiple columns with a known set of discrete values.

DBDISTRIBUTION BY LIST COLUMNS(column_list) accepts one or more column names (not expressions). Supported column types:

  • Integer types: TINYINT, SMALLINT, MEDIUMINT, INT (INTEGER), BIGINT

  • Date types: DATE, DATETIME

  • String types: CHAR, VARCHAR, BINARY, VARBINARY

DECIMAL, FLOAT, TEXT, and BLOB are not supported as list columns sharding keys.
CREATE TABLE t3 (
    id         INT NOT NULL,
    store_id   INT,
    partion_no VARCHAR(20),
    PRIMARY KEY (id, partion_no)
) DBDISTRIBUTION BY LIST COLUMNS (partion_no) (
    DBDISTRIBUTION a VALUES IN ('id_1'),
    DBDISTRIBUTION b VALUES IN ('id_2')
);

ALTER TABLE

Compatible with MySQL syntax. See the MySQL 8.0 ALTER TABLE reference.

CREATE INDEX

Compatible with MySQL syntax. See the MySQL 8.0 CREATE INDEX reference.

Distributed query

For queries that lack a filter on the distributed key, or that span multiple database shards, route them to the global read-only node. The global read-only node aggregates data from all partitions automatically, so you can query across all shards without setting up extra storage or synchronization links.

Global auto-increment column

Multi-master sharded tables support a globally unique sequence for auto-increment columns. Key behavioral differences from standard MySQL single-node auto-increment:

  • Globally unique, not guaranteed consecutive. Values are unique across all nodes, but gaps are expected.

  • Batch allocation per primary node. Each primary node allocates a range of values at a time. When multiple primary nodes insert concurrently, the resulting IDs from different nodes may not be in order.

  • Partition-local monotonicity. Values increase monotonically within a single partition, but not globally.

The following table compares auto-increment behavior between MySQL single-node and multi-master sharded tables.

FeatureMySQL single-nodeMulti-master sharded table
One auto-increment column per tableSupportedSupported
Index requirement on auto-increment columnAn index is requiredThe auto-increment column cannot also be the distributed key. If it is, you must explicitly provide the auto-increment column value for each insert. A unique index is supported.
Insert without specifying column valueSupportedSupported
Insert with a specified column valueSupportedSupported only if the specified value is within the local sequence range; otherwise relies on the global index for duplicate checks
Globally unique and monotonically increasingSupportedGlobally unique; monotonically increasing within a partition only
Gaps in the sequenceNo gapsGaps exist
Modify the auto-increment counterSupportedSupported. The sequence cache on each local node must be invalidated.
Update auto-increment column valuesSupportedNot supported. The current behavior is equivalent to inserting a row with a specified column value.

Example: range-sharded table with auto-increment

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)
);

Insert a row without specifying the auto-increment value:

INSERT INTO t1 (c1, c2) VALUES (1, 1);

Limitations

The following DDL operations are not supported for multi-master sharded tables.

OperationConstraint
Table names containing __mt__Not allowed
ALTER TABLE {DISCARD|IMPORT} TABLESPACENot supported
ALTER TABLE PARTITION BY ...Not supported
ALTER TABLE {DATA|INDEX} DIRECTORY='<absolute path>'Not supported
ALTER TABLE TABLESPACE tablespace_name [STORAGE {DISK|MEMORY}]Not supported
ALTER TABLE ADD {FULLTEXT|SPATIAL}Not supported
CREATE VIEW / DROP VIEWNot supported
CREATE TRIGGER / PROCEDURE / FUNCTION / EVENTNot supported