[mysql table partition system] record a wrong addition of table partitions to the original partition table

First of all, let me declare that the mysql version I use here is version 5.7.29. Of course, the following questions are also based on this version. Because there is no research on whether other versions will have these problems, you can check the information in the official documents

need

Now there is an order table, partitions have been created before, but as time goes by, we need to add new partitions on the basis of the original table

The following is the sql that created the partition before

alter table `order`  partition by range (TO_DAYS(CREATE_TIME)) (
 
	partition p_202202 values less than (TO_DAYS('2022-03-01')),
	partition p_202203 values less than (TO_DAYS('2022-04-01')),
	partition p_202204 values less than (TO_DAYS('2022-05-01')),
	partition p_202205 values less than (TO_DAYS('2022-06-01'))
);

Let's look at the effect of data storage after creation

Look at the information of the table partition

SELECT
    PARTITION_NAME,
    PARTITION_METHOD,
    PARTITION_EXPRESSION,
    PARTITION_DESCRIPTION,
    FROM_DAYS(PARTITION_DESCRIPTION),
    TABLE_ROWS,
    SUBPARTITION_NAME,
    SUBPARTITION_METHOD,
    SUBPARTITION_EXPRESSION 
FROM
    information_schema.PARTITIONS 
WHERE
    TABLE_SCHEMA = SCHEMA () 
    AND TABLE_NAME = 'order';

 

 Query to see how many pieces of data there are in the previously created partition and see that the execution result is 2894

select  count(*)  from `order`  PARTITION (p_202202,p_202203, p_202204,p_202205);

Please pay attention, the wrong way is coming

Wrong execution of sql

The business is growing. At this time, someone uses the following statement to increase the partition

alter table `order`  partition by range (TO_DAYS(CREATE_TIME)) (
 
	partition p_202302 values less than (TO_DAYS('2023-03-01')),
	partition p_202303 values less than (TO_DAYS('2023-04-01')),
	partition p_202304 values less than (TO_DAYS('2023-05-01')),
	partition p_202305 values less than (TO_DAYS('2023-06-01')),
	partition p_202306 values less than (TO_DAYS('2023-07-01'))
);

Let's look at the effect of storing data

 Look at the information of the table partition

SELECT
    PARTITION_NAME,
    PARTITION_METHOD,
    PARTITION_EXPRESSION,
    PARTITION_DESCRIPTION,
    FROM_DAYS(PARTITION_DESCRIPTION),
    TABLE_ROWS,
    SUBPARTITION_NAME,
    SUBPARTITION_METHOD,
    SUBPARTITION_EXPRESSION 
FROM
    information_schema.PARTITIONS 
WHERE
    TABLE_SCHEMA = SCHEMA () 
    AND TABLE_NAME = 'order';

 Playing with the basket, the original partition is gone, and the new partition will kill the previous one. See the effect and directly merge the old partition we created before into the p_202302 partition

How to save it?

here are two ways

  • Clear the partition creation and recreate the partition directly
alter table  `order` remove partitioning;
  • Use our split partition method to operate, we split p_202302 into multiple sub-partitions to complete
ALTER TABLE `order` REORGANIZE PARTITION p_202302 INTO (
    PARTITION p_202202 VALUES LESS THAN (TO_DAYS('2022-03-01')),
    PARTITION p_202203 VALUES LESS THAN (TO_DAYS('2022-04-01')),
	PARTITION p_202204 VALUES LESS THAN (TO_DAYS('2022-05-01')),
    PARTITION p_202205 VALUES LESS THAN (TO_DAYS('2022-06-01')),
	PARTITION p_202206_202302 VALUES LESS THAN (TO_DAYS('2023-03-01'))
		
);

Appending partitions correctly

Use the add partition syntax to append a partition

alter table `order`  add  partition  (
	partition p_202307 values less than (TO_DAYS('2023-08-01')),
	partition p_202308 values less than (TO_DAYS('2023-09-01'))
);

Of course, if you have used less than MAXVALUE when creating partitions before, we can use the method of splitting partitions for subsequent growth of data.

-- otherPartition 这个就是使用less than MAXVALUE 的分区名称

ALTER TABLE test_order REORGANIZE PARTITION otherPartition INTO (
    PARTITION p_202307 VALUES LESS THAN (TO_DAYS('2023-08-01')),
    PARTITION p_202308 VALUES LESS THAN (TO_DAYS('2023-09-01'))
);

 

Guess you like

Origin blog.csdn.net/run_boy_2022/article/details/131745008