InnoDB
NDB
InnoDB
MyISAM
InnoDB
InnoDB
InnoDB
InnoDB
PARTITIONS
InnoDB
RANGE
LINEAR
NULL
intval
-MAXVALUE <=intval
<= MAXVALUE
MAXVALUE
LINEAR
RANGE COLUMNS
LINEAR
InnoDB
MERGE
FEDERATED
[STORAGE]
ENGINE
CREATE
TABLE
default_storage_engine
CREATE TABLE ti (id INT, amount DECIMAL(7,2), tr_date DATE) ENGINE=INNODB PARTITION BY HASH( MONTH(tr_date) ) PARTITIONS 6;
PARTITION
default_storage_engine
DATA DIRECTORY
PARTITION
CREATE TABLE
DATA DIRECTORY
CREATE TABLE tnp ( id INT NOT NULL AUTO_INCREMENT, ref BIGINT NOT NULL, name VARCHAR(255), PRIMARY KEY pk (id), UNIQUE KEY uk (name) );
pk
name
uk
MAX_ROWS
WHERE
SELECT * FROM t
PARTITION (p0,p1) WHERE c < 5
p1
t
DELETE
INSERT
REPLACE
UPDATE
LOAD DATA
LOAD XML
RANGE COLUMNS
RANGE
LIST COLUMNS
LINEAR HASH
HASH
DATE
TIME
DATETIME
KEY
DATE
TIME
DATETIME
创建表的成员(firstName varchar(25)不为空,lastName varchar(25)不为空,用户名varchar(16)不为空,varchar(35),加入邮件日期不为空)分区键(加入)分区6;
DATE
DATETIME
LIST
COLUMNS
NULL
LIST
LINEAR HASH
DATE
TIME
DATETIME
创建表的成员(firstName varchar(25)不为空,lastName varchar(25)不空,不空用户名varchar(16),电子邮件varchar(35),加入日期不为空)按范围分区(年(加入)(分区)P0值小于(1960年),分区P1值小于(1970年),分区P2的值小于(1980年),分区P3值小于(1990年),分区P4值小于最大值);
TO_DAYS()
YEAR()
TO_SECONDS()
WEEKDAY()
DAYOFYEAR()
MONTH()
0
1
3
LIST
0
CREATE TABLE
MySQL的> CREATE TABLE t2 (val INT)
-> PARTITION BY LIST(val)(
-> PARTITION mypart VALUES IN (1,3,5),
-> PARTITION MyPart VALUES IN (2,4,6)
-> );
错误1488(hy000):复制分区的名字10.
mypart
0.8E+01
VALUES LESS THAN
创建员工信息表(ID为不空,名为varchar(30),lname VARCHAR(30),聘用日期不为空的默认“1970-01-01,分离的日期无效的默认9999-12-31 ',job_code int不空,不空store_id int);
employees
store_id
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 (21) );
p0
PARTITION BY RANGE
(72, 'Mitchell', 'Wilson', '1998-06-25', NULL,
13)
store_id
VALUES LESS
THAN
CREATE
TABLE
创建员工信息表(ID为不空,名为varchar(30),lname VARCHAR(30),聘用日期不为空的默认“1970-01-01,分离的日期无效的默认9999-12-31 ',job_code int不空,不空store_id int)按范围分区(分区store_id)(P0值小于(6),分区P1值小于(11),分区P2的值小于(16), p3分区值小于最大 );
InnoDB
IGNORE
INSERT
IGNORE
MAXVALUE
store_id
ALTER
TABLE
job_code
创建员工信息表(ID为不空,名为varchar(30),lname VARCHAR(30),聘用日期不为空的默认“1970-01-01,分离的日期无效的默认9999-12-31 ',job_code int不空,不空store_id int)按范围分区(分区job_code)(P0值小于(100),分区P1值小于(1000),分区P2的值小于(10000));
p0
p2
VALUES LESS
THAN
<
DATE
YEAR(separated)
CREATE TABLE
创建员工信息表(ID为不空,名为varchar(30),lname VARCHAR(30),聘用日期不为空的默认“1970-01-01,分离的日期无效的默认9999-12-31 ',job_code int,int store_id)按范围分区(年(分离)(分区)P0值小于(1991),分区P1值小于(1996),分区P2的值小于(2001),分区P3值小于最大值);
p0
p2
RANGE
TIMESTAMP
UNIX_TIMESTAMP()
创建表quarterly_report_status(report_id int不空,report_status varchar(20)不空,不空report_updated时间戳默认current_timestamp更新current_timestamp)按范围分区(unix_timestamp(report_updated))(分区P0值小于(unix_timestamp('2008-01-01 00:00:00”)),分区P1值小于(unix_timestamp('2008-04-01 00:00:00”)),分区P2的值小于(unix_timestamp('2008-07-01 00:00:00”)),分区P3值小于(unix_timestamp('2008-10-01 00:00:00”)),分区P4值小于(unix_timestamp('2009-01-01 00:00:00”)),分区P5值小于(unix_timestamp('2009-04-01 00:00:00”)),分区P6值小于(unix_timestamp('2009-07-01 00:00:00”)),分区P7值小于(unix_timestamp('2009-10-01 00:00:00”)),分区P8值小于(unix_timestamp('2010-01-01 00:00:00”)),分区P9值小于(次));
TIMESTAMP
employees
DELETE
DELETE FROM employees WHERE YEAR(separated) <= 1990;
EXPLAIN SELECT
COUNT(*) FROM employees WHERE separated BETWEEN '2000-01-01'
AND '2000-12-31' GROUP BY store_id;
WHERE
RANGE
COLUMNS
RANGE
DATE
TIME
DATETIME
创建表的成员(firstName varchar(25)不为空,lastName varchar(25)不空,不空用户名varchar(16),电子邮件varchar(35),加入日期不为空)按范围分区(年(加入)(分区)P0值小于(1960年),分区P1值小于(1970年),分区P2的值小于(1980年),分区P3值小于(1990年),分区P4值小于最大值);
RANGE
TIMESTAMP
UNIX_TIMESTAMP()
创建表quarterly_report_status(report_id int不空,report_status varchar(20)不空,不空report_updated时间戳默认current_timestamp更新current_timestamp)按范围分区(unix_timestamp(report_updated))(分区P0值小于(unix_timestamp('2008-01-01 00:00:00”)),分区P1值小于(unix_timestamp('2008-04-01 00:00:00”)),分区P2的值小于(unix_timestamp('2008-07-01 00:00:00”)),分区P3值小于(unix_timestamp('2008-10-01 00:00:00”)),分区P4值小于(unix_timestamp('2009-01-01 00:00:00”)),分区P5值小于(unix_timestamp('2009-04-01 00:00:00”)),分区P6值小于(unix_timestamp('2009-07-01 00:00:00”)),分区P7值小于(unix_timestamp('2009-10-01 00:00:00”)),分区P8值小于(unix_timestamp('2010-01-01 00:00:00”)),分区P9值小于(次));
TIMESTAMP
UNIX_TIMESTAMP(timestamp_column)
RANGE COLUMNS
DATE
DATETIME
joined
创建表的成员(firstName varchar(25)不为空,lastName varchar(25)不为空,用户名varchar(16)不为空,varchar(35),加入邮件日期不为空)的范围列分区(加入)(分区P0值小于('1960-01-01 '),分区P1值小于('1970-01-01 '),分区P2的值小于('1980-01-01 '),分区P3值小于('1990-01-01 '),分区P4值小于最大值);
RANGE
expr
expr
value_list
value_list
NULL
LIST
LIST COLUMN
CREATE TABLE
创建员工信息表(ID为不空,名为varchar(30),lname VARCHAR(30),聘用日期不为空的默认“1970-01-01,分离的日期无效的默认9999-12-31 ',job_code store_id int,int);
default_storage_engine
CREATE TABLE
创建员工信息表(ID为不空,名varchar(30)、lname VARCHAR(30),雇用日期不为空的默认“1970-01-01,分离的日期无效的默认9999-12-31 ',job_code store_id int,int)列表分区(store_id)(分区pnorth值(3,5,6,9,17),分区peast值(1,2,10,11,19,20),分区pwest值(4,12,13,14,18),分区pcentral值(7,8,15,16));
ALTER TABLE employees TRUNCATE PARTITION
pWest
DELETE
ALTER TABLE
employees DROP PARTITION pWest
ALTER TABLE ... ADD
PARTITION
RANGE
RANGE
MAXVALUE
INSERT
MySQL的> CREATE TABLE h2 (
-> c1 INT,
-> c2 INT
-> )
-> PARTITION BY LIST(c1) (
-> PARTITION p0 VALUES IN (1, 4, 7),
-> PARTITION p1 VALUES IN (2, 5, 8)
-> );
查询行,0行受影响(0.11秒)MySQL > INSERT INTO h2 VALUES (3, 5);
错误1525(hy000):表没有分区的价值
INSERT
InnoDB
IGNORE
mysql>TRUNCATE h2;
Query OK, 1 row affected (0.00 sec) mysql>SELECT * FROM h2;
Empty set (0.00 sec) mysql>INSERT IGNORE INTO h2 VALUES (2, 5), (6, 10), (7, 5), (3, 1), (1, 9);
Query OK, 3 rows affected (0.00 sec) Records: 5 Duplicates: 2 Warnings: 0 mysql>SELECT * FROM h2;
+------+------+ | c1 | c2 | +------+------+ | 7 | 5 | | 1 | 9 | | 2 | 5 | +------+------+ 3 rows in set (0.00 sec)
LIST
COLUMNS
COLUMNS
LIST
RANGE COLUMNS
RANGE COLUMNS
RANGE COLUMNS
RANGE COLUMNS
RANGE COLUMNS
RANGE COLUMNS
RANGE
COLUMNS
RANGE COLUMNS
DATE
DATETIME
RANGE COLUMNS
创建表 table_name
通过范围分区(列 column_list
(partition) partition_name
不太多的价值 value_list
)[,分区 partition_name
不太多的价值 value_list
)] […]) column_list
: column_name
【, column_name
] […] value_list
: value
【, value
] […]
CREATE TABLE
column_list
value_list
value_list
value_list
column_list
N
VALUES
LESS THAN
N
CREATE TABLE
MAXVALUE
CREATE TABLE
MySQL的> CREATE TABLE rcx (
-> a INT,
-> b INT,
-> c CHAR(3),
-> d INT
-> )
-> PARTITION BY RANGE COLUMNS(a,d,c) (
-> PARTITION p0 VALUES LESS THAN (5,10,'ggg'),
-> PARTITION p1 VALUES LESS THAN (10,20,'mmm'),
-> PARTITION p2 VALUES LESS THAN (15,30,'sss'),
-> PARTITION p3 VALUES LESS THAN (MAXVALUE,MAXVALUE,MAXVALUE)
-> );
查询行,0行受影响(0.15秒)
rcx
b
d
a
c
INT
a
c
COLUMNS
VALUES LESS THAN
RANGE
VALUES LESS THAN
VALUES LESS THAN
RANGE
创建表格R1(一个int,int b)按范围分区(一)(分区P0值小于(5),分区P1值小于(次));
a
p1
INFORMATION_SCHEMA.PARTITIONS
MySQL的> INSERT INTO r1 VALUES (5,10), (5,11), (5,12);
查询行,3行受影响(0秒)记录:3份:0警告:0mysql > SELECT PARTITION_NAME,TABLE_ROWS
-> FROM INFORMATION_SCHEMA.PARTITIONS
-> WHERE TABLE_NAME = 'r1';
---------------- ------------ | partition_name | table_rows | ---------------- ------------ | P0 | 0 | | P1 |三| ---------------- ------------ 2行集(0.001秒)
rc1
a
COLUMNS
创建表的RC1(一个int,int b)的范围(A,B)列分区(分区P0值小于(5, 12),分区P3值小于(最大值,最大值));
rc1
mysql>INSERT INTO rc1 VALUES (5,10), (5,11), (5,12);
Query OK, 3 rows affected (0.00 sec) Records: 3 Duplicates: 0 Warnings: 0 mysql>SELECT PARTITION_NAME,TABLE_ROWS
->FROM INFORMATION_SCHEMA.PARTITIONS
->WHERE TABLE_NAME = 'rc1';
+--------------+----------------+------------+ | TABLE_SCHEMA | PARTITION_NAME | TABLE_ROWS | +--------------+----------------+------------+ | p | p0 | 2 | | p | p1 | 1 | +--------------+----------------+------------+ 2 rows in set (0.00 sec)
VALUES THAN LESS
THAN
rc1
MySQL的> SELECT (5,10) < (5,12), (5,11) < (5,12), (5,12) < (5,12);
+-----------------+-----------------+-----------------+| (5,10) < (5,12) | (5,11) < (5,12) | (5,12) < (5,12) |+-----------------+-----------------+-----------------+| 1 | 1 | 0 |+-----------------+-----------------+-----------------+1 row in set (0.00 sec)
(5,10)
(5,12)
(5,12)
p1
SELECT
SELECT ROW(5,10) < ROW(5,12), ROW(5,11) < ROW(5,12), ROW(5,12) < ROW(5,12);
RANGE COLUMNS
CREATE TABLE
CREATE TABLE rx ( a INT, b INT ) PARTITION BY RANGE COLUMNS (a) ( PARTITION p0 VALUES LESS THAN (5), PARTITION p1 VALUES LESS THAN (MAXVALUE) );
(5,10)
(5,12)
mysql>INSERT INTO rx VALUES (5,10), (5,11), (5,12);
Query OK, 3 rows affected (0.00 sec) Records: 3 Duplicates: 0 Warnings: 0 mysql>SELECT PARTITION_NAME,TABLE_ROWS
->FROM INFORMATION_SCHEMA.PARTITIONS
->WHERE TABLE_NAME = 'rx';
+--------------+----------------+------------+ | TABLE_SCHEMA | PARTITION_NAME | TABLE_ROWS | +--------------+----------------+------------+ | p | p0 | 0 | | p | p1 | 3 | +--------------+----------------+------------+ 2 rows in set (0.00 sec)
RANGE COLUMNS
CREATE
TABLE
创建表的RC2(一个int,int b)的范围(A,B)列分区(分区P0值小于(0),分区P1值小于(10,20),分区P2的值小于(10,30),分区P3值小于(最大值,最大值);创建表RC3(一)int,int)B的范围列分区(A,B)(分区P0值小于(0),分区P1值小于(10,20),分区P2的值小于(10,30),分区P3值小于(10,35),分区P4值小于(40),分区P5值小于比(MaxValue最大));
b
p1
p1
CREATE TABLE rc4 ( a INT, b INT, c INT ) PARTITION BY RANGE COLUMNS(a,b,c) ( PARTITION p0 VALUES LESS THAN (0,25,50), PARTITION p1 VALUES LESS THAN (10,20,100), PARTITION p2 VALUES LESS THAN (10,30,50) PARTITION p3 VALUES LESS THAN (MAXVALUE,MAXVALUE,MAXVALUE) );
RANGE
COLUMNS
mysql> SELECT (0,25,50) < (10,20,100), (10,20,100) < (10,30,50);
+-------------------------+--------------------------+
| (0,25,50) < (10,20,100) | (10,20,100) < (10,30,50) |
+-------------------------+--------------------------+
| 1 | 1 |
+-------------------------+--------------------------+
1 row in set (0.00 sec)
CREATE TABLE
MySQL的> CREATE TABLE rcf (
-> a INT,
-> b INT,
-> c INT
-> )
-> PARTITION BY RANGE COLUMNS(a,b,c) (
-> PARTITION p0 VALUES LESS THAN (0,25,50),
-> PARTITION p1 VALUES LESS THAN (20,20,100),
-> PARTITION p2 VALUES LESS THAN (10,30,50),
-> PARTITION p3 VALUES LESS THAN (MAXVALUE,MAXVALUE,MAXVALUE)
-> );
错误1493(hy000):值小于值必须为每个分区严格递增
p2
mysql> SELECT (0,25,50) < (20,20,100), (20,20,100) < (10,30,50);
+-------------------------+--------------------------+
| (0,25,50) < (20,20,100) | (20,20,100) < (10,30,50) |
+-------------------------+--------------------------+
| 1 | 0 |
+-------------------------+--------------------------+
1 row in set (0.00 sec)
MAXVALUE
RANGE
COLUMNS
PARTITION ...
VALUES LESS THAN
RANGE
COLUMNS
employees
创建员工信息表(ID为不空,名为varchar(30),lname VARCHAR(30),聘用日期不为空的默认“1970-01-01,分离的日期无效的默认9999-12-31 ',job_code int不空,不空store_id int);
RANGE COLUMNS
创建表employees_by_lname(ID为不空,名varchar(30)、lname VARCHAR(30),雇用日期不为空的默认“1970-01-01,分离的日期无效的默认9999-12-31 ',job_code int不空,不空store_id int)的范围列分区(模型)(分区P0值小于(G’),分区P1值小于(是的),分区P2的值小于(不),分区P3值小于(次));
employees
ALTER
TABLE
通过改变范围列雇员表分区(模型)(分区P0值小于(G’),分区P1值小于(是的),分区P2的值小于(不),分区P3值小于(次));
RANGE COLUMNS
'Andersen'
employees
ALTER
TABLE
通过范围列表(聘用)员工分区(分区P0值小于('1970-01-01 '),分区P1值小于('1980-01-01 '),分区P2的值小于('1990-01-01 '),分区P3值小于('2000-01-01 '),分区P4值小于('2010-01-01 '),分区P5值小于(次));
PARTITION BY RANGE COLUMNS
LIST
COLUMNS
DATE
DATETIME
LIST COLUMNS
CREATE TABLE customers_1 ( first_name VARCHAR(25), last_name VARCHAR(25), street_1 VARCHAR(30), street_2 VARCHAR(30), city VARCHAR(15), renewal DATE)PARTITION BY LIST COLUMNS(city) ( PARTITION pRegion_1 VALUES IN('Oskarshamn', 'H?gsby', 'M?nster?s'), PARTITION pRegion_2 VALUES IN('Vimmerby', 'Hultsfred', 'V?stervik'), PARTITION pRegion_3 VALUES IN('N?ssj?', 'Eksj?', 'Vetlanda'), PARTITION pRegion_4 VALUES IN('Uppvidinge', 'Alvesta', 'V?xjo'));
RANGE COLUMNS
COLUMNS()
DATE
DATETIME
LIST COLUMNS
CREATE TABLE customers_2 ( first_name VARCHAR(25), last_name VARCHAR(25), street_1 VARCHAR(30), street_2 VARCHAR(30), city VARCHAR(15), renewal DATE ) PARTITION BY LIST COLUMNS(renewal) ( PARTITION pWeek_1 VALUES IN('2010-02-01', '2010-02-02', '2010-02-03', '2010-02-04', '2010-02-05', '2010-02-06', '2010-02-07'), PARTITION pWeek_2 VALUES IN('2010-02-08', '2010-02-09', '2010-02-10', '2010-02-11', '2010-02-12', '2010-02-13', '2010-02-14'), PARTITION pWeek_3 VALUES IN('2010-02-15', '2010-02-16', '2010-02-17', '2010-02-18', '2010-02-19', '2010-02-20', '2010-02-21'), PARTITION pWeek_4 VALUES IN('2010-02-22', '2010-02-23', '2010-02-24', '2010-02-25', '2010-02-26', '2010-02-27', '2010-02-28') );
RANGE
DATE
CREATE TABLE customers_3 ( first_name VARCHAR(25), last_name VARCHAR(25), street_1 VARCHAR(30), street_2 VARCHAR(30), city VARCHAR(15), renewal DATE ) PARTITION BY RANGE COLUMNS(renewal) ( PARTITION pWeek_1 VALUES LESS THAN('2010-02-09'), PARTITION pWeek_2 VALUES LESS THAN('2010-02-15'), PARTITION pWeek_3 VALUES LESS THAN('2010-02-22'), PARTITION pWeek_4 VALUES LESS THAN('2010-03-01') );
RANGE COLUMNS
PARTITION BY LIST COLUMNS()
HASH
HASH
CREATE
TABLE
expr
expr
num
num
store_id
创建员工信息表(ID为不空,名为varchar(30),lname VARCHAR(30),聘用日期不为空的默认“1970-01-01,分离的日期无效的默认9999-12-31 ',job_code int,int store_id)散列分区法(store_id)分区4;
PARTITIONS
PARTITIONS
expr
创建员工信息表(ID为不空,名为varchar(30),lname VARCHAR(30),聘用日期不为空的默认“1970-01-01,分离的日期无效的默认9999-12-31 ',job_code int,int store_id)散列分区法(年(雇))分区4;
expr
date_col
DATE
TO_DAYS(date_col)
date_col
YEAR(date_col)
TO_DAYS(date_col)
YEAR(date_col)
YEAR(date_col)
date_col
YEAR(date_col)
int_col
INT
POW(5-int_col,3) + 6
int_col
5
-1
6
-7
y=
c
xc
PARTITION BY HASH
num
expr
N
N
= MOD(expr
num
CREATE TABLE t1 (col1 INT, col2 CHAR(5), col3 DATE) PARTITION BY HASH( YEAR(col3) ) PARTITIONS 4;
t1
'2005-09-15'
MOD(YEAR('2005-09-01'),4)= MOD(2005,4)= 1
HASH
LINEAR
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 LINEAR HASH( YEAR(hired) ) PARTITIONS 4;
expr
N
num
N
num
V
V
= POWER(2, CEILING(LOG(2,num
)))
num
LOG(2,13)
CEILING(3.7004397181411)
V
=POWER(2,4)
N
=F
column_list
V
N
>=num
V
=V
N
=N
V
t1
创建表T1(col1 int,char(五),col2 col3日期)通过线性哈希分区(分区六年(COL3));
t1
'2003-04-14'
V
= POWER(2, CEILING( LOG(2,6) )) = 8N
= YEAR('2003-04-14') & (8 - 1) = 2003 & 7 = 3 (3 >= 6 is FALSE: record stored in partition #3)
V
= 8N
= YEAR('1998-10-19') & (8 - 1) = 1998 & 7 = 6 (6 >= 6 is TRUE: additional step required)N
= 6 & ((8 / 2) - 1) = 6 & 3 = 2 (2 >= 6 is FALSE: record stored in partition #2)
CREATE TABLE ... PARTITION BY
KEY
KEY
KEY
CREATE TABLE
创建表的K1(ID INT NOT NULL主键,名称为varchar(20))的key()分区2分区;
CREATE TABLE k1 ( id INT NOT NULL, name VARCHAR(20), UNIQUE KEY (id) ) PARTITION BY KEY() PARTITIONS 2;
NOT NULL
id
SHOW CREATE
TABLE
INFORMATION_SCHEMA.PARTITIONS
KEY
CREATE
TABLE
创建表的TM1(S1 char(32)主键)分区的分区键(S1)10;
PARTITION BY
KEY()
s1
ALTER TABLE DROP PRIMARY KEY
CREATE TABLE tk ( col1 INT NOT NULL, col2 CHAR(5), col3 DATE ) PARTITION BY LINEAR KEY (col1) PARTITIONS 3;
LINEAR
HASH
CREATE TABLE
创建表的TS(ID int,购买日期)按范围分区(年(购买))子分区哈希(to_days(购买)2)子分区(分区P0值小于(1990),分区P1值小于(2000),分区P2值小于最大值);
ts
p0
p2
3 * 2 = 6
PARTITION BY RANGE
RANGE
HASH
SUBPARTITION BY HASH
PARTITION BY HASH
SUBPARTITION BY
KEY
SUBPARTITION
CREATE TABLE ts (id INT, purchased DATE) PARTITION BY RANGE( YEAR(purchased) ) SUBPARTITION BY HASH( TO_DAYS(purchased) ) ( PARTITION p0 VALUES LESS THAN (1990) ( SUBPARTITION s0, SUBPARTITION s1 ), PARTITION p1 VALUES LESS THAN (2000) ( SUBPARTITION s2, SUBPARTITION s3 ), PARTITION p2 VALUES LESS THAN MAXVALUE ( SUBPARTITION s4, SUBPARTITION s5 ) );
SUBPARTITION
创建表的TS(ID int,购买日期)按范围分区(年(购买))子分区哈希(to_days(购买)(分区)P0值小于(1990)(子子S0,S1),分区P1值小于(2000),分区P2值小于最大值(子分区S2,子分区S3));
SUBPARTITIONS 2
SUBPARTITION
CREATE
TABLE
创建表的TS(ID int,购买日期)按范围分区(年(购买))子分区哈希(to_days(购买)(分区)P0值小于(1990)(子子S0,S1),分区P1值小于(2000)(S2 S3子,子分区,分区P2的值)小于最大值(子子S4、S5));
NULL
NULL
NULL
NULL
RANGE
p
MySQL的> CREATE TABLE t1 (
-> c1 INT,
-> c2 VARCHAR(20)
-> )
-> PARTITION BY RANGE(c1) (
-> PARTITION p0 VALUES LESS THAN (0),
-> PARTITION p1 VALUES LESS THAN (10),
-> PARTITION p2 VALUES LESS THAN MAXVALUE
-> );
查询行,0行受影响(0.09秒)MySQL > CREATE TABLE t2 (
-> c1 INT,
-> c2 VARCHAR(20)
-> )
-> PARTITION BY RANGE(c1) (
-> PARTITION p0 VALUES LESS THAN (-5),
-> PARTITION p1 VALUES LESS THAN (0),
-> PARTITION p2 VALUES LESS THAN (10),
-> PARTITION p3 VALUES LESS THAN MAXVALUE
-> );
查询行,0行受影响(0.09秒)
CREATE TABLE
PARTITIONS
mysql>SELECT TABLE_NAME, PARTITION_NAME, TABLE_ROWS, AVG_ROW_LENGTH, DATA_LENGTH
>FROM INFORMATION_SCHEMA.PARTITIONS
>WHERE TABLE_SCHEMA = 'p' AND TABLE_NAME LIKE 't_';
+------------+----------------+------------+----------------+-------------+ | TABLE_NAME | PARTITION_NAME | TABLE_ROWS | AVG_ROW_LENGTH | DATA_LENGTH | +------------+----------------+------------+----------------+-------------+ | t1 | p0 | 0 | 0 | 0 | | t1 | p1 | 0 | 0 | 0 | | t1 | p2 | 0 | 0 | 0 | | t2 | p0 | 0 | 0 | 0 | | t2 | p1 | 0 | 0 | 0 | | t2 | p2 | 0 | 0 | 0 | | t2 | p3 | 0 | 0 | 0 | +------------+----------------+------------+----------------+-------------+ 7 rows in set (0.00 sec)
NULL
SELECT
MySQL的> INSERT INTO t1 VALUES (NULL, 'mothra');
查询行,1行的影响(0秒)MySQL > INSERT INTO t2 VALUES (NULL, 'mothra');
查询行,1行的影响(0秒)MySQL > SELECT * FROM t1;
------ -------- | ID |名字| ------ -------- |空| Mothra | ------ -------- 1行集(0秒)MySQL > SELECT * FROM t2;
第二次会议(第三次会议)
INFORMATION_SCHEMA.PARTITIONS
MySQL的> SELECT TABLE_NAME, PARTITION_NAME, TABLE_ROWS, AVG_ROW_LENGTH, DATA_LENGTH
> FROM INFORMATION_SCHEMA.PARTITIONS
> WHERE TABLE_SCHEMA = 'p' AND TABLE_NAME LIKE 't_';
------------ ---------------- ------------ ---------------- ------------- | table_name | partition_name | table_rows | avg_row_length | data_length | ------------ ---------------- ------------ ---------------- ------------- | T1 | P0 | 1 | 20 | 20 | | T1 | | P1 0 0 0 | | | | T1 | | P2 0 0 0 | | | | T2 | P0 | 1 | 20 | 20 | | T2 | | P1 0 0 0 | | | | T2 | | P2 0 0 0 | | | | T2 | | P3 0 0 0 | | | ------------ ---------------- ------------ ---------------- ------------- 7 rows在设定(0.01秒)
SELECT
MySQL的> ALTER TABLE t1 DROP PARTITION p0;
查询行,0行受影响(0.16秒)MySQL > ALTER TABLE t2 DROP PARTITION p0;
查询行,0行受影响(0.16秒)MySQL > SELECT * FROM t1;
空集合(0秒)MySQL > SELECT * FROM t2;
空集(0.001秒)
ALTER TABLE ... DROP
PARTITION
NULL
CREATE
TABLE
创建表tndate(ID int,DT日期)按范围分区(年(DT))(分区P0值小于(1990年),分区P1值小于(2000年),分区P2值小于最大值);
YEAR(NULL)
dt
p0
LIST
NULL
NULL
mysql>CREATE TABLE ts1 (
->c1 INT,
->c2 VARCHAR(20)
->)
->PARTITION BY LIST(c1) (
->PARTITION p0 VALUES IN (0, 3, 6),
->PARTITION p1 VALUES IN (1, 4, 7),
->PARTITION p2 VALUES IN (2, 5, 8)
->);
Query OK, 0 rows affected (0.01 sec) mysql>INSERT INTO ts1 VALUES (9, 'mothra');
ERROR 1504 (HY000): Table has no partition for value 9 mysql>INSERT INTO ts1 VALUES (NULL, 'mothra');
ERROR 1504 (HY000): Table has no partition for value NULL
c1
8
NULL
ts2
NULL
MySQL的> CREATE TABLE ts2 (
-> c1 INT,
-> c2 VARCHAR(20)
-> )
-> PARTITION BY LIST(c1) (
-> PARTITION p0 VALUES IN (0, 3, 6),
-> PARTITION p1 VALUES IN (1, 4, 7),
-> PARTITION p2 VALUES IN (2, 5, 8),
-> PARTITION p3 VALUES IN (NULL)
-> );
查询好,为受影响的行(0.01秒)MySQL > CREATE TABLE ts3 (
-> c1 INT,
-> c2 VARCHAR(20)
-> )
-> PARTITION BY LIST(c1) (
-> PARTITION p0 VALUES IN (0, 3, 6),
-> PARTITION p1 VALUES IN (1, 4, 7, NULL),
-> PARTITION p2 VALUES IN (2, 5, 8)
-> );
查询好,为受影响的行(0.01秒)
NULL
VALUES IN (1, 4, 7, NULL)
VALUES IN
(NULL, 1, 4, 7)
c1
ts3
MySQL的> INSERT INTO ts2 VALUES (NULL, 'mothra');
查询行,1行的影响(0秒)MySQL > INSERT INTO ts3 VALUES (NULL, 'mothra');
查询行,1行的影响(0秒)
INFORMATION_SCHEMA.PARTITIONS
mysql>SELECT TABLE_NAME, PARTITION_NAME, TABLE_ROWS, AVG_ROW_LENGTH, DATA_LENGTH
>FROM INFORMATION_SCHEMA.PARTITIONS
>WHERE TABLE_SCHEMA = 'p' AND TABLE_NAME LIKE 'ts_';
+------------+----------------+------------+----------------+-------------+ | TABLE_NAME | PARTITION_NAME | TABLE_ROWS | AVG_ROW_LENGTH | DATA_LENGTH | +------------+----------------+------------+----------------+-------------+ | ts2 | p0 | 0 | 0 | 0 | | ts2 | p1 | 0 | 0 | 0 | | ts2 | p2 | 0 | 0 | 0 | | ts2 | p3 | 1 | 20 | 20 | | ts3 | p0 | 0 | 0 | 0 | | ts3 | p1 | 1 | 20 | 20 | | ts3 | p2 | 0 | 0 | 0 | +------------+----------------+------------+----------------+-------------+ 7 rows in set (0.01 sec)
SELECT
NULL
KEY
HASH
p
MySQL的> CREATE TABLE th (
-> c1 INT,
-> c2 VARCHAR(20)
-> )
-> PARTITION BY HASH(c1)
-> PARTITIONS 2;
查询行,0行受影响(0秒)
mysql> SELECT TABLE_NAME,PARTITION_NAME,TABLE_ROWS,AVG_ROW_LENGTH,DATA_LENGTH > FROM INFORMATION_SCHEMA.PARTITIONS > WHERE TABLE_SCHEMA = 'p' AND TABLE_NAME ='th'; +------------+----------------+------------+----------------+-------------+ | TABLE_NAME | PARTITION_NAME | TABLE_ROWS | AVG_ROW_LENGTH | DATA_LENGTH | +------------+----------------+------------+----------------+-------------+ | th | p0 | 0 | 0 | 0 | | th | p1 | 0 | 0 | 0 | +------------+----------------+------------+----------------+-------------+ 2 rows in set (0.00 sec)
TABLE_ROWS
c1
mysql>INSERT INTO th VALUES (NULL, 'mothra'), (0, 'gigan');
Query OK, 1 row affected (0.00 sec) mysql>SELECT * FROM th;
+------+---------+ | c1 | c2 | +------+---------+ | NULL | mothra | +------+---------+ | 0 | gigan | +------+---------+ 2 rows in set (0.01 sec)
N
N
HASH
0
INFORMATION_SCHEMA.PARTITIONS
mysql>SELECT TABLE_NAME, PARTITION_NAME, TABLE_ROWS, AVG_ROW_LENGTH, DATA_LENGTH
>FROM INFORMATION_SCHEMA.PARTITIONS
>WHERE TABLE_SCHEMA = 'p' AND TABLE_NAME ='th';
+------------+----------------+------------+----------------+-------------+ | TABLE_NAME | PARTITION_NAME | TABLE_ROWS | AVG_ROW_LENGTH | DATA_LENGTH | +------------+----------------+------------+----------------+-------------+ | th | p0 | 2 | 20 | 20 | | th | p1 | 0 | 0 | 0 | +------------+----------------+------------+----------------+-------------+ 2 rows in set (0.00 sec)
PARTITION BY
KEY
NULL
ALTER
TABLE
RANGE
HASH
ALTER
TABLE
partition_options
CREATE
TABLE
CREATE TABLE
创建表TRB3(ID int,NAME varchar(50),购买日期)按范围分区(年(购买)(分区)P0值小于(1990),分区P1值小于(1995),分区P2的值小于(2000),分区P3值小于(2005));
id
重点修改表TRB3分区(ID)分区2;
CREATE TABLE trb3
PARTITION BY KEY(id) PARTITIONS 2;
ALTER TABLE ... ENGINE = ...
PARTITION BY
DROP PARTITION
COALESCE
PARTITION
ALTER
TABLE
ALTER
TABLE
REORGANIZE PARTITION
ALTER TABLE ...
TRUNCATE PARTITION
RANGE
ALTER
TABLE
CREATE
TABLE
INSERT
MySQL的> CREATE TABLE tr (id INT, name VARCHAR(50), purchased DATE)
-> PARTITION BY RANGE( YEAR(purchased) ) (
-> PARTITION p0 VALUES LESS THAN (1990),
-> PARTITION p1 VALUES LESS THAN (1995),
-> PARTITION p2 VALUES LESS THAN (2000),
-> PARTITION p3 VALUES LESS THAN (2005),
-> PARTITION p4 VALUES LESS THAN (2010),
-> PARTITION p5 VALUES LESS THAN (2015)
-> );
查询行,0行受影响(0.28秒)MySQL > INSERT INTO tr VALUES
-> (1, 'desk organiser', '2003-10-15'),
-> (2, 'alarm clock', '1997-11-05'),
-> (3, 'chair', '2009-03-10'),
-> (4, 'bookcase', '1989-01-10'),
-> (5, 'exercise bike', '2014-05-09'),
-> (6, 'sofa', '1987-06-05'),
-> (7, 'espresso maker', '2011-11-22'),
-> (8, 'aquarium', '1992-08-04'),
-> (9, 'study desk', '2006-09-16'),
-> (10, 'lava lamp', '1998-12-25');
查询行,10行受影响(0.05秒)记录:10份:0警告:0
p2
MySQL的> SELECT * FROM tr
-> WHERE purchased BETWEEN '1995-01-01' AND '1999-12-31';
------ ------------- ------------ | ID |名字|购买| ------ ------------- ------------ | 2 |闹钟| 1997-11-05 | | 10 |熔岩灯| 1998-12-25 | ------ ------------- ------------ 2行集(0秒)
mysql> SELECT * FROM tr PARTITION (p2);
+------+-------------+------------+
| id | name | purchased |
+------+-------------+------------+
| 2 | alarm clock | 1997-11-05 |
| 10 | lava lamp | 1998-12-25 |
+------+-------------+------------+
2 rows in set (0.00 sec)
p2
MySQL的> ALTER TABLE tr DROP PARTITION p2;
查询好,为受影响的行(0秒)
NDBCLUSTER
ALTER
TABLE
SELECT
MySQL的> SELECT * FROM tr WHERE purchased
-> BETWEEN '1995-01-01' AND '1999-12-31';
空集(0.001秒)
DROP PARTITION
ALGORITHM={COPY|INPLACE}
DROP
PARTITION
ALGORITHM=INPLACE
DROP PARTITION
ALGORITHM=COPY
old_alter_table=ON
DROP
TRUNCATE TABLE
ALTER
TABLE ... REORGANIZE PARTITION
REORGANIZE PARTITION
SHOW CREATE
TABLE
MySQL的> SHOW CREATE TABLE tr\G
*************************** 1. row *************************** Table: trCreate Table: CREATE TABLE `tr` ( `id` int(11) DEFAULT NULL, `name` varchar(50) DEFAULT NULL, `purchased` date DEFAULT NULL) ENGINE=InnoDB DEFAULT CHARSET=latin1/*!50100 PARTITION BY RANGE ( YEAR(purchased))(PARTITION p0 VALUES LESS THAN (1990) ENGINE = InnoDB, PARTITION p1 VALUES LESS THAN (1995) ENGINE = InnoDB, PARTITION p3 VALUES LESS THAN (2005) ENGINE = InnoDB, PARTITION p4 VALUES LESS THAN (2010) ENGINE = InnoDB, PARTITION p5 VALUES LESS THAN (2015) ENGINE = InnoDB) */1 row in set (0.00 sec)
purchased
'2004-12-31'
mysql>INSERT INTO tr VALUES (11, 'pencil holder', '1995-07-12');
Query OK, 1 row affected (0.00 sec) mysql>SELECT * FROM tr WHERE purchased
->BETWEEN '1995-01-01' AND '2004-12-31';
+------+----------------+------------+ | id | name | purchased | +------+----------------+------------+ | 1 | desk organiser | 2003-10-15 | | 11 | pencil holder | 1995-07-12 | +------+----------------+------------+ 2 rows in set (0.00 sec) mysql>ALTER TABLE tr DROP PARTITION p3;
Query OK, 0 rows affected (0.03 sec) mysql>SELECT * FROM tr WHERE purchased
->BETWEEN '1995-01-01' AND '2004-12-31';
Empty set (0.00 sec)
ALTER TABLE ... DROP PARTITION
DELETE
LIST
RANGE
ALTER TABLE ... ADD PARTITION
CREATE TABLE members ( id INT, fname VARCHAR(25), lname VARCHAR(25), dob DATE ) PARTITION BY RANGE( YEAR(dob) ) ( PARTITION p0 VALUES LESS THAN (1980), PARTITION p1 VALUES LESS THAN (1990), PARTITION p2 VALUES LESS THAN (2000) );
members
修改表的成员添加分区(分区P3值小于(2010));
ADD PARTITION
MySQL的> ALTER TABLE members
> ADD PARTITION (
> PARTITION n VALUES LESS THAN (1970));
错误1463(hy000):值小于值必须严格?每个分区的增加
ALTER TABLE members REORGANIZE PARTITION p0 INTO ( PARTITION n0 VALUES LESS THAN (1970), PARTITION n1 VALUES LESS THAN (1980) );
SHOW CREATE TABLE
mysql> SHOW CREATE TABLE members\G
*************************** 1. row ***************************
Table: members
Create Table: CREATE TABLE `members` (
`id` int(11) DEFAULT NULL,
`fname` varchar(25) DEFAULT NULL,
`lname` varchar(25) DEFAULT NULL,
`dob` date DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1
/*!50100 PARTITION BY RANGE ( YEAR(dob))
(PARTITION n0 VALUES LESS THAN (1970) ENGINE = InnoDB,
PARTITION n1 VALUES LESS THAN (1980) ENGINE = InnoDB,
PARTITION p1 VALUES LESS THAN (1990) ENGINE = InnoDB,
PARTITION p2 VALUES LESS THAN (2000) ENGINE = InnoDB,
PARTITION p3 VALUES LESS THAN (2010) ENGINE = InnoDB) */
1 row in set (0.00 sec)
ALTER TABLE ... ADD
PARTITION
tt
CREATE TABLE
TT(create table id的数据,国际城市战略)分区(partition(数据)的P值(5,10,15),P1值的分区(6,12,18);
data
14
ALTER TABLE tt ADD PARTITION (PARTITION p2 VALUES IN (7, 14, 21));
LIST
MySQL的> ALTER TABLE tt ADD PARTITION
> (PARTITION np VALUES IN (4, 8, 12));
错误1465(hy000):多个定义在相同?列表分区
data
p1
12
np
p1
CREATE TABLE ...
SELECT ...
ALTER TABLE
... ADD PARTITION
创建员工信息表(ID为不空,名为varchar(50)不为空,varchar(50)模型不空,雇用日期不为空)按范围分区(年(雇))(分区P1值小于(1991),分区P2的值小于(1996),分区P3值小于(2001)、分区P4值小于(2005));改变表员工添加分区(分区P5值小于(2010),分区P6值小于最大值);
RANGE
mysql> SHOW CREATE TABLE members\G
*************************** 1. row ***************************
Table: members
Create Table: CREATE TABLE `members` (
`id` int(11) DEFAULT NULL,
`fname` varchar(25) DEFAULT NULL,
`lname` varchar(25) DEFAULT NULL,
`dob` date DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1
/*!50100 PARTITION BY RANGE ( YEAR(dob))
(PARTITION n0 VALUES LESS THAN (1970) ENGINE = InnoDB,
PARTITION n1 VALUES LESS THAN (1980) ENGINE = InnoDB,
PARTITION p1 VALUES LESS THAN (1990) ENGINE = InnoDB,
PARTITION p2 VALUES LESS THAN (2000) ENGINE = InnoDB,
PARTITION p3 VALUES LESS THAN (2010) ENGINE = InnoDB) */
1 row in set (0.00 sec)
ALTER
TABLE ... ADD PARTITION
ALTER
TABLE
修改表的成员重新分区(分区N0为S0值小于(1960),分区S1值小于(1970));
p0
s1
PARTITION ... VALUES
...
YEAR(dob)
YEAR(dob)
REORGANIZE PARTITION
ALTER TABLE members REORGANIZE PARTITION s0,s1 INTO ( PARTITION p0 VALUES LESS THAN (1970) );
REORGANIZE PARTITION
s1
REORGANIZE PARTITION
修改表 tbl_name
reorganize partition partition_list
为( partition_definitions
);
tbl_name
partition_list
partition_definitions
partition_definitions
CREATE TABLE
members
修改表的成员重新划分P0,P1,P2,P3(划分为M0值小于(1980),分区M1值小于(2000));
REORGANIZE PARTITION
tt
改变表格添加分区(分区NP值(4,8));改变表格整理划分为P1,NP(分区P1值(6、18),分区NP值(4、8、12));
ALTER TABLE ... REORGANIZE PARTITION
LIST
PARTITION
CREATE
TABLE
RANGE
partition_definitions
partition_list
p1
members
RANGE
members
p0
p1
REORGANIZE PARTITION
HASH
ALTER
TABLE ... PARTITION BY ...
通过哈希表成员分区(分区8年(DOB));
HASH
RANGE
HASH
ALTER TABLE ... COALESCE
PARTITION
CREATE TABLE clients ( id INT, fname VARCHAR(30), lname VARCHAR(30), signed DATE ) PARTITION BY HASH( MONTH(signed) ) PARTITIONS 12;
ALTER
TABLE
MySQL的> ALTER TABLE clients COALESCE PARTITION 4;
查询行,0行受影响(0.02秒)
COALESCE
KEY
LINEAR KEY
mysql>CREATE TABLE clients_lk (
->id INT,
->fname VARCHAR(30),
->lname VARCHAR(30),
->signed DATE
->)
->PARTITION BY LINEAR KEY(signed)
->PARTITIONS 12;
Query OK, 0 rows affected (0.03 sec) mysql>ALTER TABLE clients_lk COALESCE PARTITION 4;
Query OK, 0 rows affected (0.06 sec) Records: 0 Duplicates: 0 Warnings: 0
COALESCE PARTITION
mysql> ALTER TABLE clients COALESCE PARTITION 18;
ERROR 1478 (HY000): Cannot remove all partitions, use DROP TABLE instead
clients
ALTER TABLE clients ADD PARTITION PARTITIONS 6;
ALTER
TABLE
pt
EXCHANGE PARTITION
p
WITH TABLE
nt
pt
p
pt
nt
nt
nt
pt
nt
nt
nt
p
ALTER
INSERT
CREATE
ALTER TABLE
DROP
ALTER TABLE ...
EXCHANGE PARTITION
ALTER TABLE ...
EXCHANGE PARTITION
ALTER
TABLE ... EXCHANGE PARTITION
AUTO_INCREMENT
IGNORE
ALTER TABLE ...
EXCHANGE PARTITION
pt
p
nt
p
修改表 pt
交换分区 p
表 nt
;
WITH VALIDATION
WITHOUT
VALIDATION
ALTER TABLE ...
EXCHANGE PARTITION
ALTER TABLE
EXCHANGE PARTITION
ALTER TABLE
EXCHANGE PARTITION
ALTER TABLE
e
E(create table id不空,fname varchar(30),lname varchar(30)的城市范围分区(partition(ID),P值小于(50),P1值小于分区(partition),P2的值小于(150分),P3值小于(MaxValue))插入到;E值(1669年,“吉姆”,“史密斯”(337),“玛丽”,“琼斯”(16),“弗兰克”、“白”(2005年),“琳达”,“黑”);
e
mysql>CREATE TABLE e2 LIKE e;
Query OK, 0 rows affected (0.04 sec) mysql>ALTER TABLE e2 REMOVE PARTITIONING;
Query OK, 0 rows affected (0.07 sec) Records: 0 Duplicates: 0 Warnings: 0
e
INFORMATION_SCHEMA.PARTITIONS
MySQL的> SELECT PARTITION_NAME, TABLE_ROWS
FROM INFORMATION_SCHEMA.PARTITIONS
WHERE TABLE_NAME = 'e';
---------------- ------------ | partition_name | table_rows | ---------------- ------------ | P0 | 1 | | P1 | 0 | | P2 | 0 | | P3 | 3 | ---------------- ------------ 2行集(0秒)
InnoDB
INFORMATION_SCHEMA.PARTITIONS
p0
e2
ALTER
TABLE
MySQL的> ALTER TABLE e EXCHANGE PARTITION p0 WITH TABLE e2;
查询行,0行受影响(0.04秒)
INFORMATION_SCHEMA.PARTITIONS
mysql>SELECT PARTITION_NAME, TABLE_ROWS
FROM INFORMATION_SCHEMA.PARTITIONS
WHERE TABLE_NAME = 'e';
+----------------+------------+ | PARTITION_NAME | TABLE_ROWS | +----------------+------------+ | p0 | 0 | | p1 | 0 | | p2 | 0 | | p3 | 3 | +----------------+------------+ 4 rows in set (0.00 sec)
e2
mysql> SELECT * FROM e2;
+----+-------+-------+
| id | fname | lname |
+----+-------+-------+
| 16 | Frank | White |
+----+-------+-------+
1 row in set (0.00 sec)
e
id
PARTITIONS
MySQL的> INSERT INTO e VALUES (41, "Michael", "Green");
查询行,一行受到影响(0秒)MySQL > SELECT PARTITION_NAME, TABLE_ROWS
FROM INFORMATION_SCHEMA.PARTITIONS
WHERE TABLE_NAME = 'e';
---------------- ------------ | partition_name | table_rows | ---------------- ------------ | P0 | 1 | | P1 | 0 | | P2 | 0 | | P3 | 3 | ---------------- ------------ 4行集(0秒)
p0
ALTER
TABLE
MySQL的> ALTER TABLE e EXCHANGE PARTITION p0 WITH TABLE e2;
查询行,0行受影响(0.28秒)
p0
ALTER
TABLE
MySQL的> SELECT * FROM e;
------ ------- ------- | ID |名| LName | ------ ------- ------- | 16 |弗兰克|白| | 1669 |吉姆|史米斯| | 337 |玛丽|琼斯| | 2005 |琳达|黑| ------ ------- ------- 4行集(0秒)MySQL > SELECT PARTITION_NAME, TABLE_ROWS
FROM INFORMATION_SCHEMA.PARTITIONS
WHERE TABLE_NAME = 'e';
---------------- ------------ | partition_name | table_rows | ---------------- ------------ | P0 | 1 | | P1 | 0 | | P2 | 0 | | P3 | 3 | ---------------- ------------ 4行集(0秒)MySQL > SELECT * FROM e2;
---- --------- ------- | ID |名| LName | ---- --------- ------- | 41 |米迦勒|绿色| ---- --------- ------- 1行集(0秒)
ALTER TABLE ...
EXCHANGE PARTITION
p0
id
MySQL的> INSERT INTO e2 VALUES (51, "Ellen", "McDonald");
查询行,1行的影响(0.08秒)MySQL > ALTER TABLE e EXCHANGE PARTITION p0 WITH TABLE e2;
错误1707(hy000):发现行不匹配的分区
WITHOUT VALIDATION
MySQL的> ALTER TABLE e EXCHANGE PARTITION p0 WITH TABLE e2 WITHOUT VALIDATION;
查询行,0行受影响(0.02秒)
REPAIR TABLE
ALTER
TABLE ... REPAIR PARTITION
WITHOUT
VALIDATION
ALTER
TABLE ... EXCHANGE PARTITION
e
WITHOUT VALIDATION
# Create a partitioned table with 1 million rows in each partitionCREATE TABLE e ( id INT NOT NULL, fname VARCHAR(30), lname VARCHAR(30)) PARTITION BY RANGE (id) ( PARTITION p0 VALUES LESS THAN (1000001), PARTITION p1 VALUES LESS THAN (2000001),);mysql> SELECT COUNT(*) FROM e; | COUNT(*) |+----------+| 2000000 |+----------+1 row in set (0.27 sec)# View the rows in each partitionSELECT PARTITION_NAME, TABLE_ROWS FROM INFORMATION_SCHEMA.PARTITIONS WHERE TABLE_NAME = 'e';+----------------+-------------+| PARTITION_NAME | TABLE_ROWS |+----------------+-------------+| p0 | 1000000 || p1 | 1000000 |+----------------+-------------+2 rows in set (0.00 sec)# Create a nonpartitioned table of the same structure and populate it with 1 million rowsCREATE TABLE e2 ( id INT NOT NULL, fname VARCHAR(30), lname VARCHAR(30));mysql> SELECT COUNT(*) FROM e2;+----------+| COUNT(*) |+----------+| 1000000 |+----------+1 row in set (0.24 sec)# Create another nonpartitioned table of the same structure and populate it with 1 million rowsCREATE TABLE e3 ( id INT NOT NULL, fname VARCHAR(30), lname VARCHAR(30)); mysql> SELECT COUNT(*) FROM e3;+----------+| COUNT(*) |+----------+| 1000000 |+----------+1 row in set (0.25 sec)# Drop the rows from p0 of table emysql> DELETE FROM e WHERE id < 1000001;Query OK, 1000000 rows affected (5.55 sec)# Confirm that there are no rows in partition p0mysql> SELECT PARTITION_NAME, TABLE_ROWS FROM INFORMATION_SCHEMA.PARTITIONS WHERE TABLE_NAME = 'e';+----------------+------------+| PARTITION_NAME | TABLE_ROWS |+----------------+------------+| p0 | 0 || p1 | 1000000 |+----------------+------------+2 rows in set (0.00 sec) # Exchange partition p0 of table e with the table e2 'WITH VALIDATION'mysql> ALTER TABLE e EXCHANGE PARTITION p0 WITH TABLE e2 WITH VALIDATION;Query OK, 0 rows affected (0.74 sec)# Confirm that the partition was exchanged with table e2mysql> SELECT PARTITION_NAME, TABLE_ROWS FROM INFORMATION_SCHEMA.PARTITIONS WHERE TABLE_NAME = 'e';+----------------+------------+| PARTITION_NAME | TABLE_ROWS |+----------------+------------+| p0 | 1000000 || p1 | 1000000 |+----------------+------------+2 rows in set (0.00 sec)# Once again, drop the rows from p0 of table emysql> DELETE FROM e WHERE id < 1000001;Query OK, 1000000 rows affected (5.55 sec)# Confirm that there are no rows in partition p0mysql> SELECT PARTITION_NAME, TABLE_ROWS FROM INFORMATION_SCHEMA.PARTITIONS WHERE TABLE_NAME = 'e';+----------------+------------+| PARTITION_NAME | TABLE_ROWS |+----------------+------------+| p0 | 0 || p1 | 1000000 |+----------------+------------+2 rows in set (0.00 sec)# Exchange partition p0 of table e with the table e3 'WITHOUT VALIDATION'mysql> ALTER TABLE e EXCHANGE PARTITION p0 WITH TABLE e3 WITHOUT VALIDATION;Query OK, 0 rows affected (0.01 sec)# Confirm that the partition was exchanged with table e3mysql> SELECT PARTITION_NAME, TABLE_ROWS FROM INFORMATION_SCHEMA.PARTITIONS WHERE TABLE_NAME = 'e';+----------------+------------+| PARTITION_NAME | TABLE_ROWS |+----------------+------------+| p0 | 1000000 || p1 | 1000000 |+----------------+------------+2 rows in set (0.00 sec)
REPAIR
TABLE
ALTER
TABLE ... REPAIR PARTITION
ALTER TABLE ...
EXCHANGE PARTITION
RANGE
e
mysql>CREATE TABLE es (
->id INT NOT NULL,
->fname VARCHAR(30),
->lname VARCHAR(30)
->)
->PARTITION BY RANGE (id)
->SUBPARTITION BY KEY (lname)
->SUBPARTITIONS 2 (
->PARTITION p0 VALUES LESS THAN (50),
->PARTITION p1 VALUES LESS THAN (100),
->PARTITION p2 VALUES LESS THAN (150),
->PARTITION p3 VALUES LESS THAN (MAXVALUE)
->);
Query OK, 0 rows affected (2.76 sec) mysql>INSERT INTO es VALUES
->(1669, "Jim", "Smith"),
->(337, "Mary", "Jones"),
->(16, "Frank", "White"),
->(2005, "Linda", "Black");
Query OK, 4 rows affected (0.04 sec) Records: 4 Duplicates: 0 Warnings: 0 mysql>CREATE TABLE es2 LIKE es;
Query OK, 0 rows affected (1.27 sec) mysql>ALTER TABLE es2 REMOVE PARTITIONING;
Query OK, 0 rows affected (0.70 sec) Records: 0 Duplicates: 0 Warnings: 0
es
PARTITIONS
mysql>SELECT PARTITION_NAME, SUBPARTITION_NAME, TABLE_ROWS
->FROM INFORMATION_SCHEMA.PARTITIONS
->WHERE TABLE_NAME = 'es';
+----------------+-------------------+------------+ | PARTITION_NAME | SUBPARTITION_NAME | TABLE_ROWS | +----------------+-------------------+------------+ | p0 | p0sp0 | 1 | | p0 | p0sp1 | 0 | | p1 | p1sp0 | 0 | | p1 | p1sp1 | 0 | | p2 | p2sp0 | 0 | | p2 | p2sp1 | 0 | | p3 | p3sp0 | 3 | | p3 | p3sp1 | 0 | +----------------+-------------------+------------+ 8 rows in set (0.00 sec)
ALTER
TABLE
es
mysql> ALTER TABLE es EXCHANGE PARTITION p3sp0 WITH TABLE es2;
Query OK, 0 rows affected (0.29 sec)
mysql>SELECT PARTITION_NAME, SUBPARTITION_NAME, TABLE_ROWS
->FROM INFORMATION_SCHEMA.PARTITIONS
->WHERE TABLE_NAME = 'es';
+----------------+-------------------+------------+ | PARTITION_NAME | SUBPARTITION_NAME | TABLE_ROWS | +----------------+-------------------+------------+ | p0 | p0sp0 | 1 | | p0 | p0sp1 | 0 | | p1 | p1sp0 | 0 | | p1 | p1sp1 | 0 | | p2 | p2sp0 | 0 | | p2 | p2sp1 | 0 | | p3 | p3sp0 | 0 | | p3 | p3sp1 | 0 | +----------------+-------------------+------------+ 8 rows in set (0.00 sec) mysql>SELECT * FROM es2;
+------+-------+-------+ | id | fname | lname | +------+-------+-------+ | 1669 | Jim | Smith | | 337 | Mary | Jones | | 2005 | Linda | Black | +------+-------+-------+ 3 rows in set (0.00 sec)
mysql> ALTER TABLE es EXCHANGE PARTITION p3 WITH TABLE es2;
ERROR 1704 (HY000): Subpartitioned table, use subpartition instead of partition
mysql>CREATE TABLE es3 LIKE e;
Query OK, 0 rows affected (1.31 sec) mysql>ALTER TABLE es3 REMOVE PARTITIONING;
Query OK, 0 rows affected (0.53 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql>SHOW CREATE TABLE es3\G
*************************** 1. row *************************** Table: es3 Create Table: CREATE TABLE `es3` ( `id` int(11) NOT NULL, `fname` varchar(30) DEFAULT NULL, `lname` varchar(30) DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 1 row in set (0.00 sec) mysql>ALTER TABLE es3 ENGINE = MyISAM;
Query OK, 0 rows affected (0.15 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql>ALTER TABLE es EXCHANGE PARTITION p3sp0 WITH TABLE es3;
ERROR 1497 (HY000): The mix of handlers in the partitions is not allowed in this version of MySQL
CHECK TABLE
OPTIMIZE TABLE
ANALYZE TABLE
REPAIR TABLE
ALTER
TABLE
ALTER TABLE t1 REBUILD PARTITION p0, p1;
VARCHAR
BLOB
TEXT
ALTER
TABLE ... OPTIMIZE PARTITION
ALTER TABLE t1 OPTIMIZE PARTITION p0, p1;
OPTIMIZE PARTITION
ANALYZE PARTITION
InnoDB
ALTER
TABLE ... OPTIMIZE PARTITION
ALTER TABLE ...
ANALYZE PARTITION
ALTER TABLE t1 ANALYZE PARTITION p3;
ALTER TABLE t1 REPAIR PARTITION p0,p1;
REPAIR PARTITION
ALTER
IGNORE TABLE
CHECK TABLE
ALTER TABLE trb3 CHECK PARTITION p1;
p1
ALTER
TABLE ... REPAIR PARTITION
CHECK PARTITION
ALTER
IGNORE TABLE
ALL
ALTER TABLE ...
TRUNCATE PARTITION
TRUNCATE TABLE
SHOW CREATE TABLE
SHOW TABLE STATUS
EXPLAIN
SELECT
SELECT
SHOW CREATE TABLE
mysql> SHOW CREATE TABLE trb3\G
*************************** 1. row ***************************
Table: trb3
Create Table: CREATE TABLE `trb3` (
`id` int(11) DEFAULT NULL,
`name` varchar(50) DEFAULT NULL,
`purchased` date DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
/*!50100 PARTITION BY RANGE (YEAR(purchased))
(PARTITION p0 VALUES LESS THAN (1990) ENGINE = InnoDB,
PARTITION p1 VALUES LESS THAN (1995) ENGINE = InnoDB,
PARTITION p2 VALUES LESS THAN (2000) ENGINE = InnoDB,
PARTITION p3 VALUES LESS THAN (2005) ENGINE = InnoDB) */
0 row in set (0.00 sec)
SHOW TABLE STATUS
partitioned
INFORMATION_SCHEMA
PARTITIONS
SELECT
EXPLAIN
EXPLAIN
trb1
以创建表(ID int,NAME varchar(50),购买日期)按范围分区(ID)(分区P0值小于(3),分区P1值小于(7),分区P2的值小于(9),分区P3值小于(11));插入以价值(1台组织者',' 2003-10-15”),(2,“CD播放器”、“1993-11-05”),(3,“电视机”、“1996-03-10”),(4,“书架”、“1982-01-10”),(5,“自行车运动”,“2004-05-09”),(6,“沙发”、“1987-06-05”),(7,“爆米花机”、“2001-11-22”),(8,“水族馆”、“1992-08-04”),(9,“书桌”、“1984-09-16”),(10,“熔岩灯”、“1998-12-25”);
SELECT * FROM trb1;
MySQL的> EXPLAIN SELECT * FROM trb1\G
*************************** 1。行***************************编号:1 select_type:简单表:以P0,P1,P2分区,P3型:allpossible_keys:空键:空key_len:零号:空10行:额外的:使用filesort
mysql> EXPLAIN SELECT * FROM trb1 WHERE id < 5\G
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: trb1
partitions: p0,p1
type: ALL
possible_keys: NULL
key: NULL
key_len: NULL
ref: NULL
rows: 10
Extra: Using where
EXPLAIN
MySQL的> ALTER TABLE trb1 ADD PRIMARY KEY (id);
查询行,10行受影响(0.03秒)记录:10份:0警告:0mysql > EXPLAIN SELECT * FROM trb1 WHERE id < 5\G
*************************** 1。行***************************编号:1 select_type:简单表:以分区:P0、P1型:rangepossible_keys:主键:主要key_len:4编号:空7行:额外使用的地方:
EXPLAIN
NULL
rows
EXPLAIN
t1
create table(T1 fname varchar(50)不为零,lname varchar(50)不为零,代码区_ tinyint unsigned空槽,空槽的DOB的日期范围分区(市)区_ Code)(P值小于分区(partition),P1值小于(128),P2的值较小的分区比(192),分区P3值小于MaxValue);
SELECT
SELECT fname, lname, region_code, dob FROM t1 WHERE region_code > 125 AND region_code < 130;
p0
p1
WHERE
partition_column
=
constant
partition_column
IN
(constant1
,
constant2
, ...,
constantN
)
<
<=
>=
<>
WHERE
SELECT
DELETE
UPDATE
INSERT
WHERE
p1
WHERE
LIST
COLUMNS
DATE
DATETIME
YEAR()
TO_DAYS()
TO_SECONDS()
t2
DATE
create table(T2 fname varchar(50)不为零,lname varchar(50)不为零,代码区_ tinyint unsigned空槽,空槽的DOB的日期范围分区(市)年(DOB)(partition)D0值小于(1970),分区D1的值小于(1975),D2值分配低于(1980),分区值小于D3(1985年)、D4值小于分区的分区(1990),D5的值小于(2000),D6值小于分区(partition),2005年D7值小于MaxValue);
t2
SELECT * FROM t2 WHERE dob = '1982-06-23';UPDATE t2 SET region_code = 8 WHERE dob BETWEEN '1991-02-15' AND '1997-04-25';DELETE FROM t2 WHERE dob >= '1984-06-21' AND dob <= '1999-06-21'
YEAR('1984-06-21')
d3
YEAR('1999-06-21')
d5
d3
d5
DATE
WHERE
SELECT * FROM
partitioned_table
WHERE
date_column
<
'2008-12-00'
RANGE
LIST
region_code
create table(T3)fname varchar(50)不为零,lname varchar(50)不为零,代码区_ tinyint unsigned空槽,空槽的DOB约会列表(市)区的分区(partition _ Code)R0值在(1,3)分区,在R1的值(2,5,8),在分区的R2值(4,9),(partition R3的值在6,7,10);
SELECT * FROM t3 WHERE
region_code BETWEEN 1 AND 3
r1
r3
HASH
WHERE
=
CREATE TABLE t4 ( fname VARCHAR(50) NOT NULL, lname VARCHAR(50) NOT NULL, region_code TINYINT UNSIGNED NOT NULL, dob DATE NOT NULL ) PARTITION BY KEY(region_code) PARTITIONS 8;
UPDATE t4 WHERE region_code = 7;
IN
SELECT * FROM t4 WHERE region_code > 2 AND region_code < 6; SELECT * FROM t4 WHERE region_code BETWEEN 3 AND 5;
WHERE
DELETE FROM t4 WHERE region_code BETWEEN 4 AND 12;
WHERE
DELETE
HASH
dob
DATE
SELECT * FROM t4 WHERE dob >= '2001-04-14' AND dob <= '2005-10-15';
INT
WHERE year_col >= 2001 AND year_col <= 2005
NDB
WHERE
PARTITION
分区( partition_names
) partition_names
: partition_name
,…
partition_names
partition_name
partition_names
PARTITION
SELECT
SET @@SQL_MODE = ''; CREATE TABLE employees ( id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, fname VARCHAR(25) NOT NULL, lname VARCHAR(25) NOT NULL, store_id INT NOT NULL, department_id INT NOT NULL ) PARTITION BY RANGE(id) ( PARTITION p0 VALUES LESS THAN (5), PARTITION p1 VALUES LESS THAN (10), PARTITION p2 VALUES LESS THAN (15), PARTITION p3 VALUES LESS THAN MAXVALUE ); INSERT INTO employees VALUES ('', 'Bob', 'Taylor', 3, 2), ('', 'Frank', 'Williams', 1, 2), ('', 'Ellen', 'Johnson', 3, 4), ('', 'Jim', 'Smith', 2, 4), ('', 'Mary', 'Jones', 1, 1), ('', 'Linda', 'Black', 2, 3), ('', 'Ed', 'Jones', 2, 1), ('', 'June', 'Wilson', 3, 1), ('', 'Andy', 'Smith', 1, 3), ('', 'Lou', 'Waters', 2, 4), ('', 'Jill', 'Stone', 1, 4), ('', 'Roger', 'White', 3, 2), ('', 'Howard', 'Andrews', 1, 2), ('', 'Fred', 'Goldberg', 3, 3), ('', 'Barbara', 'Brown', 2, 3), ('', 'Alice', 'Rogers', 2, 2), ('', 'Mark', 'Morgan', 3, 3), ('', 'Karen', 'Cole', 3, 2);
p1
MySQL的> SELECT * FROM employees PARTITION (p1);
————————————————| -------选择ID | fname | L NAME |商店ID ID _ |部_ |——------- --------选择|玛丽琼斯| --------------- | 5 1 6 1 | | | | |琳达|黑| 2 3 7 | | | | ED | | 2 1 | |琼斯8月| | |威尔逊| 3 1 9 | | | |安迪史密斯| | | | 1——3——5 rows --------------- -------选择集(0秒)
SELECT *
FROM employees WHERE id BETWEEN 5 AND 9
SELECT * FROM
employees PARTITION (p1, p2)
p2
PARTITION
ORDER BY
HAVING
employees
MySQL的> SELECT * FROM employees PARTITION (p0, p2)
-> WHERE lname LIKE 'S%';
---- ------- ------- ---------- --------------- | ID |名| LName | store_id | department_id | ---- ------- ------- ---------- --------------- | 4 |吉姆|史米斯| 2 | 4 | | 11 |吉尔|石| 1 | 4 | ---- ------- ------- ---------- --------------- 2行集(0秒)MySQL > SELECT id, CONCAT(fname, ' ', lname) AS name
-> FROM employees PARTITION (p0) ORDER BY lname;
---- ---------------- | ID |名字| ---- ---------------- | 3 | Ellen Johnson | | 4 | Jim Smith | | 1 |鲍勃·泰勒| | 2 | Frank Williams | ---- ---------------- 4行集(0.06秒)MySQL > SELECT store_id, COUNT(department_id) AS c
-> FROM employees PARTITION (p1,p2,p3)
-> GROUP BY store_id HAVING c > 4;
--- ---------- | C | store_id | --- ---------- | 5 | 2 | | 5 | 3 | --- ---------- 2行集(0秒)
[LINEAR] HASH
p0
p2
N-1
N
X
X
X
X
X
M-1
M
SELECT
mysql>CREATE TABLE employees_sub (
->id INT NOT NULL AUTO_INCREMENT,
->fname VARCHAR(25) NOT NULL,
->lname VARCHAR(25) NOT NULL,
->store_id INT NOT NULL,
->department_id INT NOT NULL,
->PRIMARY KEY pk (id, lname)
->)
->PARTITION BY RANGE(id)
->SUBPARTITION BY KEY (lname)
->SUBPARTITIONS 2 (
->PARTITION p0 VALUES LESS THAN (5),
->PARTITION p1 VALUES LESS THAN (10),
->PARTITION p2 VALUES LESS THAN (15),
->PARTITION p3 VALUES LESS THAN MAXVALUE
->);
Query OK, 0 rows affected (1.14 sec) mysql>INSERT INTO employees_sub
# reuse data in employees table ->SELECT * FROM employees;
Query OK, 18 rows affected (0.09 sec) Records: 18 Duplicates: 0 Warnings: 0 mysql>SELECT id, CONCAT(fname, ' ', lname) AS name
->FROM employees_sub PARTITION (p2sp1);
+----+---------------+ | id | name | +----+---------------+ | 10 | Lou Waters | | 14 | Fred Goldberg | +----+---------------+ 2 rows in set (0.00 sec)
PARTITION
SELECT
INSERT ...
SELECT
MySQL的> CREATE TABLE employees_copy LIKE employees;
查询行,0行受影响(0.28秒)MySQL > INSERT INTO employees_copy
-> SELECT * FROM employees PARTITION (p2);
查询好了,五行受影响(0.04秒)记录:5重复:0警告:0mysql > SELECT * FROM employees_copy;
---- -------- ---------- ---------- --------------- | ID |名| LName | store_id | department_id | ---- -------- ---------- ---------- --------------- |十|娄|水域| 2 |四| | 11 |吉尔|石| 1 |四| | 12 |罗杰|白|三| 2 | | 13 |霍华德|安德鲁斯| 1 | 2 | | 14 |弗莱德|戈德堡|三|三| ---- -------- ---------- ---------- ---------------五行集(0.001秒)
CREATE TABLE stores ( id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, city VARCHAR(30) NOT NULL ) PARTITION BY HASH(id) PARTITIONS 2; INSERT INTO stores VALUES ('', 'Nambucca'), ('', 'Uranga'), ('', 'Bellingen'), ('', 'Grafton'); CREATE TABLE departments ( id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, name VARCHAR(30) NOT NULL ) PARTITION BY KEY(id) PARTITIONS 2; INSERT INTO departments VALUES ('', 'Sales'), ('', 'Customer Service'), ('', 'Delivery'), ('', 'Accounting');
PARTITION
departments
stores
MySQL的> SELECT
-> e.id AS 'Employee ID', CONCAT(e.fname, ' ', e.lname) AS Name,
-> s.city AS City, d.name AS department
-> FROM employees AS e
-> JOIN stores PARTITION (p1) AS s ON e.store_id=s.id
-> JOIN departments PARTITION (p0) AS d ON e.department_id=d.id
-> ORDER BY e.lname;
------------- --------------- ----------- ------------ |雇员ID |名字|市|部| ------------- --------------- ----------- ------------ | 14 | Fred Goldberg |贝林真|交付| | 5 | Mary Jones |南巴卡|销售| | 17 | Mark Morgan |贝林真|交付| | 9 |安迪史密夫|南巴卡|交付| | 8 |月Wilson |贝林真|销售| ------------- --------------- ----------- ------------ 5行集(0秒)
PARTITION
DELETE
MySQL的> SELECT * FROM employees WHERE fname LIKE 'j%';
---- ------- -------- ---------- --------------- | ID |名| LName | store_id | department_id | ---- ------- -------- ---------- --------------- |四|吉姆|史米斯| 2 |四| | 8 |月|威尔逊|三| 1 | | 11 |吉尔|石| 1 |四| ---- ------- -------- ---------- ---------------三行集(0.001秒)MySQL > DELETE FROM employees PARTITION (p0, p1)
-> WHERE fname LIKE 'j%';
查询行,2行受影响(0.09秒)MySQL > SELECT * FROM employees WHERE fname LIKE 'j%';
————————————| ------- -------选择ID | fname | L NAME |商店ID ID _ |部_ |——------- -------选择11 | --------------- |吉尔|石| 1——4 | | ------- -------选择1行到集(0秒)
p0
WHERE
SELECT
p2
UPDATE
mysql>UPDATE employees PARTITION (p0)
->SET store_id = 2 WHERE fname = 'Jill';
Query OK, 0 rows affected (0.00 sec) Rows matched: 0 Changed: 0 Warnings: 0 mysql>SELECT * FROM employees WHERE fname = 'Jill';
+----+-------+-------+----------+---------------+ | id | fname | lname | store_id | department_id | +----+-------+-------+----------+---------------+ | 11 | Jill | Stone | 1 | 4 | +----+-------+-------+----------+---------------+ 1 row in set (0.00 sec) mysql>UPDATE employees PARTITION (p2)
->SET store_id = 2 WHERE fname = 'Jill';
Query OK, 1 row affected (0.09 sec) Rows matched: 1 Changed: 1 Warnings: 0 mysql>SELECT * FROM employees WHERE fname = 'Jill';
+----+-------+-------+----------+---------------+ | id | fname | lname | store_id | department_id | +----+-------+-------+----------+---------------+ | 11 | Jill | Stone | 2 | 4 | +----+-------+-------+----------+---------------+ 1 row in set (0.00 sec)
PARTITION
DELETE
INSERT
REPLACE
MySQL的> INSERT INTO employees PARTITION (p2) VALUES (20, 'Jan', 'Jones', 1, 3);
错误1729(hy000):发现一行不匹配给定的分区设置 MySQL的> INSERT INTO employees PARTITION (p3) VALUES (20, 'Jan', 'Jones', 1, 3);
查询行,1行的影响(0.07秒)MySQL >替换员工分区(P0)值(20,'月','琼斯',3, 2); 错误1729(hy000):发现一行不匹配给定的分区设置 MySQL >替换员工分区(P3)值(20,'月','琼斯',3,2);查询行,2行受影响(0.09秒)
InnoDB
partition_names
INSERT
mysql>ALTER TABLE employees
->REORGANIZE PARTITION p3 INTO (
->PARTITION p3 VALUES LESS THAN (20),
->PARTITION p4 VALUES LESS THAN (25),
->PARTITION p5 VALUES LESS THAN MAXVALUE
->);
Query OK, 6 rows affected (2.09 sec) Records: 6 Duplicates: 0 Warnings: 0 mysql>SHOW CREATE TABLE employees\G
*************************** 1. row *************************** Table: employees Create Table: CREATE TABLE `employees` ( `id` int(11) NOT NULL AUTO_INCREMENT, `fname` varchar(25) NOT NULL, `lname` varchar(25) NOT NULL, `store_id` int(11) NOT NULL, `department_id` int(11) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=27 DEFAULT CHARSET=utf8mb4 /*!50100 PARTITION BY RANGE (id) (PARTITION p0 VALUES LESS THAN (5) ENGINE = InnoDB, PARTITION p1 VALUES LESS THAN (10) ENGINE = InnoDB, PARTITION p2 VALUES LESS THAN (15) ENGINE = InnoDB, PARTITION p3 VALUES LESS THAN (20) ENGINE = InnoDB, PARTITION p4 VALUES LESS THAN (25) ENGINE = InnoDB, PARTITION p5 VALUES LESS THAN MAXVALUE ENGINE = InnoDB) */ 1 row in set (0.00 sec) mysql>INSERT INTO employees PARTITION (p3, p4) VALUES
->(24, 'Tim', 'Greene', 3, 1), (26, 'Linda', 'Mills', 2, 1);
ERROR 1729 (HY000): Found a row not matching the given partition set mysql>INSERT INTO employees PARTITION (p3, p4. p5) VALUES
->(24, 'Tim', 'Greene', 3, 1), (26, 'Linda', 'Mills', 2, 1);
Query OK, 2 rows affected (0.06 sec) Records: 2 Duplicates: 0 Warnings: 0
INSERT
REPLACE
NDB
+
-
*
[LINEAR] KEY
NULL
ERROR_FOR_DIVISION_BY_ZERO
column
value
column
value
CREATE TABLE
NO_UNSIGNED_SUBTRACTION
MySQL的> SELECT @@sql_mode;
------------ | @ @ sql_mode | ------------ | | ------------ 1行集(0秒)MySQL > CREATE TABLE tu (c1 BIGINT UNSIGNED)
-> PARTITION BY RANGE(c1 - 10) (
-> PARTITION p0 VALUES LESS THAN (-5),
-> PARTITION p1 VALUES LESS THAN (0),
-> PARTITION p2 VALUES LESS THAN (5),
-> PARTITION p3 VALUES LESS THAN (10),
-> PARTITION p4 VALUES LESS THAN (MAXVALUE)
-> );
(hy000):错误1563年分配常数不分区函数域 MySQL的> SET sql_mode='NO_UNSIGNED_SUBTRACTION';
查询好,为受影响的行(0.001秒)MySQL > SELECT @@sql_mode;
------------------------- | @ @ sql_mode | ------------------------- | no_unsigned_subtraction | ------------------------- 1行集(0秒)MySQL > CREATE TABLE tu (c1 BIGINT UNSIGNED)
-> PARTITION BY RANGE(c1 - 10) (
-> PARTITION p0 VALUES LESS THAN (-5),
-> PARTITION p1 VALUES LESS THAN (0),
-> PARTITION p2 VALUES LESS THAN (5),
-> PARTITION p3 VALUES LESS THAN (10),
-> PARTITION p4 VALUES LESS THAN (MAXVALUE)
-> );
查询行,0行受影响(0.05秒)
NO_UNSIGNED_SUBTRACTION
mysql>SET sql_mode='';
Query OK, 0 rows affected (0.00 sec) mysql>SELECT * FROM tu;
ERROR 1563 (HY000): Partition constant is out of partition function domain mysql>INSERT INTO tu VALUES (20);
ERROR 1563 (HY000): Partition constant is out of partition function domain
ALTER
TABLE
REORGANIZE PARTITION
large_files_support
open_files_limit
innodb_file_per_table
INSERT
UPDATE
LOAD
DATA
open_files_limit
InnoDB
InnoDB
InnoDB
InnoDB
CREATE
TABLE
ALTER TABLE
ALTER TABLE ... ORDER BY
column
REPLACE
FULLTEXT
POINT
ALTER
TABLE ... PARTITION BY ...
ENUM
LINEAR
TEXT
BLOB
CREATE TABLE
创建表的TKC(C1 char)分区的分区键(C1)4;创建表TKE(C1枚举(红,橙,黄,绿,蓝,靛蓝','紫'))通过线性关键分区(C1)分区6;
RANGE COLUMNS
DATE
DATETIME
CREATE
TABLE
CREATE TABLE rc (c1 INT, c2 DATE)PARTITION BY RANGE COLUMNS(c2) ( PARTITION p0 VALUES LESS THAN('1990-01-01'), PARTITION p1 VALUES LESS THAN('1995-01-01'), PARTITION p2 VALUES LESS THAN('2000-01-01'), PARTITION p3 VALUES LESS THAN('2005-01-01'), PARTITION p4 VALUES LESS THAN(MAXVALUE));CREATE TABLE lc (c1 INT, c2 CHAR(1))PARTITION BY LIST COLUMNS(c2) ( PARTITION p0 VALUES IN('a', 'd', 'g', 'j', 'm', 'p', 's', 'v', 'y'), PARTITION p1 VALUES IN('b', 'e', 'h', 'k', 'n', 'q', 't', 'w', 'z'), PARTITION p2 VALUES IN('c', 'f', 'i', 'l', 'o', 'r', 'u', 'x', NULL));
NULL
HASH
RANGE
HASH
SUBPARTITION BY KEY
CREATE TABLE ts ( id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, name VARCHAR(30) );
KEY
创建表的TS(ID int不空auto_increment主键,名称为varchar(30))的key()分区4分区;
CREATE TABLE ts ( id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, name VARCHAR(30) ) PARTITION BY KEY(id) PARTITIONS 4;
mysql>CREATE TABLE ts (
->id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
->name VARCHAR(30)
->)
->PARTITION BY RANGE(id)
->SUBPARTITION BY KEY()
->SUBPARTITIONS 4
->(
->PARTITION p0 VALUES LESS THAN (100),
->PARTITION p1 VALUES LESS THAN (MAXVALUE)
->);
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') mysql>CREATE TABLE ts (
->id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
->name VARCHAR(30)
->)
->PARTITION BY RANGE(id)
->SUBPARTITION BY KEY(id)
->SUBPARTITIONS 4
->(
->PARTITION p0 VALUES LESS THAN (100),
->PARTITION p1 VALUES LESS THAN (MAXVALUE)
->);
Query OK, 0 rows affected (0.07 sec)
DATA DIRECTORY
InnoDB
CHECK TABLE
OPTIMIZE TABLE
ANALYZE TABLE
REPAIR TABLE
ALTER TABLE ... REBUILD
PARTITION
ANALYZE
OPTIMIZE
TRUNCATE
CREATE TABLE t1 ( col1 INT NOT NULL, col2 DATE NOT NULL, col3 INT NOT NULL, col4 INT NOT NULL, UNIQUE KEY (col1, col2) ) PARTITION BY HASH(col3) PARTITIONS 4; CREATE TABLE t2 ( col1 INT NOT NULL, col2 DATE NOT NULL, col3 INT NOT NULL, col4 INT NOT NULL, UNIQUE KEY (col1), UNIQUE KEY (col3) ) PARTITION BY HASH(col1 + col3) PARTITIONS 4;
CREATE TABLE t1 ( col1 INT NOT NULL, col2 DATE NOT NULL, col3 INT NOT NULL, col4 INT NOT NULL, UNIQUE KEY (col1, col2, col3) ) PARTITION BY HASH(col3) PARTITIONS 4; CREATE TABLE t2 ( col1 INT NOT NULL, col2 DATE NOT NULL, col3 INT NOT NULL, col4 INT NOT NULL, UNIQUE KEY (col1, col3) ) PARTITION BY HASH(col1 + col3) PARTITIONS 4;
mysql>CREATE TABLE t3 (
->col1 INT NOT NULL,
->col2 DATE NOT NULL,
->col3 INT NOT NULL,
->col4 INT NOT NULL,
->UNIQUE KEY (col1, col2),
->UNIQUE KEY (col3)
->)
->PARTITION BY HASH(col1 + col3)
->PARTITIONS 4;
ERROR 1491 (HY000): A PRIMARY KEY must include all columns in the table's partitioning function
CREATE TABLE
col3
MySQL的> CREATE TABLE t3 (
-> col1 INT NOT NULL,
-> col2 DATE NOT NULL,
-> col3 INT NOT NULL,
-> col4 INT NOT NULL,
-> UNIQUE KEY (col1, col2, col3),
-> UNIQUE KEY (col3)
-> )
-> PARTITION BY HASH(col3)
-> PARTITIONS 4;
查询行,0行受影响(0.05秒)
col3
CREATE TABLE t4 ( col1 INT NOT NULL, col2 INT NOT NULL, col3 INT NOT NULL, col4 INT NOT NULL, UNIQUE KEY (col1, col3), UNIQUE KEY (col2, col4) );
CREATE TABLE t5 ( col1 INT NOT NULL, col2 DATE NOT NULL, col3 INT NOT NULL, col4 INT NOT NULL, PRIMARY KEY(col1, col2) ) PARTITION BY HASH(col3) PARTITIONS 4; CREATE TABLE t6 ( col1 INT NOT NULL, col2 DATE NOT NULL, col3 INT NOT NULL, col4 INT NOT NULL, PRIMARY KEY(col1, col3), UNIQUE KEY(col2) ) PARTITION BY HASH( YEAR(col2) ) PARTITIONS 4;
CREATE TABLE t7 ( col1 INT NOT NULL, col2 DATE NOT NULL, col3 INT NOT NULL, col4 INT NOT NULL, PRIMARY KEY(col1, col2) ) PARTITION BY HASH(col1 + YEAR(col2)) PARTITIONS 4; CREATE TABLE t8 ( col1 INT NOT NULL, col2 DATE NOT NULL, col3 INT NOT NULL, col4 INT NOT NULL, PRIMARY KEY(col1, col2, col4), UNIQUE KEY(col2, col1) ) PARTITION BY HASH(col1 + YEAR(col2)) PARTITIONS 4;
mysql>CREATE TABLE t_no_pk (c1 INT, c2 INT)
->PARTITION BY RANGE(c1) (
->PARTITION p0 VALUES LESS THAN (10),
->PARTITION p1 VALUES LESS THAN (20),
->PARTITION p2 VALUES LESS THAN (30),
->PARTITION p3 VALUES LESS THAN (40)
->);
Query OK, 0 rows affected (0.12 sec)
t_no_pk
ALTER
TABLE
可能的 ALTER TABLE t_no_pk ADD PRIMARY KEY(c1);
查询行,0行受影响(0.13秒)记录:0份:0警告:0 #降本pkmysql > ALTER TABLE t_no_pk DROP PRIMARY KEY;
查询行,0行受影响(0.10秒)记录:0份:0警告:0 #使用另一个可能的pkmysql > ALTER TABLE t_no_pk ADD PRIMARY KEY(c1, c2);
查询行,0行受影响(0.12秒)记录:0份:0警告:0 #降本pkmysql > ALTER TABLE t_no_pk DROP PRIMARY KEY;
查询行,0行受影响(0.09秒)记录:0份:0警告:0
c1
#失败与错误1503mysql > ALTER TABLE t_no_pk ADD PRIMARY KEY(c2);
错误1503(hy000):主键必须包括表中的所有列的分区函数
t_no_pk
c2
c2
ALTER
TABLE ... PARTITION BY
mysql>CREATE TABLE np_pk (
->id INT NOT NULL AUTO_INCREMENT,
->name VARCHAR(50),
->added DATE,
->PRIMARY KEY (id)
->);
Query OK, 0 rows affected (0.08 sec)
ALTER
TABLE
mysql>ALTER TABLE np_pk
->PARTITION BY HASH( TO_DAYS(added) )
->PARTITIONS 4;
ERROR 1503 (HY000): A PRIMARY KEY must include all columns in the table's partitioning function
id
MySQL的> ALTER TABLE np_pk
-> PARTITION BY HASH(id)
-> PARTITIONS 4;
查询行,0行受影响(0.11秒)记录:0份:0警告:0
np_pk
InnoDB
NDB
ALTER
TABLE ... OPTIMIZE PARTITION
ALTER TABLE ... REBUILD PARTITION
KEY
ALTER TABLE ...
REMOVE PARTITIONING
ALTER TABLE ...
ENGINE=INNODB
MyISAM
TO_DAYS()
TO_SECONDS()
YEAR()
UNIX_TIMESTAMP()
INT
DECIMAL
CREATE
TABLE
MySQL的> CREATE TABLE t (c FLOAT) PARTITION BY LIST( FLOOR(c) )(
-> PARTITION p0 VALUES IN (1,3,5),
-> PARTITION p1 VALUES IN (2,4,6)
-> );
错误1490(hy000):分区函数返回类型错误
EXTRACT()
EXTRACT(WEEK FROM
col
)default_week_format
EXTRACT()