Help Center/
GaussDB/
Feature Guide(Distributed_3.x)/
Partitioned Table/
Introduction to Partitioned Tables/
Basic Usage of Partitions/
Creating Partitioned Tables
Updated on 2024-05-20 GMT+08:00
Creating Partitioned Tables
Creating Partitioned Tables
The SQL syntax tree is complex due to the powerful and flexible functions of the SQL language. So do partitioned tables. The creation of a partitioned table can be regarded as adding partition attributes to the original non-partitioned table. Therefore, the syntax interface of a partitioned table can be regarded to extend the CREATE TABLE statement of a non-partitioned table with a PARTITION BY clause and specify the following three core elements related to the partition:
- partType: describes the partitioning policy of a partitioned table. The options are RANGE, INTERVAL, LIST, and HASH.
- partKey: describes the partition key of a partitioned table. Currently, range and list partitioning supports a partition key with up to 16 columns, while hash partitioning supports a one-column partition key only.
- partExpr: describes the specific partitioning type of a partitioned table, that is, the mapping between key values and partitions.
The three elements are reflected in the PARTITION BY clause of the CREATE TABLE statement, for example, PARTITION BY partType (partKey) (partExpr[,partExpr]...). Example:
CREATE TABLE [ IF NOT EXISTS ] partition_table_name ( [ /* Inherited from the CREATE TABLE statement of an ordinary table */ { column_name data_type [ COLLATE collation ] [ column_constraint [ ... ] ] | table_constraint | LIKE source_table [ like_option [...] ] }[, ... ] ] ) [ WITH ( {storage_parameter = value} [, ... ] ) ] [ COMPRESS | NOCOMPRESS ] [ TABLESPACE tablespace_name ] /* Range partitioning */ PARTITION BY RANGE (partKey) [ INTERVAL ('interval_expr') [ STORE IN (tablespace_name [, ... ] ) ] ] ( partition_start_end_item [, ... ] partition_less_then_item [, ... ] ) /* List partitioning */ PARTITION BY LIST (partKey) ( PARTITION partition_name VALUES (list_values_clause) [ TABLESPACE tablespace_name [, ... ] ] ... ) /* Hash partitioning */ PARTITION BY HASH (partKey) ( PARTITION partition_name [ TABLESPACE tablespace_name [, ... ] ] ... ) /* Enable or disable row migration for a partitioned table. */ [ { ENABLE | DISABLE } ROW MOVEMENT ];
Restrictions
- Range and list partitioning supports a partition key with up to 16 columns. Hash partitioning supports a one-column partition key only.
- The partition key value cannot be null except for hash partitioning. Otherwise, the DML statement reports an error. The only exception is the MAXVALUE partition defined for a range partitioned table and the DEFAULT partition defined for a list partitioned table.
- The maximum number of partitions is 1048575, which can meet the requirements of most service scenarios. If the number of partitions increases, the number of files in the system increases, which affects the system performance. It is recommended that the number of partitions for a single table be less than or equal to 200.
Modifying Partition Attributes
You can run the ALTER TABLE command similar to that of a non-partitioned table to modify attributes related to partitioned tables and partitions. Common statements for modifying partition attributes are as follows:
- ADD PARTITION
- DROP PARTITION
- TRUNCATE PARTITION
- SPLIT PARTITION
- MERGE PARTITION
- MOVE PARTITION
- EXCHANGE PARTITION
- RENAME PARTITION
The preceding statements for modifying partition attributes are extended based on the ALTER TABLE statement of an ordinary table. Most of the statements are used in a similar way. The following is an example of the basic syntax framework for modifying partitioned table attributes:
/* Basic ALTER TABLE syntax */ ALTER TABLE [ IF EXISTS ] { table_name [*] | ONLY table_name | ONLY ( table_name )} action [, ... ];
For details about how to use the ALTER TABLE statement, see Partitioned Table O&M Management and section "SQL Reference > SQL Syntax > ALTER TABLE PARTITION" in Developer Guide.
Parent topic: Basic Usage of Partitions
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
The system is busy. Please try again later.
For any further questions, feel free to contact us through the chatbot.
Chatbot