mysql8-表分区-范围分区-RANGE Partitioning

mysql8-表分区-范围分区-RANGE Partitioning

mysql版本

show variables like '%version%';
| version                  | 8.0.16                       |
| version_comment          | MySQL Community Server - GPL |

1、创建基于列范围分区表

drop TABLE employees ;
CREATE TABLE employees (
    id INT NOT NULL,
    fname VARCHAR(30),
    lname VARCHAR(30),
    hired DATE NOT NULL DEFAULT '1970-01-01',
    separated DATE NOT NULL DEFAULT '9999-12-31',
    job_code INT NOT NULL,
    store_id INT NOT NULL
)
PARTITION BY RANGE (store_id) (
    PARTITION p0 VALUES LESS THAN (6),
    PARTITION p1 VALUES LESS THAN (11),
    PARTITION p2 VALUES LESS THAN (16),
    PARTITION p3 VALUES LESS THAN MAXVALUE
);

-- 查看分区信息
select * from information_schema.PARTITIONS
where table_name ='employees';

-- 写入测试数据
insert into employees(id,fname,lname,hired,separated,job_code,store_id) values(1,'f1','l1','2019-12-01','2020-01-01',1,1);
insert into employees(id,fname,lname,hired,separated,job_code,store_id) values(2,'f2','l2','2019-12-02','2020-01-01',1,6);
insert into employees(id,fname,lname,hired,separated,job_code,store_id) values(3,'f3','l3','2019-12-03','2020-01-01',1,11);
insert into employees(id,fname,lname,hired,separated,job_code,store_id) values(4,'f4','l4','2019-12-04','2020-01-01',1,16);

-- 查询
select * from employees;

-- 按分区查询
select * from employees partition(p0);
select * from employees partition(p1);
select * from employees partition(p2);
select * from employees partition(p3);

2、创建基于日期函数的范围分区

drop TABLE employees ;
CREATE TABLE employees (
    id INT NOT NULL,
    fname VARCHAR(30),
    lname VARCHAR(30),
    hired DATE NOT NULL DEFAULT '1970-01-01',
    separated DATE NOT NULL DEFAULT '9999-12-31',
    job_code INT,
    store_id INT
)
PARTITION BY RANGE ( YEAR(separated) ) (
    PARTITION p0 VALUES LESS THAN (2017),
    PARTITION p1 VALUES LESS THAN (2018),
    PARTITION p2 VALUES LESS THAN (2019),
    PARTITION p3 VALUES LESS THAN MAXVALUE
);
-- 写入测试数据
insert into employees(id,fname,lname,hired,separated,job_code,store_id) values(1,'f1','l1','2016-01-01','2016-02-01',1,1);
insert into employees(id,fname,lname,hired,separated,job_code,store_id) values(2,'f2','l2','2017-01-02','2017-02-01',1,6);
insert into employees(id,fname,lname,hired,separated,job_code,store_id) values(3,'f3','l3','2018-01-03','2018-02-01',1,11);
insert into employees(id,fname,lname,hired,separated,job_code,store_id) values(4,'f4','l4','2019-01-04','2019-02-01',1,16);

-- 查询
select * from employees;
select year(a.separated), a.* from employees a;

-- 按分区查询
select * from employees partition(p0);
select * from employees partition(p1);
select * from employees partition(p2);
select * from employees partition(p3);

3、创建基于TIMESTAMP列,使用UNIX_TIMESTAMP()函数的范围分区

CREATE TABLE quarterly_report_status (
    report_id INT NOT NULL,
    report_status VARCHAR(20) NOT NULL,
    report_updated TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
)
PARTITION BY RANGE ( UNIX_TIMESTAMP(report_updated) ) (
    PARTITION p0 VALUES LESS THAN ( UNIX_TIMESTAMP('2008-01-01 00:00:00') ),
    PARTITION p1 VALUES LESS THAN ( UNIX_TIMESTAMP('2008-04-01 00:00:00') ),
    PARTITION p2 VALUES LESS THAN ( UNIX_TIMESTAMP('2008-07-01 00:00:00') ),
    PARTITION p3 VALUES LESS THAN ( UNIX_TIMESTAMP('2008-10-01 00:00:00') ),
    PARTITION p4 VALUES LESS THAN ( UNIX_TIMESTAMP('2009-01-01 00:00:00') ),
    PARTITION pmax VALUES LESS THAN (MAXVALUE)
);

-- 写入测试数据
insert into quarterly_report_status(report_id,report_status,report_updated) values(1,'r1','2008-01-01');

-- 查询
select * from quarterly_report_status;
select UNIX_TIMESTAMP(a.report_updated), a.* from quarterly_report_status a;

-- 按分区查询
select * from quarterly_report_status partition(p0);
select * from quarterly_report_status partition(p1);

4、创建基于RANGE COLUMNS的范围分区


CREATE TABLE members (
    firstname VARCHAR(25) NOT NULL,
    lastname VARCHAR(25) NOT NULL,
    username VARCHAR(16) NOT NULL,
    email VARCHAR(35),
    joined DATE NOT NULL
)
PARTITION BY RANGE COLUMNS(joined) (
    PARTITION p0 VALUES LESS THAN ('1960-01-01'),
    PARTITION p1 VALUES LESS THAN ('1970-01-01'),
    PARTITION p2 VALUES LESS THAN ('1980-01-01'),
    PARTITION p3 VALUES LESS THAN ('1990-01-01'),
    PARTITION p4 VALUES LESS THAN MAXVALUE
);

-- 写入测试数据
insert into members(firstname,lastname,username,email,joined) values('f1','l1','un1','[email protected]','1961-01-01');

-- 查询
select * from members;

-- 按分区查询
select * from members partition(p0);
select * from members partition(p1);

参考

https://dev.mysql.com/doc/refman/8.0/en/partitioning-range.html

发布了230 篇原创文章 · 获赞 29 · 访问量 23万+

猜你喜欢

转载自blog.csdn.net/huryer/article/details/103759384
今日推荐