前言:当数据库存储数据达到一定量级时,传统的数据读取会扫描许多不需要的数据,而真正所用的数据却可能只有几行。因此,对于几百g以上的数据库,有必要采取数据库分区,以分区的形式来划分数据库存储,这种方式对用户的操作来说是透明的,只需要将读取数据时加上分区形式的字段就可以达到分区的效果,提升读写速度,降低对数据库的资源使用。但是对于数据库使用人员来说,必须要加上相关的分区字段,而且不能跨区,否则就不会有分区的效果。
分区的方式也有很多种,今天介绍我们采用较多的一种分区方式,按date进行划分。分区时可以以月划分,以周划分,以天划分,都可以。主要取决于数据的集中情况。
相关的表分区语句介绍如下:
1.先创建一个新的表,并且分区。再将数据从原表导入。
数据库表分区相关语句功能
查看表分区数据,查看全量数据量
USE information_schema;
SELECT PARTITION_NAME,TABLE_ROWS
FROM INFORMATION_SCHEMA.PARTITIONS
WHERE TABLE_NAME = 'talbles_name';
SELECT table_name,table_rows FROM TABLES
WHERE TABLE_SCHEMA = 'db_name'
ORDER BY table_rows DESC
;
#判断当前MySQL是否支持分区?
show variables like '%partition%';
#查看表分区情况
select partition_name part,partition_expression expr,partition_description descr,table_rows from information_schema.partitions where table_schema = schema() and table_name='test';
#创建新表,并且表分区,以月为单位
CREATE TABLE `apdailysts_p` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`ap_id` INT(11) NOT NULL,
`mac` VARCHAR(17) NOT NULL,
`liveinfo` LONGTEXT NOT NULL,
`livetime` INT(11) NOT NULL,
`stsdate` DATE NOT NULL,
`lastmodified` DATETIME NOT NULL,
PRIMARY KEY (`id`, `stsdate`)
)
PARTITION BY RANGE COLUMNS(stsdate) (
PARTITION p0 VALUES LESS THAN ('2016-06-01'),
PARTITION p1 VALUES LESS THAN ('2016-07-01'),
PARTITION p2 VALUES LESS THAN ('2016-08-01'),
PARTITION p3 VALUES LESS THAN ('2016-09-01'),
PARTITION p15 VALUES LESS THAN MAXVALUE
)
2.直接修改表结构,达到分区的效果,这种会创建临时表,比较耗费资源(会覆盖以前的分区),第一次分区的话,需要将字段修改为date 类型,并添加至主键
ALTER TABLE question_P PARTITION BY RANGE (Month(send_time))
(
PARTITION p_Apr VALUES LESS THAN (TO_DAYS('2017-10-01')),
PARTITION p_May VALUES LESS THAN (TO_DAYS('2017-11-01')),
PARTITION p_Dec VALUES LESS THAN MAXVALUE )
3.修改datetime字段的表分区
3.1创建新表并分区
CREATE TABLE `t_ask_unit_re` (
`id` varchar(32) NOT NULL COMMENT '主键',
`create_time` datetime NOT NULL DEFAULT '0000-00-00 00:00:00' COMMENT '创建时间',
`update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
PRIMARY KEY (`id`,`create_time`)
) partition by range(to_days(create_time)) (
partition p201708 values less than (to_days('2017-09-01')),
partition p201709 values less than (to_days('2017-10-01')),
partition p201710 values less than (to_days('2017-11-01')),
partition p201711 values less than (to_days('2017-12-01')),
partition p201712 values less than (to_days('2018-01-01')),
PARTITION p202XYZ VALUES LESS THAN (MAXVALUE));
3.2alter 方式直接修改表分区
alter table t_ask_123 partition by range(to_days(create_time)) (
partition p201708 values less than (to_days('2017-09-01')),
partition p201709 values less than (to_days('2017-10-01')),
partition p201710 values less than (to_days('2017-11-01')),
partition p201711 values less than (to_days('2017-12-01')),
partition p201712 values less than (to_days('2018-01-01')),
partition p201801 values less than (to_days('2018-02-01')),
partition p201802 values less than (to_days('2018-03-01')),
partition p201803 values less than (to_days('2018-04-01')),
partition p201804 values less than (to_days('2018-05-01')),
partition p201805 values less than (to_days('2018-06-01')),
partition p201806 values less than (to_days('2018-07-01')),
partition p201807 values less than (to_days('2018-08-01')),
partition p201808 values less than (to_days('2018-09-01')),
partition p201809 values less than (to_days('2018-10-01')),
partition p201810 values less than (to_days('2018-11-01')),
partition p201811 values less than (to_days('2018-12-01')),
partition p201812 values less than (to_days('2019-01-01')),
partition p201901 values less than (to_days('2019-02-01')),
partition p201902 values less than (to_days('2019-03-01')),
partition p201903 values less than (to_days('2019-04-01')),
partition p201904 values less than (to_days('2019-05-01')),
partition p201905 values less than (to_days('2019-06-01')),
partition p201906 values less than (to_days('2019-07-01')),
partition p201907 values less than (to_days('2019-08-01')),
partition p201908 values less than (to_days('2019-09-01')),
partition p201909 values less than (to_days('2019-10-01')),
partition p201910 values less than (to_days('2019-11-01')),
partition p201911 values less than (to_days('2019-12-01')),
partition p201912 values less than (to_days('2020-01-01')),
partition p202001 values less than (to_days('2020-02-01')),
partition p202002 values less than (to_days('2020-03-01')),
partition p202003 values less than (to_days('2020-04-01')),
partition p202004 values less than (to_days('2020-05-01')),
partition p202005 values less than (to_days('2020-06-01')),
partition p202006 values less than (to_days('2020-07-01')),
partition p202007 values less than (to_days('2020-08-01')),
partition p202008 values less than (to_days('2020-09-01')),
partition p202009 values less than (to_days('2020-10-01')),
partition p202010 values less than (to_days('2020-11-01')),
partition p202011 values less than (to_days('2020-12-01')),
partition p202012 values less than (to_days('2021-01-01')),
PARTITION p202XYZ VALUES LESS THAN (MAXVALUE));