示例,创建一张员工表按照employee_id进行列表分区:
CREATE TABLE employees (
employee_id int(6) primary key,
first_name varchar(20),
last_name varchar(25),
email varchar(25),
phone_number varchar(20),
hire_date datetime,
job_id varchar(10),
salary int(8),
commission_pct decimal(2,2),
manager_id decimal(6,0),
department_id int(4)
)
PARTITION BY LIST(employee_id) (
PARTITION p0 VALUES IN (101,103,105,107,109),
PARTITION p1 VALUES IN (102,104,106,108,110)
);
INSERT INTO employees VALUES ('100', 'Steven', 'King', 'SKING', '515.123.4567', '1987-06-17 00:00:00', 'AD_PRES', '24000.00', null, null, '90');
INSERT INTO employees VALUES ('101', 'Neena', 'Kochhar', 'NKOCHHAR', '515.123.4568', '1989-09-21 00:00:00', 'AD_VP', '17000.00', null, '100', '90');
INSERT INTO employees VALUES ('102', 'Lex', 'De Haan', 'LDEHAAN', '515.123.4569', '1993-01-13 00:00:00', 'AD_VP', '17000.00', null, '100', '90');
INSERT INTO employees VALUES ('103', 'Alexander', 'Hunold', 'AHUNOLD', '590.423.4567', '1990-01-03 00:00:00', 'IT_PROG', '9000.00', null, '102', '60');
INSERT INTO employees VALUES ('104', 'Bruce', 'Ernst', 'BERNST', '590.423.4568', '1991-05-21 00:00:00', 'IT_PROG', '6000.00', null, '103', '60');
INSERT INTO employees VALUES ('105', 'David', 'Austin', 'DAUSTIN', '590.423.4569', '1997-06-25 00:00:00', 'IT_PROG', '4800.00', null, '103', '60');
INSERT INTO employees VALUES ('106', 'Valli', 'Pataballa', 'VPATABAL', '590.423.4560', '1998-02-05 00:00:00', 'IT_PROG', '4800.00', null, '103', '60');
INSERT INTO employees VALUES ('107', 'Diana', 'Lorentz', 'DLORENTZ', '590.423.5567', '1999-02-07 00:00:00', 'IT_PROG', '4200.00', null, '103', '60');
INSERT INTO employees VALUES ('108', 'Nancy', 'Greenberg', 'NGREENBE', '515.124.4569', '1994-08-17 00:00:00', 'FI_MGR', '12000.00', null, '101', '100');
INSERT INTO employees VALUES ('109', 'Daniel', 'Faviet', 'DFAVIET', '515.124.4169', '1994-08-16 00:00:00', 'FI_ACCOUNT', '9000.00', null, '108', '230');
INSERT INTO employees VALUES ('110', 'John', 'Chen', 'JCHEN', '515.124.4 269', '1997-09-28 00:00:00', 'FI_ACCOUNT', '8200.00', null, '108', '100');
查询0号分区:
select * from employees partition(p0);
select * from employees partition(p1);
select * from employees partition(p0,p1);
和range分区一样,可以使用alter table … add/drop partition新增/删除分区:
ALTER TABLE employees ADD PARTITION(PARTITION p2 VALUES IN (111,112,113,114,115));
ALTER TABLE employees DROP PARTITION p2;
LIST COLUMNS分区表,分区字段支持使用CHAR VARCHAR DATE等数据类型。普通的LIST分区表分区字段必须是INT类型。
CREATE TABLE employees (
employee_id int(6),
first_name varchar(20),
last_name varchar(25),
email varchar(25),
phone_number varchar(20),
hire_date datetime,
job_id varchar(10),
salary int(8),
commission_pct decimal(2,2),
manager_id decimal(6,0),
department_id int(4)
)
PARTITION BY LIST COLUMNS(last_name) (
PARTITION p0 VALUES IN (‘King’,‘Grant’),
PARTITION p1 VALUES IN (‘Scott’,‘Jim’)
);