1. Overview
In order to improve the efficiency of the database in large amount of data reading and writing operations and queries, Dameng database provides the technology of partitioning tables and indexes. The data in database objects such as tables and indexes are divided into small units and stored in separate segments. Users' access to tables is transformed into access to smaller segments, so as to improve the performance of large-scale application systems.
Damon provides a horizontal partition method, which includes three methods: range, HASH and list.
2. Partition concept
Partitioning refers to the technology of dividing database objects such as tables and indexes into smaller manageable fragments. Each fragment is called partitioned sub tables or partitioned indexes.
After a table is partitioned, the query operation on the table can be limited to a partition rather than the whole table, which can greatly improve the query speed.
Damon uses the sub table method to create the partition table. The partition table is used as the partition main table, and each partition exists as a sub table entity, that is, each partition is a complete table, which is generally named as the main table name_ Partition name.
In the syntax of creating a table, use the partition clause to specify the partition method, partition column, partition name and other information to create a partitioned table
3. Partition method
(1) Range partition: partition the range of values on some columns in the table. According to the range of a value, decide which partition to store the data in.
(2) HASH partition: a partition type that evenly distributes data by specifying the partition number. These partitions are basically the same size by HASH partition on the I/O device.
(3) list partition: determine the data that should be stored together by specifying the discrete value set of a column in the table.
(4) Multi level partition table: any combination of the above three methods.
4. Create range partition table
Range partition is to create a partition according to the range of values of a column or several columns. When users write data to the table, the database server will judge according to the values on these columns and write the data to the corresponding partition. When creating a range partition, you must first specify the partition columns, that is, which columns to partition according to.
For example, create a table STUDENT and partition it:
CREATE TABLE STUDENT( N_ID INT, C_NAME CHAR(15), N_AGE INT, N_SCORE INT, C_ADDRESS VARCHAR(30) ) PARTITION BY RANGE (N_SCORE)( PARTITION FALED VALUES LESS THAN ('45'), PARTITION BAD VALUES LESS THAN ('60'), PARTITION GOOD VALUES LESS THAN ('80'), PARTITION EXCELLENT VALUES EQU OR LESS THAN (MAXVALUE) );
The partition definition of this partition table is based on N of STUDENT table_ Score column is used for partition. Those lower than 45 are divided into FALED partitions, those between 45 and 60 are divided into BAD partitions, and so on.
Insert data into the table as follows:
Specify the value of a partition in the query table, such as:
5. Create list partition table
Range partitioning is based on the data range on a column. If the data on a column cannot be partitioned by range partitioning, and the data on the column is relatively fixed values, list partitioning can be considered.
For example, create a SALES table as follows:
CREATE TABLE SALES( SALER_ID INT NOT NULL, SALER CHAR(20), CITY CHAR(10) ) PARTITION BY LIST(CITY)( PARTITION SOUTH VALUES ('Guangzhou','Shenzhen','Xiamen'), PARTITION NORTH VALUES ('Beijing','Tianjin','Dalian'), PARTITION EAST VALUES ('Nanjing','Shanghai'), PARTITION WEST VALUES ('Chengdu','Guiyang','Chongqing') );
The definition of the partition is: when the data in the inserted table is a partition key in the partition, the value will be compared with the definition of the partition. If the comparison is successful, it will be inserted into the partition.
Note: the partition key of the partition must be unique, otherwise the following error message will be reported
6. Create HASH partition table
In many cases, users cannot predict the range of data changes on a column, so it is impossible to create a fixed number of range partitions or LIST partitions.
In this regard, DM hash partitioning provides a method to divide data equally in a specified number of partitions, and map rows to partitions based on the hash value of partition key. When the user writes data to the table, the database server will calculate the data according to a hash function and distribute the data evenly in each partition.
For example, take the SALES table as an example. In the list partition, the table is limited to several fixed cities. However, in practice, cities are distributed throughout the country and are not fixed. At this time, hash partition can be used to partition the table.
CREATE TABLE SALES_FOR_HASH( SALER_ID INT NOT NULL, SALER CHAR(20), CITY CHAR(10) ) PARTITION BY HASH(CITY)( PARTITION P1, PARTITION P2, PARTITION P3, PARTITION P4 );
The values of the specified partition in the query table are as follows:
7. Create multi-level partition table
In many cases, the data cannot be accurately classified after one partition, so a multi-level partition table is required; DM supports up to eight layers and multi-level partitions
8. Maintenance level zoning table
(1) Add partition
ALTER TABLE STUDENT ADD PARTITION P5 VALUES ...
(2) Delete partition
ALTER TABLE STUDENT DROP PARTITION P5 ;
(3) Merge partitions
ALTER TABLE STUDENT MERGE PARTITIONS P1,P2 INTO PARTISION P1_2;
(4) Split partition
ALTER TABLE STUDENT SPLIT PARTITION P1_2 AT ('90') INTO (PARTITION P1,PARTITION P2);