show variables like '%version%';
| version | 8.0.16 |
| version_comment | MySQL Community Server - GPL |
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);
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);
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);
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