mysql已有表分区
一、表结构:
*****************************1. row **********************************
Table: history_uint
Create Table: CREATE TABLE history_uint
(
itemid
bigint(20) unsigned NOT NULL,
clock
int(11) NOT NULL DEFAULT ‘0’,
value
bigint(20) unsigned NOT NULL DEFAULT ‘0’,
ns
int(11) NOT NULL DEFAULT ‘0’,
KEY history_uint_1
(itemid
,clock
)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin
clock中存储的是时间戳。
二、构想
1、使用时间的RANGE分区,每个小时一个分区:(以失败告终)
alter table history_uint
partition by range( hour(from_unixtime(clock)) )(
partition p0 values less than (1),
partition p1 values less than (2),
…
partition p23 values less than (24)
);#失败。。。
#错误提示:
ERROR 1479 (HY000): Syntax error: RANGE PARTITIONING requires definition of VALUES LESS THAN for each partition
2、使用时间的HASH分区,每小时一个分区:(以失败告终)
alter table history_uint
partition by hash( hour(from_unixtime(clock)) )
partitions 24;
#提示错误:
ERROR 1564 (HY000): This partition function is not allowed
下面链接罗列了在分区中可以使用的函数:
https://dev.mysql.com/doc/refman/5.7/en/partitioning-limitations-functions.html
分析:根据官网的提示from_unixtime()函数不能在分区中使用。
3、使用日期的RANGE分区,自定义一段日期将大数据表分成几个表:(成功)
alter table history_uint
partition by range( clock )
(
partition p0 values less than (unix_timestamp(‘2019-4-15’)),
partition p1 values less than (unix_timestamp(‘2019-4-20’)),
partition p2 values less than (unix_timestamp(‘2019-4-30’)),
partition p3 values less than (MAXVALUE)
);
如果表很大的话,这个过程十分漫长:
tage: 1 of 2 ‘copy to tmp table’ 78% of stage done
####################
Query OK, 38753477 rows affected (1 hour 29 min 53.91 sec)
Records: 38753477 Duplicates: 0 Warnings: 0
最后,介绍两个函数unix_timestamp()和from_unixtime():
(1)unix_timestamp():可以交日期时间转换成时间戳,unix_timestamp(‘2019-7-15 14:5:0’)
(2)from_unixtime(时间戳),转换成日期时间。
PS:MariaDB10.0之前的版本不支持分区查询,我使用select * from history_uint partition (p0);提示我语法错误,但是分区是支持的(我的版本是5.5.60)。