代码语言:javascript复制
下面的表employees是根据store_id列值来进行分区的——用range来分区的
代码语言:javascript复制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 );
下面的代码是list而不是range来分区的,根据store_id的值来进行分区
代码语言:javascript复制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 LIST(store_id) (
PARTITION pNorth VALUES IN (3,5,6,9,17),
PARTITION pEast VALUES IN (1,2,10,11,19,20),
PARTITION pWest VALUES IN (4,12,13,14,18),
PARTITION pCentral VALUES IN (7,8,15,16)
);
下面的语句可以查看每一个分区里有多少行记录
代码语言:javascript复制mysql> SELECT PARTITION_NAME,TABLE_ROWS -> FROM INFORMATION_SCHEMA.PARTITIONS -> WHERE TABLE_NAME = 'employees';
---------------- ------------
| PARTITION_NAME | TABLE_ROWS |
p0 1
p1 4
p2 0
p3 0
代码语言:javascript复制下面这条语句返回的是p1分区中的所有记录:
代码语言:javascript复制SELECT * FROM employees PARTITION (p1);
(adsbygoogle = window.adsbygoogle || []).push({});