本文的原文连接是: https://blog.csdn.net/freewebsys/article/details/84839478
未经博主允许不得转载。
博主地址是:http://blog.csdn.net/freewebsys
也就是说,对于原表分区后,对于应用层来说可以不做变化,我们无需改变原有的SQL语句,相当于MySQL帮我们实现了传统分表后的SQL中间件,当然,MySQL的分区表的实现要复杂很多。
另外,在创建分区时可以指定分区的索引文件和数据文件的存储位置,所以可以把数据表的数据分布在不同的物理设备上,从而高效地利用多个硬件设备。
一些限制:
1.在5.6.7之前的版本,一个表最多有1024个分区;从5.6.7开始,一个表最多可以有8192个分区。
2.分区表中无法使用外键约束。
3.主表的所有唯一索引列(包括主键)都必须包含分区字段。MySQL官方文档中写的是:
All columns used in the partitioning expression for a partitioned
table must be part of every unique key that the table may have.
https://dev.mysql.com/doc/refman/5.7/en/partitioning-limitations-partitioning-keys-unique-keys.html
https://dev.mysql.com/doc/refman/8.0/en/partitioning-management-exchange.html
别人的测试文章:
https://www.cnblogs.com/acpp/archive/2010/08/09/1795464.html
一般情况按季度进行分区,要小于季度最后一天。
所以小于下个月的第一天。
这个时间要是没有写对,会报错误:
value must be strictly increasing for each partition
CREATE TABLE `user_part` (
`id` BIGINT(20) NOT NULL AUTO_INCREMENT ,
`name` VARCHAR(200) COMMENT '名称',
`create_date` date NOT NULL COMMENT '日期',
PRIMARY KEY (`id`,`create_date`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
PARTITION BY RANGE(TO_DAYS(`create_date`))
(
PARTITION p2018q4 VALUES LESS THAN (TO_DAYS('2019-01-01')),
PARTITION p2019q1 VALUES LESS THAN (TO_DAYS('2019-04-01')),
PARTITION p2019q2 VALUES LESS THAN (TO_DAYS('2019-07-01')),
PARTITION p2019q3 VALUES LESS THAN (TO_DAYS('2019-10-01')),
PARTITION p2019q4 VALUES LESS THAN (TO_DAYS('2020-01-01')),
PARTITION p2020q1 VALUES LESS THAN (TO_DAYS('2020-04-01')),
PARTITION p2020q2 VALUES LESS THAN (TO_DAYS('2020-07-01')),
PARTITION p2020q3 VALUES LESS THAN (TO_DAYS('2020-10-01')),
PARTITION p2020q4 VALUES LESS THAN (TO_DAYS('2021-01-01')),
PARTITION p2021q1 VALUES LESS THAN (TO_DAYS('2021-04-01')),
PARTITION p2021q2 VALUES LESS THAN (TO_DAYS('2021-07-01')),
PARTITION p2021q3 VALUES LESS THAN (TO_DAYS('2021-10-01')),
PARTITION p2021q4 VALUES LESS THAN (TO_DAYS('2022-01-01')),
PARTITION p2022q1 VALUES LESS THAN (TO_DAYS('2022-04-01')),
PARTITION p2022q2 VALUES LESS THAN (TO_DAYS('2022-07-01')),
PARTITION p2022q3 VALUES LESS THAN (TO_DAYS('2022-10-01')),
PARTITION p2022q4 VALUES LESS THAN (TO_DAYS('2023-01-01')),
PARTITION p2023q1 VALUES LESS THAN (TO_DAYS('2023-04-01')),
PARTITION p2023q2 VALUES LESS THAN (TO_DAYS('2023-07-01')),
PARTITION p2023q3 VALUES LESS THAN (TO_DAYS('2023-10-01')),
PARTITION p2023q4 VALUES LESS THAN (TO_DAYS('2024-01-01'))
);
INSERT INTO user_part VALUES
(1, 'desk organiser', '2018-10-15'),
(2, 'CD player', '2019-01-05'),
(3, 'TV set', '2019-03-10'),
(4, 'bookcase', '2019-04-10'),
(5, 'exercise bike', '2019-05-09'),
(6, 'sofa', '2019-06-05'),
(7, 'popcorn maker', '2018-01-22'),
(8, 'aquarium', '2023-08-04'),
(9, 'study desk', '2022-09-16'),
(10, 'lava lamp', '2021-12-25');
explain partitions select * from user_part where create_date = '2022-09-16';
SELECT DISTINCT PARTITION_EXPRESSION
FROM INFORMATION_SCHEMA.PARTITIONS
WHERE TABLE_NAME='user_part';
SELECT PARTITION_NAME, TABLE_ROWS
FROM INFORMATION_SCHEMA.PARTITIONS
WHERE TABLE_NAME = 'user_part' ;
mysql 分区还是非常不错的数据方法。
当然以后可以使用tidb 存储数据,处理数据。
本文的原文连接是:
https://blog.csdn.net/freewebsys/article/details/84839478
博主地址是:http://blog.csdn.net/freewebsys