Mysql千万级别表分区优化

通过上篇文章(https://blog.csdn.net/qq_31150503/article/details/105450236)相信大家已经能够运用MyISAM进行水平分表优化接下来使用Mysql分区进行数据存储优化,也是目前公司采用的大批量数据存储优化方案。

方案:根据日期进行分区存储

1、创建分区表

-- 创建数据分区 根据日期进行分区存储
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、初始化分区数据

--  初始化数据
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、查询、删除、添加表分区

--  查询当前表分区情况
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、数据验证:通过

                    

 

发布了25 篇原创文章 · 获赞 9 · 访问量 46万+

猜你喜欢

转载自blog.csdn.net/qq_31150503/article/details/105451273