> ## Documentation Index
> Fetch the complete documentation index at: /documents/llms.txt
> Use this file to discover all available pages before exploring further.

# Data distribution

> How to configure partitioning and bucketing in the database for even data distribution and optimal query performance.

Configuring appropriate partitioning and bucketing at table creation can help to achieve even data distribution. Even data distribution means dividing the data into subsets according to certain rules and distributing them evenly across different nodes. It can also reduce the amount of data scanned and make full use of the cluster's parallel processing capability, thereby improving query performance.

> **NOTE**
>
> * After the data distribution is specified at table creation and query patterns or data characteristics in the business scenario evolves, since v3.2 the database supports [modifying certain data distribution-related properties after table creation](#optimize-data-distribution-after-table-creation-since-32) to meet the requirements for query performance in the latest business scenarios.
> * Since v3.1, you do not need to specify the bucketing key in the DISTRIBUTED BY clause when creating a table or adding a partition. The database supports random bucketing, which randomly distributes data across all buckets. For more information, see [Random bucketing](#random-bucketing-since-v31).
> * Since v2.5.7, you can choose not to manually set the number of buckets when you create a table or add a partition. The database can automatically set the number of buckets (BUCKETS). However, if the performance does not meet your expectations after the database automatically sets the number of buckets, and you are familiar with the bucketing mechanism, you can still [manually set the number of buckets](#set-the-number-of-buckets).

## Introduction

### Distribution methods in general

Modern distributed database systems generally use the following basic distribution methods: Round-Robin, Range, List, and Hash.

![Data distribution method](https://mintcdn.com/mirrorship-docs/8FakUMFrFjmoxRt5/documents/images/e452b2745b12fdbb9fc8158f.png?fit=max\&auto=format\&n=8FakUMFrFjmoxRt5\&q=85\&s=79a98b0ca34bcd064ab419692a77cac1)

* **Round-Robin**: distributes data across different nodes in a cyclic.
* **Range**: distributes data across different nodes based on the ranges of partitioning column values. As shown in the diagram, the ranges \[1-3] and \[4-6] correspond to different nodes.
* **List**: distributes data across different nodes based on the discrete values of partitioning columns, such as gender and province. Each discrete value is mapped to a node, and multiple different values might be mapped to the same node.
* **Hash**: distributes data across different nodes based on a hash function.

To achieve more flexible data partitioning, in addition to using one of the above data distribution methods, you can also combine these methods based on specific business requirements. Common combinations include Hash+Hash, Range+Hash, and Hash+List.

### Distribution methods in the database

The database supports both separate and composite use of data distribution methods.

> **NOTE**
>
> In addition to the general distribution methods, the database also supports Random distribution to simplify bucketing configuration.

Also, the database distributes data by implementing the two-level partitioning + bucketing method.

* The first level is partitioning: Data within a table can be partitioned. Supported partitioning methods are expression partitioning, range partitioning, and list partitioning. Or you can choose not to use partitioning (the entire table is regarded as one partition).
* The second level is bucketing: Data in a partition needs to be further distributed into smaller buckets. Supported bucketing methods are hash and random bucketing.

| **Distribution method** | **Partitioning and bucketing method** | **Description** |
| - | - | - |
| Random distribution | Random bucketing | The entire table is considered a partition. The data in the table is randomly distributed into different buckets. This is the default data distribution method when range distribution is not in effect (see Range distribution). |
| Hash distribution | Hash bucketing | The entire table is considered a partition. The data in the table is distributed to the corresponding buckets, which is based on the hash values of the data's bucketing key by using a hash function. |
| Range distribution | Range-based bucketing | Supported from v4.1 onwards. In shared-data mode, it is the default distribution when no `DISTRIBUTED BY` clause is specified. The entire table is considered a partition. The data in the table is distributed to the corresponding buckets, which is based on the value range of the table key. |
| Range+Random distribution | <ol><li>Range partitioning </li><li>Random bucketing </li></ol> | <ol><li>The data in the table is distributed to the corresponding partitions, which is based on the ranges where partitioning column values fall in. </li><li>The data in the partition is randomly distributed across different buckets. </li></ol> |
| Range+Hash distribution | <ol><li>Range partitioning</li><li>Hash bucketing </li></ol> | <ol><li>The data in the table is distributed to the corresponding partitions, which is based on the ranges where partitioning column values fall in.</li><li>The data in the partition is distributed to the corresponding buckets, which is based on the hash values of the data's bucketing key by using a hash function. </li></ol> |
| List+Random distribution | <ol><li>Expression partitioning or list partitioning</li><li>Random bucketing </li></ol> | <ol><li>The data in the table is partitioned based on the value lists that the partitioning columns values belongs to.</li><li>The data in the partition is randomly distributed across different buckets.</li></ol> |
| List+Hash distribution | <ol><li>Expression partitioning or List partitioning</li><li>Hash bucketing </li></ol> | <ol><li>The data in the table is partitioned based on the value lists that the partitioning columns values belongs to.</li><li>The data in the partition is distributed to the corresponding buckets, which is based on the hash values of the data's bucketing key by using a hash function.</li></ol> |

* **Random distribution**

  If you do not configure partitioning and bucketing methods at table creation, random distribution is used by default. This distribution method currently can only be used to create a Duplicate Key table.

  ```SQL theme={null}
  CREATE TABLE site_access1 (
      event_day DATE,
      site_id INT DEFAULT '10', 
      pv BIGINT DEFAULT '0' ,
      city_code VARCHAR(100),
      user_name VARCHAR(32) DEFAULT ''
  );
  -- Because no key type, ORDER BY, or bucketing method is configured, a Duplicate Key table with random distribution is created by default.
  ```

  **Note**

  In shared-data mode, if you specify an explicit key type or an `ORDER BY` clause but no `DISTRIBUTED BY` clause, range distribution is used by default instead. See [Range-based bucketing](#range-based-bucketing).

* **Hash distribution**

  ```SQL theme={null}
  CREATE TABLE site_access2 (
      event_day DATE,
      site_id INT DEFAULT '10',
      city_code SMALLINT,
      user_name VARCHAR(32) DEFAULT '',
      pv BIGINT SUM DEFAULT '0'
  )
  AGGREGATE KEY (event_day, site_id, city_code, user_name)
  -- Use hash bucketing as the bucketing method and must specify the bucketing key.
  DISTRIBUTED BY HASH(event_day,site_id); 
  ```

* **Range distribution**

  From v4.1 onwards, the database supports the **Range-based Distribution semantic**, controlled by the FE configuration `enable_range_distribution`. The data will be sequenced according to the data range of the key columns, and each tablet contains the data from a certain range.

  **Note**

  In shared-data mode, range distribution is **enabled by default**, controlled by the FE configuration `enable_range_distribution`; set it to `false` to disable this default. This configuration has no effect in shared-nothing mode.

  ```SQL theme={null}
  CREATE TABLE users (
    user_id bigint NOT NULL,
    name string NOT NULL,
    email string NULL,
    address string NULL
  ) 
  PRIMARY KEY (user_id);
  -- Because the bucketing method is not configured, while the key column is specified, range-based bucketing is used.
  ```

* **Range+Random distribution** (This distribution method currently can only be used to create a Duplicate Key table.)

  ```SQL theme={null}
  CREATE TABLE site_access3 (
      event_day DATE,
      site_id INT DEFAULT '10', 
      pv BIGINT DEFAULT '0' ,
      city_code VARCHAR(100),
      user_name VARCHAR(32) DEFAULT ''
  )
  DUPLICATE KEY(event_day,site_id,pv)
  -- Use expression partitioning as the partitioning method and configure a time function expression.
  -- You can also use range partitioning.
  PARTITION BY date_trunc('day', event_day);
  -- Because the bucketing method is not configured, random bucketing is used by default.
  ```

* **Range+Hash distribution**

  ```SQL theme={null}
  CREATE TABLE site_access4 (
      event_day DATE,
      site_id INT DEFAULT '10',
      city_code VARCHAR(100),
      user_name VARCHAR(32) DEFAULT '',
      pv BIGINT SUM DEFAULT '0'
  )
  AGGREGATE KEY(event_day, site_id, city_code, user_name)
  -- Use expression partitioning as the partitioning method and configure a time function expression.
  -- You can also use range partitioning.
  PARTITION BY date_trunc('day', event_day)
  -- Use hash bucketing as the bucketing method and must specify the bucketing key.
  DISTRIBUTED BY HASH(event_day, site_id);
  ```

* **List+Random distribution** (This distribution method currently can only be used to create a Duplicate Key table.)

  ```SQL theme={null}
  CREATE TABLE t_recharge_detail1 (
      id bigint,
      user_id bigint,
      recharge_money decimal(32,2), 
      city varchar(20) not null,
      dt date not null
  )
  DUPLICATE KEY(id)
  -- Use expression partitioning as the partitioning method and specify the partitioning column.
  -- You can also use list partitioning.
  PARTITION BY (city);
  -- Because the bucketing method is not configured, random bucketing is used by default.
  ```

* **List+Hash distribution**

  ```SQL theme={null}
  CREATE TABLE t_recharge_detail2 (
      id bigint,
      user_id bigint,
      recharge_money decimal(32,2), 
      city varchar(20) not null,
      dt date not null
  )
  DUPLICATE KEY(id)
  -- Use expression partitioning as the partitioning method and specify the partitioning column.
  -- You can also use list partitioning.
  PARTITION BY (city)
  -- Use hash bucketing as the bucketing method and must specify the bucketing key.
  DISTRIBUTED BY HASH(city,id); 
  ```

#### Partitioning

The partitioning method divides a table into multiple partitions. Partitioning primarily is used to split a table into different management units (partitions) based on the partitioning key. You can set a storage strategy for each partition, including the number of buckets, the strategy of storing hot and cold data, the type of storage medium, and the number of replicas. The database allows you to use different types of storage mediums within a cluster. For example, you can store the latest data on solid-state drives (SSDs) to improve query performance, and historical data on SATA hard drives to reduce storage costs.

| **Partitioning method** | **Scenarios** | **Methods to create partitions** |
| - | - | - |
| Expression partitioning (recommended) | Previously known as automatic partitioning. This partitioning method is more flexible and easy-to-use. It is suitable for most scenarios including querying and managing data based on continuous date ranges or enum values. | Automatically created during data loading |
| Range partitioning (Legacy) | The typical scenario is to store simple, ordered data that is often queried and managed based on continuous date/numeric ranges. For instance, in some special cases, historical data needs to be partitioned by month, while recent data needs to be partitioned by day. | Created manually, dynamically, or in batch |
| List partitioning (Legacy) | A typical scenario is to query and manage data based on enum values, and a partition needs to include data with different values for each partitioning column. For example, if you frequently query and manage data based on countries and cities, you can use this method and select `city` as the partitioning column. So a partition can store data for multiple cities belonging to the same country. | Created manually |

##### How to choose partitioning columns and granularity

* **The partitioning key is composed of one or more partitioning columns**. Selecting a proper partitioning column can effectively reduce the amount of data scanned during queries. In most business systems, partitioning based on time is commonly adopted to resolve certain issues caused by the deletion of expired data and facilitate the management of tiered storage of hot and cold data. In this case, you can use expression partitioning or range partitioning and specify a time column as the partitioning column. Additionally, if the data is frequently queried and managed based on ENUM values, you can use expression partitioning or list partitioning and specify a column including these values as the partitioning column.
* When choosing the partitioning granularity, you need to consider data volume, query patterns, and data management granularity.
  * Example 1: If the monthly data volume in a table is small, partitioning by month can reduce the amount of metadata compared to partitioning by day, thereby reducing the resource consumption of metadata management and scheduling.
  * Example 2: If the monthly data volume in a table is large and queries mostly request data of certain days, partitioning by day can effectively reduce the amount of data scanned during queries.
  * Example 3: If the data needs to expire on a daily basis, partitioning by day is recommended.

#### Bucketing

The bucketing method divides a partition into multiple buckets. Data in a bucket is referred to as a tablet.

The supported bucketing methods are [random bucketing](#random-bucketing-since-v31) (from v3.1), [hash bucketing](#hash-bucketing), and [range-based bucketing](#range-based-bucketing) (from v4.1).

* Random bucketing: When creating a table or adding partitions, you do not need to set a bucketing key. Data within a partition is randomly distributed into different buckets.

* Hash Bucketing: When creating a table or adding partitions, you need to specify a bucketing key. Data within the same partition is divided into buckets based on the values of the bucketing key, and rows with the same value in the bucketing key are distributed to the corresponding and unique bucket.

* Range-based bucketing: From v4.1, if the FE configuration `enable_range_distribution` is enabled, a table that specifies an explicit key type or an `ORDER BY` clause but no `DISTRIBUTED BY` clause distributes data within a partition by the range of those columns. Tablets can be automatically split or merged to mitigate data skew. In shared-data mode this is enabled by default; set `enable_range_distribution` to `false` to disable it. The configuration has no effect in shared-nothing mode.

The number of buckets: By default, the database automatically sets the number of buckets (from v2.5.7). You can also manually set the number of buckets. For more information, please refer to [determining the number of buckets](#set-the-number-of-buckets).

## Create and manage partitions

### Create partitions

#### Expression partitioning (recommended)

> **NOTICE**
>
> The database's shared-data mode supports the time function expression from v3.1.0 and the column expression from v3.1.1.

Since v3.0, the database has supported [expression partitioning](/documents/4.2/table_design/data_distribution/expression_partitioning.md) (previously known as automatic partitioning) which is more flexible and easy to use. This partitioning method is suitable for most scenarios such as querying and managing data based on continuous date ranges or ENUM values.

You only need to configure a partition expression at table creation, and the database will automatically create partitions during data loading. You no longer need to manually create numerous partitions in advance, nor configure dynamic partition properties.

From v3.4 onwards, expression partitioning is further optimized to unify all partitioning strategies and supported more complex solutions. It is recommended in most cases, and will replace the other partitioning strategies in future releases.

Example 1: Use a simple time function expression with a DATETIME column.

```SQL theme={null}
CREATE TABLE site_access(
    event_day DATETIME NOT NULL,
    site_id INT DEFAULT '10',
    city_code VARCHAR(100),
    user_name VARCHAR(32) DEFAULT '',
    pv BIGINT DEFAULT '0'
)
DUPLICATE KEY(event_day, site_id, city_code, user_name)
PARTITION BY time_slice(event_day, INTERVAL 7 day)
DISTRIBUTED BY HASH(event_day, site_id)
```

Example 2: Use a column expression with multiple columns.

```SQL theme={null}
CREATE TABLE t_recharge_detail1 (
    id bigint,
    user_id bigint,
    recharge_money decimal(32,2), 
    city varchar(20) not null,
    dt varchar(20) not null
)
DUPLICATE KEY(id)
PARTITION BY dt,city
DISTRIBUTED BY HASH(`id`);
```

Example 3: Use a complex time function expression with a Unix timestamp column.

```SQL theme={null}
CREATE TABLE orders (
    ts BIGINT NOT NULL,
    id BIGINT NOT NULL,
    city STRING NOT NULL
)
PARTITION BY from_unixtime(ts,'%Y%m%d');
```

Example 4: Use a mixed expression of time function expression and column expression.

```SQL theme={null}
CREATE TABLE orders (
    ts BIGINT NOT NULL,
    id BIGINT NOT NULL,
    city STRING NOT NULL
)
PARTITION BY from_unixtime(ts,'%Y%m%d'), city;
```

#### Range partitioning

[Range partitioning](/documents/4.2/table_design/data_distribution/dynamic_partitioning.md) is suitable for storing simple contiguous data, such as time series data, or continuous numerical data.

##### Manually create partitions

Define the mapping relationship between each partition and the range of partitioning column values.

* **The partitioning column is of date type.**

  ```SQL theme={null}
  CREATE TABLE site_access(
      event_day DATE,
      site_id INT,
      city_code VARCHAR(100),
      user_name VARCHAR(32),
      pv BIGINT SUM DEFAULT '0'
  )
  AGGREGATE KEY(event_day, site_id, city_code, user_name)
  PARTITION BY RANGE(event_day)(
      PARTITION p1 VALUES LESS THAN ("2020-01-31"),
      PARTITION p2 VALUES LESS THAN ("2020-02-29"),
      PARTITION p3 VALUES LESS THAN ("2020-03-31")
  )
  DISTRIBUTED BY HASH(site_id);
  ```

* **The partitioning column is of integer type.**

  ```SQL theme={null}
  CREATE TABLE site_access(
      datekey INT,
      site_id INT,
      city_code SMALLINT,
      user_name VARCHAR(32),
      pv BIGINT SUM DEFAULT '0'
  )
  AGGREGATE KEY(datekey, site_id, city_code, user_name)
  PARTITION BY RANGE (datekey) (
      PARTITION p1 VALUES LESS THAN ("20200131"),
      PARTITION p2 VALUES LESS THAN ("20200229"),
      PARTITION p3 VALUES LESS THAN ("20200331")
  )
  DISTRIBUTED BY HASH(site_id);
  ```

* **Three specific time functions can be used as partitioning columns (supported since v3.3.0).**

  When explicitly defining the mapping relationship between partitions and the ranges of partition column values, you can use a specific time function to convert the partition column values of timestamps or strings into date values, and then divide the partitions based on the converted date values.

**The partition column values are timestamps**

```SQL theme={null}
-- A 10-digit timestamp accurate to the second, for example, 1703832553.
CREATE TABLE site_access(
    event_time bigint,
    site_id INT,
    city_code SMALLINT,
    user_name VARCHAR(32),
    pv BIGINT SUM DEFAULT '0'
  )
AGGREGATE KEY(event_time, site_id, city_code, user_name)
PARTITION BY RANGE(from_unixtime(event_time)) (
    PARTITION p1 VALUES LESS THAN ("2021-01-01"),
    PARTITION p2 VALUES LESS THAN ("2021-01-02"),
    PARTITION p3 VALUES LESS THAN ("2021-01-03")
)
DISTRIBUTED BY HASH(site_id)
;

-- A 13-digit timestamp accurate to the millisecond, for example, 1703832553219.
CREATE TABLE site_access(
    event_time bigint,
    site_id INT,
    city_code SMALLINT,
    user_name VARCHAR(32),
    pv BIGINT SUM DEFAULT '0'
  )
AGGREGATE KEY(event_time, site_id, city_code, user_name)
PARTITION BY RANGE(from_unixtime_ms(event_time))(
    PARTITION p1 VALUES LESS THAN ("2021-01-01"),
    PARTITION p2 VALUES LESS THAN ("2021-01-02"),
    PARTITION p3 VALUES LESS THAN ("2021-01-03")
)
DISTRIBUTED BY HASH(site_id);
```

**The partition column values are strings**

```SQL theme={null}
CREATE TABLE site_access (
     event_time  varchar(100),
     site_id INT,
     city_code SMALLINT,
     user_name VARCHAR(32),
     pv BIGINT SUM DEFAULT '0'
)
AGGREGATE KEY(event_time, site_id, city_code, user_name)
PARTITION BY RANGE(str2date(event_time, '%Y-%m-%d'))(
    PARTITION p1 VALUES LESS THAN ("2021-01-01"),
    PARTITION p2 VALUES LESS THAN ("2021-01-02"),
    PARTITION p3 VALUES LESS THAN ("2021-01-03")
)
DISTRIBUTED BY HASH(site_id);
```

##### Dynamic partitioning

[Dynamic partitioning](/documents/4.2/table_design/data_distribution/dynamic_partitioning.md#dynamic-partitioning) related properties are configured at table creation. The database automatically creates new partitions in advance and removes expired partitions to ensure data freshness, which implements time-to-live (TTL) management for partitions.

Different from the automatic partition creation ability provided by the expression partitioning, dynamic partitioning can only periodically create new partitions based on the properties. If the new data does not belong to these partitions, an error is returned for the load job. However, the automatic partition creation ability provided by the expression partitioning can always create corresponding new partitions based on the loaded data.

##### Create multiple partitions in batch

Multiple partitions can be created in batch at and after table creation. You can specify the start and end time for all the partitions created in batch in `START()` and `END()` and the partition increment value in `EVERY()`. However, note that the range of partitions is left-closed and right-open, which includes the start time but does not include the end time. The naming rule for partitions is the same as that of dynamic partitioning.

* **The partitioning column is of date type.**

  When the partitioning column is of date type, at table creation, you can use `START()` and `END()` to specify the start date and end date for all the partitions created in batch, and `EVERY(INTERVAL xxx)` to specify the incremental interval between two partitions. Currently, the interval granularity supports `HOUR` (since v3.0), `DAY`, `WEEK`, `MONTH`, and `YEAR`.

**with the same date interval**

In the following example, the partitions created in a batch start from `2021-01-01` and end on `2021-01-04`, with a partition increment of one day:

```SQL theme={null}
CREATE TABLE site_access (
    datekey DATE,
    site_id INT,
    city_code SMALLINT,
    user_name VARCHAR(32),
    pv BIGINT DEFAULT '0'
)
DUPLICATE KEY(datekey, site_id, city_code, user_name)
PARTITION BY RANGE (datekey) (
    START ("2021-01-01") END ("2021-01-04") EVERY (INTERVAL 1 DAY)
)
DISTRIBUTED BY HASH(site_id);
```

It is equivalent to using the following `PARTITION BY` clause in the CREATE TABLE statement:

```SQL theme={null}
PARTITION BY RANGE (datekey) (
    PARTITION p20210101 VALUES [('2021-01-01'), ('2021-01-02')),
    PARTITION p20210102 VALUES [('2021-01-02'), ('2021-01-03')),
    PARTITION p20210103 VALUES [('2021-01-03'), ('2021-01-04'))
)
```

**with different date intervals**

You can create batches of date partitions with different incremental intervals by specifying different incremental intervals in `EVERY` for each batch of partitions (make sure that the partition ranges between different batches do not overlap). Partitions in each batch are created according to the `START (xxx) END (xxx) EVERY (xxx)` clause. For example:

```SQL theme={null}
CREATE TABLE site_access (
    datekey DATE,
    site_id INT,
    city_code SMALLINT,
    user_name VARCHAR(32),
    pv BIGINT DEFAULT '0'
)
DUPLICATE KEY(datekey, site_id, city_code, user_name)
PARTITION BY RANGE (datekey) (
    START ("2019-01-01") END ("2021-01-01") EVERY (INTERVAL 1 YEAR),
    START ("2021-01-01") END ("2021-05-01") EVERY (INTERVAL 1 MONTH),
    START ("2021-05-01") END ("2021-05-04") EVERY (INTERVAL 1 DAY)
)
DISTRIBUTED BY HASH(site_id);
```

It is equivalent to using the following `PARTITION BY` clause in the CREATE TABLE statement:

```SQL theme={null}
PARTITION BY RANGE (datekey) (
    PARTITION p2019 VALUES [('2019-01-01'), ('2020-01-01')),
    PARTITION p2020 VALUES [('2020-01-01'), ('2021-01-01')),
    PARTITION p202101 VALUES [('2021-01-01'), ('2021-02-01')),
    PARTITION p202102 VALUES [('2021-02-01'), ('2021-03-01')),
    PARTITION p202103 VALUES [('2021-03-01'), ('2021-04-01')),
    PARTITION p202104 VALUES [('2021-04-01'), ('2021-05-01')),
    PARTITION p20210501 VALUES [('2021-05-01'), ('2021-05-02')),
    PARTITION p20210502 VALUES [('2021-05-02'), ('2021-05-03')),
    PARTITION p20210503 VALUES [('2021-05-03'), ('2021-05-04'))
)
```

* **The partitioning column is of integer type.**

  When the data type of the partitioning column is INT, you specify the range of partitions in `START` and `END` and define the incremental value in `EVERY`. Example:

  > **NOTE**
  >
  > The partitioning column values in **START()** and **END()** need to be wrapped in double quotation marks, while the incremental value in the **EVERY()** does not need to be wrapped in double quotation marks.

**with the same numerical interval**

In the following example, the range of all the partition starts from `1` and ends at `5`, with a partition increment of `1`:

```SQL theme={null}
CREATE TABLE site_access (
    datekey INT,
    site_id INT,
    city_code SMALLINT,
    user_name VARCHAR(32),
    pv BIGINT DEFAULT '0'
)
DUPLICATE KEY(datekey, site_id, city_code, user_name)
PARTITION BY RANGE (datekey) (
    START ("1") END ("5") EVERY (1)
)
DISTRIBUTED BY HASH(site_id);
```

It is equivalent to using the following `PARTITION BY` clause in the CREATE TABLE statement:

```SQL theme={null}
PARTITION BY RANGE (datekey) (
    PARTITION p1 VALUES [("1"), ("2")),
    PARTITION p2 VALUES [("2"), ("3")),
    PARTITION p3 VALUES [("3"), ("4")),
    PARTITION p4 VALUES [("4"), ("5"))
)
```

**with different numerical intervals**

You can create batches of numerical partitions with different incremental intervals by specifying different incremental intervals in `EVERY` for each batch of partitions (make sure that the partition ranges between different batches do not overlap). Partitions in each batch are created according to the `START (xxx) END (xxx) EVERY (xxx)` clause. For example:

```SQL theme={null}
CREATE TABLE site_access (
    datekey INT,
    site_id INT,
    city_code SMALLINT,
    user_name VARCHAR(32),
    pv BIGINT DEFAULT '0'
)
DUPLICATE KEY(datekey, site_id, city_code, user_name)
PARTITION BY RANGE (datekey) (
    START ("1") END ("10") EVERY (1),
    START ("10") END ("100") EVERY (10)
)
DISTRIBUTED BY HASH(site_id);
```

* **Three specific time functions can be used as partitioning columns (supported since v3.3.0).**

  When explicitly defining the mapping relationship between partitions and the ranges of partition column values, you can use a specific time function to convert the partition column values of timestamps or strings into date values, and then divide the partitions based on the converted date values.

**The partitioning column values are timestamps**

```SQL theme={null}
-- A 10-digit timestamp accurate to the second, for example, 1703832553.
CREATE TABLE site_access(
    event_time bigint,
    site_id INT,
    city_code SMALLINT,
    user_name VARCHAR(32),
    pv BIGINT DEFAULT '0'
  )
PARTITION BY RANGE(from_unixtime(event_time)) (
    START ("2021-01-01") END ("2021-01-10") EVERY (INTERVAL 1 DAY)
)
DISTRIBUTED BY HASH(site_id);
-- A 13-digit timestamp accurate to the milliseconds, for example, 1703832553219.
CREATE TABLE site_access(
    event_time bigint,
    site_id INT,
    city_code SMALLINT,
    user_name VARCHAR(32),
    pv BIGINT DEFAULT '0'
)
PARTITION BY RANGE(from_unixtime_ms(event_time))(
    START ("2021-01-01") END ("2021-01-10") EVERY (INTERVAL 1 DAY)
)
DISTRIBUTED BY HASH(site_id);
```

**The partitioning column values are strings**

```SQL theme={null}
CREATE TABLE site_access (
     event_time  varchar(100),
     site_id INT,
     city_code SMALLINT,
     user_name VARCHAR(32),
     pv BIGINT DEFAULT '0'
)
PARTITION BY RANGE(str2date(event_time, '%Y-%m-%d'))(
    START ("2021-01-01") END ("2021-01-10") EVERY (INTERVAL 1 DAY)
)
DISTRIBUTED BY HASH(site_id);
```

#### List partitioning (since v3.1)

[List Partitioning](/documents/4.2/table_design/data_distribution/list_partitioning.md) is suitable for accelerating queries and efficiently managing data based on enum values. It is especially useful for scenarios where a partition needs to include data with different values in a partitioning column. For example, if you frequently query and manage data based on countries and cities, you can use this partitioning method and select the `city` column as the partitioning column. In this case, one partition can contain data for various cities belonging to one country.

The database stores data in the corresponding partitions based on the explicit mapping of the predefined value list for each partition.

### Manage partitions

#### Add partitions

For range partitioning and list partitioning, you can manually add new partitions to store new data. However for expression partitioning, because partitions are created automatically during data loading, you do not need to do so.

The following statement adds a new partition to table `site_access` to store data for a new month:

```SQL theme={null}
ALTER TABLE site_access
ADD PARTITION p4 VALUES LESS THAN ("2020-04-30")
DISTRIBUTED BY HASH(site_id);
```

#### Delete a partition

The following statement deletes partition `p1` from table `site_access`.

> **NOTE**
>
> This operation does not immediately delete data in a partition. Data is retained in the Trash for a period of time (one day by default). If a partition is mistakenly deleted, you can use the [RECOVER](/documents/4.2/sql-reference/sql-statements/backup_restore/RECOVER.md) command to restore the partition and its data.

```SQL theme={null}
ALTER TABLE site_access
DROP PARTITION p1;
```

#### Restore a partition

The following statement restores partition `p1` and its data to table `site_access`.

```SQL theme={null}
RECOVER PARTITION p1 FROM site_access;
```

#### View partitions

The following statement returns details of all partitions in table `site_access`.

```SQL theme={null}
SHOW PARTITIONS FROM site_access;
```

## Configure bucketing

### Random bucketing (since v3.1)

The database distributes the data in a partition randomly across all buckets. It is suitable for scenarios with small data sizes and relatively low requirements for query performance. If you do not set a bucketing method, the database uses random bucketing by default and automatically sets the number of buckets. In shared-data mode, a table that specifies a key type or an `ORDER BY` clause but no `DISTRIBUTED BY` clause uses range-based bucketing by default instead; see [Range-based bucketing](#range-based-bucketing).

However, note that if you query massive amounts of data and frequently use certain columns as filter conditions, the query performance provided by random bucketing may not be optimal. In such scenarios, it is recommended to use [hash bucketing](#hash-bucketing). When these columns are used as filter conditions for queries, only data in a small number of buckets that the query hits need to be scanned and computed, which can significantly improve query performance.

#### Limits

* You can only use random bucketing to create a Duplicate Key table.
* You cannot specify a table bucketed randomly to belong to a [Colocation Group](/documents/4.2/using_starrocks/Colocate_join.md).
* Spark Load cannot be used to load data into tables bucketed randomly.

In the following CREATE TABLE example, the `DISTRIBUTED BY xxx` statement is not used, so the database uses random bucketing by default, and automatically sets the number of buckets.

```SQL theme={null}
CREATE TABLE site_access1(
    event_day DATE,
    site_id INT DEFAULT '10', 
    pv BIGINT DEFAULT '0' ,
    city_code VARCHAR(100),
    user_name VARCHAR(32) DEFAULT ''
)
DUPLICATE KEY(event_day,site_id,pv);
```

You can also explicitly specify the clause in the statement:

```SQL theme={null}
CREATE TABLE site_access1(
    event_day DATE,
    site_id INT DEFAULT '10', 
    pv BIGINT DEFAULT '0' ,
    city_code VARCHAR(100),
    user_name VARCHAR(32) DEFAULT ''
)
DUPLICATE KEY(event_day,site_id,pv)
DISTRIBUTED BY RANDOM BUCKETS;
```

However, if you are familiar with the database' bucketing mechanism, you can also manually set the number of buckets when creating a table with random bucketing.

```SQL theme={null}
CREATE TABLE site_access2(
    event_day DATE,
    site_id INT DEFAULT '10', 
    pv BIGINT DEFAULT '0' ,
    city_code VARCHAR(100),
    user_name VARCHAR(32) DEFAULT ''
)
DUPLICATE KEY(event_day,site_id,pv)
DISTRIBUTED BY RANDOM BUCKETS 8; -- manually set the number of buckets to 8
```

### Hash bucketing

The database can use hash bucketing to subdivide data in a partition into buckets based on the bucketing key and [the number of buckets](#set-the-number-of-buckets). In hash bucketing, a hash function takes data's bucketing key value as an input and calculates a hash value. Data is stored in the corresponding bucket based on the mapping between the hash values and buckets.

#### Advantages

* Improved query performance: Rows with the same bucketing key values are stored in the same bucket, reducing the amount of data scanned during queries.

* Even data distribution: By selecting columns with higher cardinality (a larger number of unique values) as the bucketing key, data can be more evenly distributed across buckets.

#### How to choose the bucketing columns

We recommend that you choose the column that satisfies the following two requirements as the bucketing column.

* high cardinality column such as ID
* column that often used in a filter for queries

But if no columns satisfy both requirements, you need to determine the bucketing column according to the complexity of queries.

* If the query is complex, it is recommended that you select high cardinality columns as bucketing columns to ensure that the data is as evenly distributed as possible across all the buckets and improve the cluster resource utilization.
* If the query is relatively simple, it is recommended to select columns that are frequently used as filer conditions in queries as bucketing columns to improve query efficiency.

If partition data cannot be evenly distributed across all the buckets by using one bucketing column, you can choose multiple bucketing columns. Note that it is recommended to use no more than 3 columns.

#### Precautions

* **When a table is created, you must specify the bucketing columns**.
* The data types of bucketing columns must be INTEGER, DECIMAL, DATE/DATETIME, or CHAR/VARCHAR/STRING.
* Since 3.2, bucketing columns can be modified by using ALTER TABLE after table creation.

#### Examples

In the following example, the `site_access` table is created by using `site_id` as the bucketing column. Additionally, when data in the `site_access` table is queried, data is often filtered by sites. Using `site_id` as the bucketing key can prune a significant number of irrelevant buckets during queries.

```SQL theme={null}
CREATE TABLE site_access(
    event_day DATE,
    site_id INT DEFAULT '10',
    city_code VARCHAR(100),
    user_name VARCHAR(32) DEFAULT '',
    pv BIGINT SUM DEFAULT '0'
)
AGGREGATE KEY(event_day, site_id, city_code, user_name)
PARTITION BY RANGE(event_day)
(
    PARTITION p1 VALUES LESS THAN ("2020-01-31"),
    PARTITION p2 VALUES LESS THAN ("2020-02-29"),
    PARTITION p3 VALUES LESS THAN ("2020-03-31")
)
DISTRIBUTED BY HASH(site_id);
```

Suppose each partition of table `site_access` has 10 buckets. In the following query, 9 out of 10 buckets are pruned, so the database only needs to scan 1/10 of the data in the `site_access` table:

```SQL theme={null}
select sum(pv)
from site_access
where site_id = 54321;
```

However, if `site_id` is unevenly distributed and a large number of queries only request data of a few sites, using only one bucketing column can result in severe data skew, causing system performance bottlenecks. In this case, you can use a combination of bucketing columns. For example, the following statement uses `site_id` and `city_code` as bucketing columns.

```SQL theme={null}
CREATE TABLE site_access
(
    site_id INT DEFAULT '10',
    city_code SMALLINT,
    user_name VARCHAR(32) DEFAULT '',
    pv BIGINT SUM DEFAULT '0'
)
AGGREGATE KEY(site_id, city_code, user_name)
DISTRIBUTED BY HASH(site_id,city_code);
```

Practically speaking, you can use one or two bucketing columns based on your business characteristics. Using one bucketing column `site_id` is highly beneficial for short queries as it reduces data exchange between nodes, thereby enhancing the overall performance of the cluster. On the other hand, adopting two bucketing columns `site_id` and `city_code` is advantageous for long queries as it can leverage the overall concurrency of the distributed cluster to significantly improve performance.

> **NOTE**
>
> * Short queries involve scanning a small amount of data, and can be completed on a single node.
> * Long queries involve scanning a large amount of data, and their performance can be significantly improved by parallel scanning across multiple nodes in a distributed cluster.

### Range-based bucketing

From v4.1 onwards, the database supports the **Range-based Distribution semantic**, controlled by the FE configuration `enable_range_distribution`. In shared-data clusters, it is enabled by default. Range-based Distribution semantic addresses the performance issue for small-tenant, customer-facing scenarios, and allows an adaptive mechanism for handling data skew. The data will be sequenced according to the data range of the key columns, and each tablet contains the data from a certain range. For existing tables, you can split or merge tablets to achieve dynamic management of tablet size. Colocate Join is supported when creating a table with Range-based Distribution semantic.

The range-based distribution semantic is different from the default semantic in the following aspects:

* If the key type (AGGREGATE KEY/UNIQUE KEY/PRIMARY KEY/DUPLICATE KEY) is explicitly specified, and a DISTRIBUTED BY clause is not specified, the data will be distributed by range by default.
* If none of the key type, a DISTRIBUTED BY clause, or an ORDER BY is specified, a Duplicate Key table with the random bucketing strategy will be created.
* If the key type and a DISTRIBUTED BY clause are not specified, but an ORDER BY clause is specified, a Duplicate Key table with the range-based distribution strategy will be created. In this case, DUPLICATE KEY is equivalent to an ORDER BY clause, and vice versa.
* If both DUPLICATE KEY and an ORDER BY clause are specified, only the ORDER BY clause will take effect, and DUPLICATE KEY will be ignored.
* When the `colocate_with` property is specified, you must also specify the colocated columns in addition to the colocation group. The colocated columns must be a prefix of the sort columns, and the default colocated columns are the sort columns.

#### Advantages

**Fewer concepts and simpler usage**

* In most cases, table creation should require only column definitions and an `ORDER BY` clause.
* Users do not have to define a `DISTRIBUTED BY` clause.

**Ability to handle data skew**

* Tablets support automatic splitting and merging, enabling the system to re-balance data dynamically and mitigate skew. For more instructions on splitting and merging tablets, see [ALTER TABLE - Split or merge tablets](/documents/4.2/sql-reference/sql-statements/table_bucket_part_index/ALTER_TABLE.md#split-or-merge-tablets).

**Balanced data locality and distribution**

* Data from a small tenant can be located on a single BE/CN for optimal locality.
* Data from a large tenant can be distributed across multiple BEs/CNs to maintain scalability and performance.

**No dependency on time-based partitioning**

* Time-based partitioning is no longer a must for data management.
* A single partition will be capable of storing large data volumes, and the amount of data in different partitions can vary greatly, allowing users to choose partitioning strategies based on their specific needs.

#### Precautions

* This semantic is supported from v4.1 onwards. In shared-data mode it is enabled by default, controlled by the FE configuration `enable_range_distribution`.
* To disable it and fall back to the previous default distribution, set the FE configuration `enable_range_distribution` to `false`. It has no effect in shared-nothing mode.

#### Target tablet size

Automatic tablet split and merge keep the tablets of a range-distributed table around a target size. By default, every table uses the FE configuration `tablet_reshard_target_size` (Default: 10 GB). To give a table its own target size, set the table property `tablet_reshard_target_size` (Unit: bytes) to a positive integer when you create the table, or change it with `ALTER TABLE ... SET`. The table property has the same name as the FE configuration and is supported only on range-distributed tables. On any other table, the statement fails with `tablet_reshard_target_size is only supported for range-distributed tables`.

```SQL theme={null}
CREATE TABLE orders (
    tenant_id varchar(128),
    created_time datetime,
    id int,
    amount decimal(18, 2)
)
DUPLICATE KEY (tenant_id, created_time, id)
PROPERTIES ("tablet_reshard_target_size" = "1073741824");

-- Change the target size of the table to 4 GB.
ALTER TABLE orders SET ("tablet_reshard_target_size" = "4294967296");

-- Remove the property. The table follows the FE configuration tablet_reshard_target_size again.
ALTER TABLE orders SET ("tablet_reshard_target_size" = "");
```

A table with the property uses the property value in place of the FE configuration:

* Automatic split splits a tablet once it reaches 1.5 times the property value (rounded up), into tablets of about the property value.
* Automatic merge, when enabled by the FE configuration `tablet_reshard_enable_tablet_merge`, is triggered when two adjacent tablets together are smaller than 80% of the property value (rounded up), and the total size of each group of merged tablets does not exceed the property value.
* `ALTER TABLE ... SPLIT TABLET` and `ALTER TABLE ... MERGE TABLET` use the property value as the target size unless `tablet_reshard_target_size` is specified in the `PROPERTIES` of the statement.
* Tablet pre-split during data loading, online sort key changes such as `ALTER TABLE ... ORDER BY`, and rollups of the table size their new tablets against the property value.

For details, see [ALTER TABLE - Modify the tablet size](/documents/4.2/sql-reference/sql-statements/table_bucket_part_index/ALTER_TABLE.md#modify-the-tablet-size).

The table property has no default value. A table without it follows the FE configuration `tablet_reshard_target_size`, including any later change to that configuration. `0`, negative values, and non-numeric values are rejected.

If the FE configuration `tablet_reshard_target_size` is set to `0`, automatic split and merge are disabled for all tables, including tables with the table property. The table property takes effect again after the configuration is set back to a positive value.

**Note**

`SHOW CREATE TABLE` returns the `tablet_reshard_target_size` property of a table that has it. Where range distribution is not the default, that is, when the FE configuration `enable_range_distribution` is `false` or in shared-nothing mode, the returned statement would create a hash-bucketed or randomly bucketed table, so it fails because the property is supported only on range-distributed tables. A version that does not support the property rejects it as an unknown property. Remove the property before you run the statement in these environments.

#### Examples

##### Create new tables with Range-based Distribution semantic

The following examples omit the partition syntax.

**Duplicate key tables:**

```SQL theme={null}
-- When DUPLICATE KEY is specified, the data is distributed by range of the key column.
CREATE TABLE dup_table (
    tenant_id varchar(128),
    created_time datetime,
    id int, 
    name varchar(64)
)
DUPLICATE KEY (tenant_id, created_time, id);

-- When ORDER BY is specified, a Duplicate Key table is created. The data is distributed by range of the sort key.
CREATE TABLE dup_table_order (
    tenant_id varchar(128),
    created_time datetime,
    id int, 
    name varchar(64)
)
ORDER BY (tenant_id, created_time, id);

-- If both DUPLICATE KEY and ORDER BY are not specified, a Duplicate Key table with the random bucketing strategy is created.
CREATE TABLE random_table (
    tenant_id varchar(128),
    created_time datetime,
    id int, 
    name varchar(64)
);
```

**Primary Key/Aggregate Key/Unique Key tables:**

```SQL theme={null}
-- Take Primary Key table as an example.
-- When the key type and the key column are specified, the data is distributed by range of the key column.
CREATE TABLE pk_table (
    tenant_id varchar(128),
    created_time datetime,
    id int, 
    name varchar(64)
)
PRIMARY KEY (tenant_id, created_time, id);
```

**Tables with Colocate Join:**

**Note**

In addition to the colocation group, you must also specify the colocated columns in the `colocate_with` property in the format `<colocation_group>:<colocated_column>[,<colocated_column>]`:

* The colocated columns must be a prefix of the sort columns.
* Tables in the same colocation group must have the same type, number, and order of colocated columns.
* The type of a colocated column cannot be changed after the table is created, including increasing the length of a VARCHAR column.
* You can change the sort key online after the table is created (`ALTER TABLE ... ORDER BY`, adding or dropping a key column, or widening an integer sort-key column), as long as the new sort key still starts with the colocated columns, in the same order and with the same types. A change that drops, reorders, or changes the type of a colocated column is rejected. The table stays in its colocation group and keeps using Colocate Join.
* Default colocated columns are the sort columns.
* If `colocate_with` specifies only the colocation group and a sort key change makes the sort key longer than the colocated columns, the database rewrites the property to the explicit form `<colocation_group>:<colocated_column>[,<colocated_column>]`, which `SHOW CREATE TABLE` returns from then on.
* Rollups and synchronous materialized views are supported, as long as the sort key of the rollup or view starts with the colocated columns, in the same order and with the same types. A rollup defined in `CREATE TABLE ... ROLLUP (...)` is sorted by its key columns, and an `ORDER BY` written in it is not used, so its key columns must start with the colocated columns. The sort key of a synchronous materialized view is its leading non-aggregated columns, in `SELECT` order. For a view without `GROUP BY`, aggregation, or `DISTINCT`, the sort key is a prefix of the `SELECT` list (at most three columns and 36 bytes; it ends with the first VARCHAR column, or before the first column whose type cannot be a key, such as FLOAT or DOUBLE), and `ORDER BY` does not change it. To choose the sort key of such a projection, use `ALTER TABLE ... ADD ROLLUP ... ORDER BY (...)` instead. The sort key changes described above are not supported on a table that has a rollup or synchronous materialized view.

```SQL theme={null}
-- When the colocate_with property is specified, 
-- data with the same value in the colocated columns will be distributed to the same set of BE/CN.
CREATE TABLE pk_table (
    tenant_id varchar(128),
    created_time datetime,
    id int, 
    name varchar(64)
)
PRIMARY KEY (tenant_id, created_time, id)
PROPERTIES(
    "colocate_with" = "colocation_group1:tenant_id,created_time"
)
```

##### Optimize existing tables by splitting or merging tablets

For detailed instructions on splitting and merging tablets, see [ALTER TABLE - Modify the tablet size](/documents/4.2/sql-reference/sql-statements/table_bucket_part_index/ALTER_TABLE.md#modify-the-tablet-size).

* Split all tablets that meet the conditions in the table to the table's target size (10 GB by default).

```SQL theme={null}
ALTER TABLE table1 SPLIT TABLETS;
```

* Merge all tablets that meet the conditions in the table to a target size of 2 GB.

```SQL theme={null}
ALTER TABLE table1 MERGE TABLETS
PROPERTIES (
    "tablet_reshard_target_size"="2147483648");
```

#### Limitations

The following operations are not supported on tables with Range-based Distribution:

| DDL | Reason |
| - | - |
| `ALTER TABLE ... ADD ROLLUP ...` without an `ORDER BY` clause | A plain synchronous rollup assumes 1-to-1 base/rollup tablet pairing with the same row order, which range distribution semantic does not provide. Use `ALTER TABLE ... ADD ROLLUP ... ORDER BY (...)` instead: on range tables in shared-data clusters, this statement builds an independent-sort-key rollup, and you can add multiple rollups (one per `ALTER TABLE` statement). |
| `CREATE MATERIALIZED VIEW ... AS ...` (synchronous form, no `REFRESH` and no `DISTRIBUTED BY` clause) | In a shared-data cluster, a synchronous materialized view on a Duplicate Key, Aggregate, or Unique Key table is supported; see [CREATE MATERIALIZED VIEW](/documents/4.2/sql-reference/sql-statements/materialized_view/CREATE_MATERIALIZED_VIEW.md). In other cases, a synchronous materialized view is internally a plain synchronous rollup and shares the same limitation. |
| `ALTER TABLE ... OPTIMIZE` | OPTIMIZE redistributes / rebuckets a partition, which is incompatible with range tablet boundaries. |
| `CREATE TABLE ... ROLLUP (...)`, `ALTER TABLE ... ADD ROLLUP ...`, or `CREATE MATERIALIZED VIEW` on a table with `colocate_with`, when the sort key of the rollup or synchronous materialized view does not start with the colocated columns | The tablets of a rollup or synchronous materialized view must be aligned with the colocation group, and requiring its sort key to start with the colocated columns keeps every index of the table aligned. A synchronous materialized view without `GROUP BY`, aggregation, or `DISTINCT` cannot choose its sort key; use `ALTER TABLE ... ADD ROLLUP ... ORDER BY (...)` instead. |
| `ALTER TABLE ... MERGE TABLETS` on a Primary Key table whose `ORDER BY` differs from its primary key (automatic size-based merge is skipped for the same shape) | Such a table routes rows by a range in primary-key space while its segments are laid out in sort-key order, so a merge cannot decide which source tablet still owns a given row of a segment those sources share. SPLIT is unaffected, and a Primary Key table whose `ORDER BY` is its primary key can still be merged. |

For rollup-like aggregation use cases, use an **asynchronous materialized view** with an explicit `REFRESH` clause or a `DISTRIBUTED BY` clause, for example:

```sql theme={null}
CREATE MATERIALIZED VIEW mv
DISTRIBUTED BY HASH(col)
REFRESH ASYNC
AS SELECT ... FROM range_table;
```

### Set the number of buckets

Buckets reflect how data files are actually organized in the database.

#### At table creation

* Automatically set the number of buckets (recommended)

  Since v2.5.7, the database supports automatically setting the number of buckets based on machine resources and data volume for a partition.

  **Tip**

  If the raw data size of a partition exceeds 100 GB, we recommend that you manually configure the number of buckets using Method 2.

**Table configured with hash bucketing**

Example:

```sql theme={null}
CREATE TABLE site_access (
    site_id INT DEFAULT '10',
    city_code SMALLINT,
    user_name VARCHAR(32) DEFAULT '',
    event_day DATE,
    pv BIGINT SUM DEFAULT '0')
AGGREGATE KEY(site_id, city_code, user_name,event_day)
PARTITION BY date_trunc('day', event_day)
DISTRIBUTED BY HASH(site_id,city_code); -- do not need to set the number of buckets
```

**Table configured with random bucketing**

As for tables configured with random bucketing, in addition to automatically setting the number of buckets in a partition, the database further optimizes the logic since v3.2.0. The database also can **dynamically increase** the number of buckets in a partition **during data loading**, based on the cluster capacity and the volume of loaded data.

**Warning**

* To enable the on-demand and dynamic increase of the number of buckets, you need to set the table property `PROPERTIES("bucket_size"="xxx")` to specify the size of a single bucket. If the data volume in a partition is small, you can set the `bucket_size` to 1 GB. Otherwise, you can set the `bucket_size` to 4 GB.
* Once the on-demand and dynamic increase of the number of buckets is enabled, and you need to rollback to version 3.1, you have to first delete the table which enables the dynamic increase in the number of buckets. Then you need to manually execute a metadata checkpoint using [ALTER SYSTEM CREATE IMAGE](/documents/4.2/sql-reference/sql-statements/cluster-management/nodes_processes/ALTER_SYSTEM.md) before rolling back.

Example:

```sql theme={null}
CREATE TABLE details1 (
    event_day DATE,
    site_id INT DEFAULT '10', 
    pv BIGINT DEFAULT '0',
    city_code VARCHAR(100),
    user_name VARCHAR(32) DEFAULT '')
DUPLICATE KEY (event_day,site_id,pv)
PARTITION BY date_trunc('day', event_day)
DISTRIBUTED BY RANDOM
-- The number of buckets in a partition is automatically determined by StarRocks and the number dynamically increases on demand because the size of a bucket is set to 1 GB.
PROPERTIES("bucket_size"="1073741824")
;

CREATE TABLE details2 (
    event_day DATE,
    site_id INT DEFAULT '10',
    pv BIGINT DEFAULT '0' ,
    city_code VARCHAR(100),
    user_name VARCHAR(32) DEFAULT '')
DUPLICATE KEY (event_day,site_id,pv)
PARTITION BY date_trunc('day', event_day)
DISTRIBUTED BY RANDOM
-- The number of buckets in the table partition is automatically determined by StarRocks, and the number is fixed and does not dynamically increases on demand because the size of a bucket is not set.
;
```

* Manually set the number of buckets

  Since v2.4.0, the database supports using multiple threads to scan a tablet in parallel during a query, thereby reducing the dependency of scanning performance on the tablet count. We recommend that each tablet contain about 10 GB of raw data. For shared-data clusters of v4.1 or later, which support large-capacity tablets, we recommend that each tablet contain 10 GB to 50 GB of data, and no more than 100 GB. If you intend to manually set the number of buckets, you can estimate the amount of data in each partition of a table and then decide the number of tablets.

  To enable parallel scanning on tablets, make sure the `enable_tablet_internal_parallel` parameter is set to `TRUE` globally for the entire system (`SET GLOBAL enable_tablet_internal_parallel = true;`).

**Table configured with hash bucketing**

```SQL theme={null}
CREATE TABLE site_access (
    site_id INT DEFAULT '10',
    city_code SMALLINT,
    user_name VARCHAR(32) DEFAULT '',
    event_day DATE,
    pv BIGINT SUM DEFAULT '0')
AGGREGATE KEY(site_id, city_code, user_name,event_day)
PARTITION BY date_trunc('day', event_day)
DISTRIBUTED BY HASH(site_id,city_code) BUCKETS 30;
-- Suppose the amount of raw data that you want to load into a partition is 300 GB.
-- Because we recommend that each tablet contain 10 GB of raw data, the number of buckets can be set to 30.
DISTRIBUTED BY HASH(site_id,city_code) BUCKETS 30;
```

**Table configured with random bucketing**

```sql theme={null}
CREATE TABLE details (
    site_id INT DEFAULT '10', 
    city_code VARCHAR(100),
    user_name VARCHAR(32) DEFAULT '',
    event_day DATE,
    pv BIGINT DEFAULT '0'
)
DUPLICATE KEY (site_id,city_code)
PARTITION BY date_trunc('day', event_day)
DISTRIBUTED BY RANDOM BUCKETS 30
; 
```

#### After table creation

* Automatically set the number of buckets (recommended)

  Since v2.5.7, the database supports automatically setting the number of buckets based on machine resources and data volume for a partition.

  **Tip**

  If the raw data size of a partition exceeds 100 GB, we recommend that you manually configure the number of buckets using Method 2.

**Table configured with hash bucketing**

```sql theme={null}
-- Automatically set the number of buckets for all partitions.
ALTER TABLE site_access DISTRIBUTED BY HASH(site_id,city_code);

-- Automatically set the number of buckets for specific partitions.
ALTER TABLE site_access PARTITIONS (p20230101, p20230102)
DISTRIBUTED BY HASH(site_id,city_code);

-- Automatically set the number of buckets for new partitions.
ALTER TABLE site_access ADD PARTITION p20230106 VALUES [('2023-01-06'), ('2023-01-07'))
DISTRIBUTED BY HASH(site_id,city_code);
```

**Table configured with random bucketing**

As for tables configured with random bucketing, in addition to automatically setting the number of buckets in a partition, the database further optimizes the logic since v3.2.0. The database also can **dynamically increase** the number of buckets in a partition **during data loading**, based on the cluster capacity and the volume of loaded data. This not only makes partition creation easier but also increases bulk load performance.

**Warning**

* To enable the on-demand and dynamic increase of the number of buckets, you need to set the table property `PROPERTIES("bucket_size"="xxx")` to specify the size of a single bucket. If the data volume in a partition is small, you can set the `bucket_size` to 1 GB. Otherwise, you can set the `bucket_size` to 4 GB.
* Once the on-demand and dynamic increase of the number of buckets is enabled, and you need to rollback to version 3.1, you have to first delete the table which enables the dynamic increase in the number of buckets. Then you need to manually execute a metadata checkpoint using [ALTER SYSTEM CREATE IMAGE](/documents/4.2/sql-reference/sql-statements/cluster-management/nodes_processes/ALTER_SYSTEM.md) before rolling back.

```sql theme={null}
-- The number of buckets for all partitions is automatically set by StarRocks and this number is fixed because the on-demand and dynamic increase of the number of buckets is disabled.
ALTER TABLE details DISTRIBUTED BY RANDOM;
-- The number of buckets for all partitions is automatically set by StarRocks and the on-demand and dynamic increase of the number of buckets is enabled.
ALTER TABLE details SET("bucket_size"="1073741824");

-- Automatically set the number of buckets for specific partitions.
ALTER TABLE details PARTITIONS (p20230103, p20230104)
DISTRIBUTED BY RANDOM;

-- Automatically set the number of buckets for new partitions.
ALTER TABLE details ADD PARTITION  p20230106 VALUES [('2023-01-06'), ('2023-01-07'))
DISTRIBUTED BY RANDOM;
```

* Manually set the number of buckets

  You can also manually specify the buckets number. To calculate the number of buckets for a partition, you can refer to the approach used when manually setting the number of buckets at table creation, [as mentioned above](#at-table-creation).

**Table configured with hash bucketing**

```sql theme={null}
-- Manually set the number of buckets for all partitions 
ALTER TABLE site_access
DISTRIBUTED BY HASH(site_id,city_code) BUCKETS 30;
-- Manually set the number of buckets for specific partitions.
ALTER TABLE site_access
partitions p20230104
DISTRIBUTED BY HASH(site_id,city_code)  BUCKETS 30;
-- Manually set the number of buckets for new partitions.
ALTER TABLE site_access
ADD PARTITION p20230106 VALUES [('2023-01-06'), ('2023-01-07'))
DISTRIBUTED BY HASH(site_id,city_code) BUCKETS 30;
```

**Table configured with random bucketing**

```sql theme={null}
-- Manually set the number of buckets for all partitions 
ALTER TABLE details
DISTRIBUTED BY RANDOM BUCKETS 30;
-- Manually set the number of buckets for specific partitions.
ALTER TABLE details
partitions p20230104
DISTRIBUTED BY RANDOM BUCKETS 30;
-- Manually set the number of buckets for new partitions.
ALTER TABLE details
ADD PARTITION p20230106 VALUES [('2023-01-06'), ('2023-01-07'))
DISTRIBUTED BY RANDOM BUCKETS 30;
```

Manually set the default number of buckets for dynamic partitions.

```sql theme={null}
ALTER TABLE details_dynamic
SET ("dynamic_partition.buckets"="xxx");
```

#### View the number of buckets

After creating a table, you can execute [SHOW PARTITIONS](/documents/4.2/sql-reference/sql-statements/table_bucket_part_index/SHOW_PARTITIONS.md) to view the number of buckets set by the database for each partition. Tables configured with hash bucketing have a fixed number of buckets per partition.

**Info**

* As for a table configured with random bucketing which enable the on-demand and dynamic increase of the number of buckets, the number of buckets in each partition dynamically increases. So the returned result displays the current number of buckets for each partition.
* For this table type, the actual hierarchy within a partition is as follows: partition > subpartition > bucket. To increase the number of buckets, the database actually adds a new subpartition which includes a certain number of buckets. As a result, the SHOW PARTITIONS statement may return multiple data rows with the same partition name, which show the information of the subpartitions within the same partition.

## Optimize data distribution after table creation (since 3.2)

> **NOTICE**
>
> The database's shared-data mode currently does not support this feature.

As query patterns and data volume evolve in business scenarios, the configurations specified at table creation, such as the bucketing method, the number of buckets, and the sort key, may no longer be suitable for the new business scenario and even may cause query performance to decrease. At this point, you can use `ALTER TABLE` to modify the bucketing method, the number of buckets, and the sort key to optimize data distribution. For example:

* **Increase the number of buckets when data volume within partitions is significantly increased**

  When the data volume within partitions becomes significantly larger than before, it is necessary to modify the number of buckets to maintain tablet sizes generally within the range of 1 GB to 10 GB.

* **Modify the bucketing key to avoid data skew**

  When the current bucketing key can cause data skew (for example, only the `k1` column is configured as the bucketing key), it is necessary to specify more suitable columns or add additional columns to the bucketing key. For example:

  ```SQL theme={null}
  ALTER TABLE t DISTRIBUTED BY HASH(k1, k2) BUCKETS 20;
  -- When the StarRocks's version is 3.1 or later, and the table is Duplicate Key table, you can consider directly using the system's default bucketing settings, that is, random bucketing and the number of buckets automatically set by StarRocks.
  ALTER TABLE t DISTRIBUTED BY RANDOM;
  ```

* **Adapting the sort key due to changes in query patterns**

  If the business query patterns are significantly changed and additional columns are used as conditional columns, it can be beneficial to adjust the sort key. For example:

  ```SQL theme={null}
  ALTER TABLE t ORDER BY k2, k1;
  ```

For more information, see [ALTER TABLE](/documents/4.2/sql-reference/sql-statements/table_bucket_part_index/ALTER_TABLE.md).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.