Mysql table partitioning

1. Mysql partition

Data is scattered in different physical files of a table. Partitioning does not change the table structure. What changes is the storage method. Data access does not require users to specify, and maintenance is easy. The application layer does not need to be changed. Partitions are assigned to different locations on the disk

2. The relationship between Mysql table partition and index, the performance decreases in turn

  • Primary key partition: The primary key partition means that the field is both the primary key and the partition field, and the performance is the best.
  • Partial primary key + partition index: Use part of the fields in the combined primary key as the partitioning field, and at the same time build the indexing of the partitioning field.
  • Partition index: There is no primary key, only the partition field and the partition field is indexed.
  • There is no index for partition + partition field: only the partition is built, but the partition field is not indexed.

3.Mysql RANGE partition type

Refer to the online documentation: http://tool.oschina.net/apidocs/apidoc?api=mysql-5.1-zh Tables partitioned according to RANGE are partitioned in one of the following ways, and each partition contains the values ​​of those partition expressions Rows that lie within a given contiguous interval. These intervals must be continuous and cannot overlap each other. Use the VALUES LESS THAN operator to define them. Case: Change the temperature and humidity record list to a table partition sql statement, and make a table based on the year (field in T_TEMP_RECORD_DETAIL: op_time). partition.

alter table T_TEMP_RECORD_DETAIL
partition by range(year(op_time))(
partition  p2019 values less than(2019),
partition  p2020 values less than(2020),
partition  p2021 values less than(2021),
partition  p2022 values less than(2022),
partition  p2023 values less than(2023),
partition  p2024 values less than(2024),
partition  p2025 values less than(2025),
partition  p2026 values less than(2026),
partition p_des VALUES LESS THAN MAXVALUE
 );

The field op_time must be of the correct time type and not null, otherwise there is no bucket in the partition for when the field is null.

4. Storage of RANGE partition data

  • After using the range partition, data with a year less than 2019 will be stored in the p2019 partition, and data with a year greater than 2019 and less than 2020 will be automatically stored in the p2020 partition.

5. Deletion of RANGE partition data

“Alter table T_TEMP_RECORD_DETAIL drop partition p2019 “

run sql to delete a partition to delete data

6. Query of RANGE partitioned data

Select * from T_TEMP_RECORD_DETAIL where op_time>=’2018-1-1 00:00:00’ and 
op_time<=’2019-12-31 23:59:59’

Here, the sql optimizer will only query the data in the p2019 partition, and the query efficiency will be improved.

7. Increase in RANGE partition

  • The largest partition must be removed first:
alter table T_TEMP_RECORD_DETAIL drop partition p_des;
  • Add partition:
alter table T_TEMP_RECORD_DETAIL add partition(partition p_2027 values less than(2027));

8. Deletion of RANGE partition

alter table T_TEMP_RECORD_DETAIL drop partition p2019;

After the partition is deleted, the data in the partition will also be deleted.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325363310&siteId=291194637