Mysql million-level table partition optimization

Through the last article ( https://blog.csdn.net/qq_31150503/article/details/105450236 ), I believe that you have been able to use MyISAM to optimize the horizontal sub-table optimization, and then use Mysql partition to optimize the data storage. Optimized solution for batch data storage.

Scheme: partition storage according to date

1. Create a partition table

-- 创建数据分区 根据日期进行分区存储
create table dept(
	id varchar(50) not null COMMENT 'id',
	dept_name varchar(20) DEFAULT null COMMENT '部分名称',
	creator varchar(50) DEFAULT null COMMENT '创建者',
	create_date datetime not null COMMENT '创建时间',
	updater varchar(50) DEFAULT null COMMENT '更新者',
	update_date datetime DEFAULT null COMMENT '更新时间',
	KEY (create_date,id),
	PRIMARY KEY (id,create_date)
) PARTITION by range columns(create_date)(
	partition p20200411 VALUES less than ('2020-04-11'),
	partition p20200412 VALUES less than ('2020-04-12'),
	partition p20200413 VALUES less than ('2020-04-13')
);

 2. Initialize partition data

--  初始化数据
INSERT INTO dept ( `id`, `dept_name`, `create_date`, `update_date` )
VALUES
	( '1', '技术部', now( ), now( ) );
	
INSERT INTO dept ( `id`, `dept_name`, `create_date`, `update_date` )
VALUES
	( '4', '运营部', '2020-04-10 13:26:02', '2020-04-10 13:26:02' );
	
INSERT INTO dept ( `id`, `dept_name`, `create_date`, `update_date` )
VALUES
	( '2', '销售部', '2020-04-11 13:26:02', '2020-04-11 13:26:02' );
	
INSERT INTO dept ( `id`, `dept_name`, `create_date`, `update_date` )
VALUES
	( '3', '管理部', '2020-04-12 13:26:02', '2020-04-12 13:26:02' );

 3. Query, delete, and add table partitions

--  查询当前表分区情况
select * from information_schema.`PARTITIONS` WHERE table_name = 'dept';
--  添加新分区
alter table dept add partition (partition p20200414 values less than ('2020-04-14'));
--  删除分区数据
alter table dept drop partition p20200414;

 4. Data verification: pass

                    

 

Published 25 original articles · Like9 · Visit 460,000+

Guess you like

Origin blog.csdn.net/qq_31150503/article/details/105451273