---
title: "Table partitioning: RANGE, HASH and LIST"
description: "Partition large CoraDB tables by RANGE, HASH or LIST: partition keys, pruning, adding, dropping and promoting partitions, local indexes."
url: "https://www.coradb.ai/manual/sql/partition.html"
markdown_url: "https://www.coradb.ai/manual/markdown/sql/partition.md"
product: "CoraDB"
version: "1.0"
keywords: "coradb partition, table partitioning, range partition, hash partition, list partition, partition pruning, reorganize partition, promote partition, coalesce partition, db_partition"
---

# Table partitioning: RANGE, HASH and LIST

*Partitioning* divides one table into several physical parts, the *partitions*, each holding the rows whose *partitioning key* falls into its range or list, or hashes to it. Applications keep using the table as before: **INSERT**, **UPDATE**, **DELETE** and **SELECT** statements name the partitioned table, and CoraDB places and finds the rows in the right partitions.

Partitioning helps with large tables:

- **Faster queries.** A query that filters on the partitioning key only reads the partitions that can contain matching rows ([partition pruning](https://www.coradb.ai/manual/sql/partition.html#partition-pruning)).
- **Easier data management.** Old data can be removed by dropping a partition, or detached as its own table by promoting it, instead of deleting rows one by one.
- **Smaller indexes.** Every index is local to its partition, which also spreads I/O.

How it works:

- Each partition is stored as a table of its own, a subclass of the partitioned table, named `<table>__p__<partition>`.
- **INSERT** puts each row into the partition that matches its key. A row that matches no partition is rejected with `Appropriate partition does not exist.`
- **UPDATE** moves a row to another partition when its key changes.
- The partitions of a table are listed in the catalog view [db_partition](https://www.coradb.ai/manual/sql/catalog_views.html#db_partition) and shown by **SHOW CREATE TABLE**.

Partitioning pays off for large tables; what "large" means depends on the application and its queries. Choose [range](https://www.coradb.ai/manual/sql/partition.html#range-partitioning), [list](https://www.coradb.ai/manual/sql/partition.html#list-partitioning) or [hash](https://www.coradb.ai/manual/sql/partition.html#hash-partitioning) partitioning by how the table is queried and how its data is distributed, and read the [notes](https://www.coradb.ai/manual/sql/partition.html#notes-on-partitioning) below.


## Partitioning key

The partitioning key is a column, or an expression on one column, whose value decides the partition of each row.

- It uses exactly one column of the table (`Only one column can be specified as partition column.`).
- The column must be of type **CHAR**, **VARCHAR**, **SMALLINT**, **INT**, **BIGINT**, **DATE**, **TIME**, **TIMESTAMP** or **DATETIME** (`Unsupported partition column type.` otherwise, for example for **NUMERIC**).
- The expression cannot contain aggregate or analytic functions, logical or comparison operators, or any of these: **CASE**, **CHARSET**, **CHR**, **COALESCE**, **DECODE**, **DECR**, **INCR**, **DRAND**, **DRANDOM**, **GREATEST**, **LEAST**, **IF**, **IFNULL**, **INSTR**, **NVL**, **NVL2**, **ROWNUM**, **INST_NUM**, **USER**, **PRIOR**, **WIDTH_BUCKET**, **SERIAL_CURRENT_VALUE**, **SERIAL_NEXT_VALUE** (`Invalid partition definition.`).
- The expression can be at most 1,024 bytes long.
- The partitioning column must be part of every unique index, including the primary key; see [Indexes on partitioned tables](https://www.coradb.ai/manual/sql/partition.html#indexes-on-partitioned-tables).

A typical key is `YEAR(order_date)` for an orders table.


## Range partitioning

Range partitioning assigns each partition a continuous, non-overlapping range of key values. It suits data that is naturally divided into intervals, such as order dates, and it gives the best [pruning](https://www.coradb.ai/manual/sql/partition.html#partition-pruning), because both range and equality conditions can be used.

```sql
CREATE TABLE table_name ( ... )
PARTITION BY RANGE ( partitioning_key ) (
    PARTITION partition_name VALUES LESS THAN ( range_value | MAXVALUE ) [COMMENT 'comment'],
    ...
);

ALTER TABLE table_name
PARTITION BY RANGE ( partitioning_key ) ( ... );
```

- A partition holds the rows whose key is less than its *range_value* and at least the *range_value* of the previous partition. CoraDB sorts the partitions by *range_value*.
- **MAXVALUE** gives the last partition no upper limit.
- A row whose key is **NULL** goes to the partition with the smallest range.
- **ALTER TABLE … PARTITION BY** partitions an existing table.


The following table keeps an archive of e-commerce orders by year:

```sql
CREATE TABLE order_archive (
    id            INT,
    customer_id   INT,
    order_date    DATE,
    shipping_cost NUMERIC(10,2)
)
PARTITION BY RANGE (YEAR(order_date)) (
    PARTITION before_2023 VALUES LESS THAN (2023) COMMENT 'orders 2021-2022',
    PARTITION y2023       VALUES LESS THAN (2024),
    PARTITION y2024       VALUES LESS THAN (2025)
);

INSERT INTO order_archive
SELECT id, customer_id, order_date, shipping_cost FROM orders WHERE order_date < DATE'2025-01-01';   -- 2,124 rows
```

| Partition | Range of YEAR(order_date) | Rows |
|---|---|---|
| before_2023 | Below 2023 | 898 |
| y2023 | 2023 | 551 |
| y2024 | 2024 | 675 |

Orders from 2025 have no partition yet, so `INSERT INTO order_archive … WHERE order_date >= DATE'2025-01-01'` fails with `Appropriate partition does not exist.` until a partition is [added](https://www.coradb.ai/manual/sql/partition.html#adding-partitions).


## Hash partitioning

Hash partitioning spreads rows over a fixed number of partitions by a hash of the key. It suits keys for which ranges or lists have no meaning, such as customer IDs; with evenly distributed key values, the partitions are about the same size. Pruning works only for equality conditions (`=`, **IN**), so hash partitioning helps mainly when most queries look up single key values.

```sql
CREATE TABLE table_name ( ... )
PARTITION BY HASH ( partitioning_key ) PARTITIONS number_of_partitions;

ALTER TABLE table_name
PARTITION BY HASH ( partitioning_key ) PARTITIONS number_of_partitions;
```

Partitions are named `p0`, `p1`, and so on. A row whose key is **NULL** goes to the first partition. Comments cannot be given to hash partitions.


```sql
CREATE TABLE customer_h (
    id           INT PRIMARY KEY,
    company_name VARCHAR(64)
)
PARTITION BY HASH (id) PARTITIONS 4;

INSERT INTO customer_h SELECT id, company_name FROM customer;   -- 1,000 rows, about 250 per partition
```


## List partitioning

List partitioning assigns each partition an explicit list of key values. The lists must not overlap. It suits keys with a small set of meaningful values, such as country codes. As with hash partitioning, pruning works only for equality conditions.

```sql
CREATE TABLE table_name ( ... )
PARTITION BY LIST ( partitioning_key ) (
    PARTITION partition_name VALUES IN ( value [, value ...] ) [COMMENT 'comment'],
    ...
);

ALTER TABLE table_name
PARTITION BY LIST ( partitioning_key ) ( ... );
```


```sql
CREATE TABLE city_l (id INT, name VARCHAR(32), country_code VARCHAR(8))
PARTITION BY LIST (country_code) (
    PARTITION europe       VALUES IN ('AUT','BEL','CHE','DEU','DNK','ESP','FIN','FRA','GBR','IRL','ITA','NLD','NOR','POL','PRT','SWE') COMMENT 'EU, UK, CH, NO',
    PARTITION americas     VALUES IN ('ARG','BRA','CAN','MEX','USA','VEN'),
    PARTITION asia_pacific VALUES IN ('AUS','JPN','SGP')
);

INSERT INTO city_l SELECT id, name, country_code FROM city;   -- europe 70, americas 40, asia_pacific 5
```

A key value that is in no list is rejected, and **NULL** is no exception: to accept **NULL** keys, include `NULL` in one of the lists, for example `VALUES IN ('AUS','JPN','SGP', NULL)`.


### Partition comments

Range and list partitions can have a comment (`COMMENT '…'` after the partition definition); hash partitions cannot. Comments are shown by **SHOW CREATE TABLE** and in [db_partition](https://www.coradb.ai/manual/sql/catalog_views.html#db_partition):

```sql
SELECT partition_name, partition_class_name, partition_type, comment
FROM db_partition
WHERE class_name = 'order_archive'
ORDER BY partition_name;
```

For the table above:

| partition_name | partition_class_name | partition_type | comment |
|---|---|---|---|
| before_2023 | order_archive__p__before_2023 | RANGE | orders 2021-2022 |
| y2023 | order_archive__p__y2023 | RANGE | NULL |
| y2024 | order_archive__p__y2024 | RANGE | NULL |

In corasql, the session command `;schema order_archive` shows the partitions as well.


## Partition pruning

*Partition pruning* limits a query to the partitions that can contain matching rows. CoraDB examines the **WHERE** clause when the query is executed and skips every partition for which the condition is always false. This reduces disk I/O and the number of rows processed.

Pruning happens only when:

- The partitioning key expression appears in the **WHERE** clause exactly as it was defined, not wrapped in another expression;
- For range partitioning, it is used in a range condition (`<`, `>`, **BETWEEN**, …) or an equality condition (`=`, **IN**);
- For list and hash partitioning, it is used in an equality condition.

With `order_archive` partitioned by `YEAR(order_date)` (one partition per year 2021–2025, 2,910 rows in all), [SHOW TRACE](https://www.coradb.ai/manual/sql/tuning_plan.html#query-profiling) shows how many rows each query reads:

```sql
SET TRACE ON;
SELECT COUNT(*) FROM order_archive WHERE YEAR(order_date) >= 2024;
SHOW TRACE;
```

```text
Trace Statistics:
  SELECT (time: 0, fetch: 14, ioread: 0)
    SCAN (table: order_archive), (heap time: 0, fetch: 7, ioread: 0, readrows: 1461, rows: 1461)
```

Only the partitions for 2024 and 2025 are read (1,461 rows). The same filter written on the column itself does not use the key expression, so all partitions are read:

```sql
SELECT COUNT(*) FROM order_archive WHERE order_date >= DATE'2024-01-01';
SHOW TRACE;
```

```text
    SCAN (table: order_archive), (heap time: 0, fetch: 13, ioread: 0, readrows: 2910, rows: 1461)
```

More cases:

| Condition | Partitions read | Rows read |
|---|---|---|
| `YEAR(order_date) = 2023` | y2023 | 551 |
| `YEAR(order_date) BETWEEN 2022 AND 2023` | y2022, y2023 | 1,033 |
| `order_date = DATE'2023-06-01'` | All: the key expression is not used | 2,910 |
| `YEAR(order_date) + 1 = 2024` | All: the key is wrapped in another expression | 2,910 |
| `YEAR(order_date) <> 2023` | All: no usable condition | 2,910 |

Pruning is done at execution time, so the query plan does not show which partitions are skipped.

### Accessing a partition directly

A partition can be queried on its own, either through the **PARTITION** clause or by the name of its table:

```sql
SELECT COUNT(*) FROM order_archive PARTITION (y2023);    -- 551
SELECT COUNT(*) FROM order_archive__p__y2023;            -- 551
```

The query then sees only that partition, regardless of its **WHERE** clause, and an **INSERT** or **UPDATE** fails if the row does not belong to the partition. This can be useful for optimizations that are not available on partitioned tables (see [Restrictions](https://www.coradb.ai/manual/sql/partition.html#restrictions-on-partitioned-tables)), but applications that name partitions lose the main benefit of partitioning: that partitions can be reorganized without changing the application. Inserting through a partition brings no performance gain.

## Partitioning management

Partitions are managed with clauses of **ALTER TABLE**:

| Task | Clause | Range | List | Hash |
|---|---|---|---|---|
| [Add partitions](https://www.coradb.ai/manual/sql/partition.html#adding-partitions) | **ADD PARTITION** | ✓ | ✓ | ✓ (number of partitions) |
| [Drop partitions and their data](https://www.coradb.ai/manual/sql/partition.html#dropping-partitions) | **DROP PARTITION** | ✓ | ✓ | |
| [Split or merge partitions](https://www.coradb.ai/manual/sql/partition.html#partition-reorganization) | **REORGANIZE PARTITION** | ✓ | ✓ | |
| [Reduce the number of partitions](https://www.coradb.ai/manual/sql/partition.html#hash-partitioning-reorganization) | **COALESCE PARTITION** | | | ✓ |
| [Turn partitions into tables](https://www.coradb.ai/manual/sql/partition.html#partition-promotion) | **PROMOTE PARTITION** | ✓ | ✓ | |
| [Remove partitioning](https://www.coradb.ai/manual/sql/partition.html#modifying-a-partitioned-table-into-a-regular-table) | **REMOVE PARTITIONING** | ✓ | ✓ | ✓ |

These statements move data between partitions where needed, which can take a long time on large tables. Plan them accordingly.


### Adding partitions

```sql
ALTER TABLE table_name ADD PARTITION ( partition_definition [, partition_definition ...] );
```

```sql
ALTER TABLE order_archive ADD PARTITION (
    PARTITION y2025 VALUES LESS THAN (2026),
    PARTITION later VALUES LESS THAN MAXVALUE
);

INSERT INTO order_archive
SELECT id, customer_id, order_date, shipping_cost FROM orders WHERE order_date >= DATE'2025-01-01';   -- 786 rows, all in y2025
```

- The table must already be partitioned.
- For range partitioning, new ranges must be above all existing ones (`Partition addition is to set with a bigger range than the existing.`). Once a partition has **MAXVALUE**, use [REORGANIZE PARTITION](https://www.coradb.ai/manual/sql/partition.html#partition-reorganization) to split it instead.
- For hash partitioning, see [Hash partitioning reorganization](https://www.coradb.ai/manual/sql/partition.html#hash-partitioning-reorganization).


### Dropping partitions

```sql
ALTER TABLE table_name DROP PARTITION partition_name [, partition_name ...];
```

```sql
ALTER TABLE order_archive DROP PARTITION later;
```

- All rows in a dropped partition are deleted; the number of deleted rows is not reported. To keep the data, [reorganize](https://www.coradb.ai/manual/sql/partition.html#partition-reorganization) or [promote](https://www.coradb.ai/manual/sql/partition.html#partition-promotion) the partition instead; to delete rows but keep the partition, use **DELETE**.
- At least one partition must remain.
- Not available for hash partitioning (`Invalid partition requests.`); use **COALESCE PARTITION**.


### Partition reorganization

**REORGANIZE PARTITION** splits one partition into several or merges several into one. Rows are moved to match the new definitions.

```sql
ALTER TABLE table_name
REORGANIZE PARTITION partition_name [, partition_name ...]
INTO ( partition_definition [, partition_definition ...] );
```

Split `before_2023` into one partition per year, then merge them again:

```sql
ALTER TABLE order_archive REORGANIZE PARTITION before_2023 INTO (
    PARTITION y2021 VALUES LESS THAN (2022),    -- 416 rows
    PARTITION y2022 VALUES LESS THAN (2023)     -- 482 rows
);

ALTER TABLE order_archive REORGANIZE PARTITION y2021, y2022 INTO (
    PARTITION before_2023 VALUES LESS THAN (2023)
);
```

Split the American cities of `city_l`:

```sql
ALTER TABLE city_l REORGANIZE PARTITION americas INTO (
    PARTITION north_america VALUES IN ('CAN','MEX','USA'),     -- 25 rows
    PARTITION south_america VALUES IN ('ARG','BRA','VEN')      -- 15 rows
);
```

- Only range and list partitions can be reorganized, and the partitioning method cannot be changed.
- For range partitioning, only adjacent partitions can be merged.


### Hash partitioning reorganization

The partitions of a hash-partitioned table cannot be named individually; you can only change their number. No data is lost, but all rows are redistributed, because the hash function changes with the number of partitions.

```sql
ALTER TABLE table_name COALESCE PARTITION number_to_remove;
ALTER TABLE table_name ADD PARTITION PARTITIONS number_to_add;
```

```sql
ALTER TABLE customer_h COALESCE PARTITION 1;          -- 4 → 3 partitions
ALTER TABLE customer_h ADD PARTITION PARTITIONS 3;    -- 3 → 6 partitions, still 1,000 rows
```


### Partition promotion

**PROMOTE PARTITION** detaches range or list partitions and turns them into regular tables, named like the partition tables (`<table>__p__<partition>`). This is useful for historical data that is rarely used: the partitioned table becomes smaller, and the old data stays available. Promotion cannot be undone.

```sql
ALTER TABLE table_name PROMOTE PARTITION partition_name [, partition_name ...];
```

```sql
ALTER TABLE order_archive PROMOTE PARTITION before_2023;

SELECT COUNT(*) FROM order_archive__p__before_2023;   -- 898: now a regular table
SELECT COUNT(*) FROM order_archive;                    -- 2,012: 2023 to 2025 only
```

The promoted table keeps its data and ordinary indexes. It does not keep primary keys, foreign keys, unique indexes, **AUTO_INCREMENT** attributes and serials, triggers, methods, or inheritance relationships. Hash partitions cannot be promoted.


### Removing partitioning

**REMOVE PARTITIONING** turns a partitioned table back into a regular table. All rows are moved from the partitions into the table, which can take a long time.

```sql
ALTER TABLE order_archive REMOVE PARTITIONING;
```


## Indexes on partitioned tables

All indexes on a partitioned table are *local*: each partition has its own part of every index. Local indexes are smaller, which makes inserts faster, and transactions working on different partitions use different index structures. A scan of a partition never reads index entries of other partitions.

To keep unique indexes local, the partitioning column must be part of the primary key and of every unique index:

```sql
CREATE TABLE customer_h (id INT PRIMARY KEY, company_name VARCHAR(64))
PARTITION BY HASH (id) PARTITIONS 4;

CREATE UNIQUE INDEX u_customer_h_name ON customer_h (company_name);
```

```text
ERROR: Partition key attributes must be present in the index key.
```

`CREATE UNIQUE INDEX u_customer_h_name ON customer_h (company_name, id)` is accepted.


## Notes on partitioning

### Statistics on partitioned tables

CoraDB keeps statistics for each partition. The statistics of the partitioned table are the average of its partitions' statistics, which suits the common case of a query that is pruned to one partition. See [Updating statistics](https://www.coradb.ai/manual/sql/tuning.html).


### Restrictions on partitioned tables

- A table can have at most 1,024 partitions (`Invalid partition size.` for more).
- Partitions cannot take part in inheritance: no table can inherit from a partition, and a partition inherits only from its partitioned table.
- These optimizations are not used on partitioned tables: skipping **ORDER BY** ([Optimizing ORDER BY clause](https://www.coradb.ai/manual/sql/tuning_index_opt.html#optimizing-order-by-clause)), skipping **GROUP BY** ([Optimizing GROUP BY clause](https://www.coradb.ai/manual/sql/tuning_index_opt.html#optimizing-group-by-clause)), multiple key ranges ([Multiple key ranges optimization](https://www.coradb.ai/manual/sql/tuning_index_opt.html#multiple-key-ranges-optimization)), and index join.

### Partitioning key, character set and collation

The values in partition definitions must use the character set of the key column:

```sql
CREATE TABLE t (c CHAR(50) COLLATE utf8_bin)
PARTITION BY LIST (c) (
    PARTITION p0 VALUES IN (_utf8'x'),
    PARTITION p1 VALUES IN (_iso88591'y')
);
```

```text
ERROR: Invalid codeset '_iso88591' for partition value. Expecting '_utf8' codeset.
```

Key values are compared with the collation of the key column. In a case-insensitive collation `'test'` and `'TEST'` are the same value, so they cannot be in different partitions:

```sql
CREATE TABLE t (c STRING) COLLATE utf8_en_ci
PARTITION BY LIST (c) (
    PARTITION p0 VALUES IN ('test'),
    PARTITION p1 VALUES IN ('TEST')
);
```

```text
ERROR: Partition definition is duplicated. 'p1'
```
