Hello everyone, I am a duck:
share with you today about mysql partition.
demand:
Time partition. Right and left tens of millions of data tables, partition, increase the amount of data of about ten million / year.
Code:
Before the simulation table already exists:
DROP TABLE IF EXISTS `test`;
CREATE TABLE `test` (
`id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '主键id',
`description` varchar(512) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL,
`state` tinyint(4) NULL DEFAULT 0 COMMENT '0:未处理,1:处理中,2:处理完成,3:异常订单',
`create_time` datetime(0) NULL DEFAULT NULL COMMENT '创建时间'
) ;
# Cancel increment
alter table test modify id int;
# Drop Primary
alter table test drop PRIMARY KEY;
# Add composite primary key
alter table test add PRIMARY KEY(id,create_time);
#id changed increment
alter table test modify id int AUTO_INCREMENT;
# Increase composite index, partition field must be unique, so unique index can not be created.
#ALTER TABLE test ADD UNIQUE (serial_no,delete_flag);
# Add partition, press 1 year
ALTER TABLE test PARTITION BY RANGE COLUMNS(create_time ) (
PARTITION p1 VALUES LESS THAN ( '20190101'),
PARTITION p2 VALUES LESS THAN ( '20200101'),
PARTITION p3 VALUES LESS THAN ( '20210101'),
PARTITION p4 VALUES LESS THAN ( '20220101'),
PARTITION p5 VALUES LESS THAN ( '20230101'),
PARTITION p6 VALUES LESS THAN ( '20240101'),
PARTITION p7 VALUES LESS THAN ( '20250101'),
PARTITION p8 VALUES LESS THAN ( '20260101'),
PARTITION p9 VALUES LESS THAN ( '20270101')
);
a brief introdction:
mysql partition type
RANGE Partitioning:
based on a column belonging to a given continuous value range, a plurality of lines allocated to the partition.
LIST partition:
similar press RANGE partition, except that LIST partition is a value based on column values match a set of discrete values to be selected.
HASH Partitioning:
based on the expression of the return value of a user-defined partition to be selected, and the value of this expression using the column to be inserted into the table of these lines are calculated. This function may be included in MySQL effective expression of any non-negative integer value.
KEY Subdivision:
HASH partition by similar, except that only support partition KEY calculating one or more columns, and MySQL server provides its own hash function. There must be one or more columns comprise integer values.
Composite partition:
re-divided based on the each partition RANGE / LIST type partition table. Child partition can be HASH / KEY other types.
Frequently used commands:
# When you create a partition table:
CREATE TABLE `test` (
`id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '主键id',
`description` varchar(512) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL,
`state` tinyint(4) NULL DEFAULT 0 COMMENT '0:未处理,1:处理中,2:处理完成,3:异常订单',
`create_time` datetime(0) NULL DEFAULT NULL COMMENT '创建时间',
PRIMARY KEY (`id`) USING BTREE,
INDEX `etc_cg_document_i4`(`state`) USING BTREE,
) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8 COLLATE = utf8_bin ROW_FORMAT = Compact PARTITION BY RANGE (`id`)
PARTITIONS 2
(PARTITION `p1` VALUES LESS THAN (10000) ENGINE = InnoDB MAX_ROWS = 0 MIN_ROWS = 0 ,
PARTITION `p2` VALUES LESS THAN (20000) ENGINE = InnoDB MAX_ROWS = 0 MIN_ROWS = 0 )
;
# :( increase partition to partition id)
alter table test partition by range(id)
(
partition p1 values less than (10000),
partition p2 values less than (20000)
);
# Delete the specified partition:
alter table test drop partition p1;
# Delete all partitions:
Alter table test remove partitioning;
# View partition information
SELECT
PARTITION_NAME,
TABLE_ROWS
FROM
INFORMATION_SCHEMA.PARTITIONS
WHERE
TABLE_NAME = 'test';
Under another said that if the deleted partition, specify the partition data will synchronize deletions, exercise caution.
If you want mysql rebuild partition table and keep the data of the case, refer to this:
https://blog.csdn.net/fdipzone/article/details/79769524
About a large amount of data to optimize mysql look at this:
https://blog.csdn.net/afsvsv/article/details/84998119
For more mysql partition information, see this article: