##mysql分區
測試添加分區和刪除分區
//測試range分區
(1)建立range分區
mysql> CREATE TABLE titles_range
(emp_no INT NOT NULL,
title VARCHAR(50) NOT NULL,
from_date DATE NOT NULL,
to_date DATE,
KEY (emp_no),
PRIMARY KEY (emp_no,title, from_date) )
partition by range columns(from_date)
(partition p01 values less than ('1985-12-31'),
partition p02 values less than ('1995-12-31'),
partition p03 values less than ('2000-12-31'),
partition p04 values less than ('3000-12-31')
);
(2)執行計劃,查詢結果在p01
mysql> explain partitions select * from titles_range where from_date < '1985-12-31';
+----+-------------+--------------+------------+------+---------------+------+---------+------+------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+--------------+------------+------+---------------+------+---------+------+------+-------------+
| 1 | SIMPLE | titles_range | p01 | ALL | NULL | NULL | NULL | NULL | 2 | Using where |
+----+-------------+--------------+------------+------+---------------+------+---------+------+------+-------------+
(3)刪除p01
mysql> alter table titles_range drop partition p01;
(4)從新查詢 查詢結果在p02
mysql> explain partitions select * from titles_range where from_date < '1985-12-31';
+----+-------------+--------------+------------+------+---------------+------+---------+------+------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+--------------+------------+------+---------------+------+---------+------+------+-------------+
| 1 | SIMPLE | titles_range | p02 | ALL | NULL | NULL | NULL | NULL | 2 | Using where |
+----+-------------+--------------+------------+------+---------------+------+---------+------+------+-------------+
(5)添加分區
//添加3001-12-31 比 3000-12-31大的語法
mysql> alter table titles_range add partition(
partition p01 values less than ('3001-12-31')
PARTITION p6 VALUES LESS THAN MAXVALUE);
//重組p02 reorganize
mysql> alter table titles_range reorganize partition p02 into(partition n0 values less than ('1985-12-31'),partition n1 values less than ('1995-12-31'));
//從新查詢1985-12-31
mysql> explain partitions select * from titles_range where from_date < '1985-12-31';
+----+-------------+--------------+------------+------+---------------+------+---------+------+------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+--------------+------------+------+---------------+------+---------+------+------+-------------+
| 1 | SIMPLE | titles_range | n0 | ALL | NULL | NULL | NULL | NULL | 2 | Using where |
+----+-------------+--------------+------------+------+---------------+------+---------+------+------+-------------+
//測試list分區
(1)建立list分區
CREATE TABLE titles_list (
emp_no INT NOT NULL,
title VARCHAR(50) NOT NULL,
from_date DATE NOT NULL,
to_date DATE,
KEY (emp_no),
PRIMARY KEY (emp_no,title, from_date)
) partition by list columns(title)
(
partition p0 values in ('Assistant Engineer'),
partition p1 values in ('Engineer'),
partition p2 values in ('Manager'),
partition p3 values in ('Senior Engineer'),
partition p4 values in ('Senior Staff'),
partition p5 values in ('Staff'),
partition p6 values in ('Technique Leader')
);
(2)刪除p0
mysql> alter table titles_list drop partition p0;
(3)
//添加分區
mysql> alter table titles_list add partition(partition p0 values in ('Assistant Engineer'));
或者
mysql> alter table titles_list reorganize partition p1 into(partition n0 values in ('Assistant Engineer'),partition n1 values in ('Engineer'));
//測試hash分區
(1)建立hash分區
CREATE TABLE titles_hash (
emp_no INT NOT NULL,
title VARCHAR(50) NOT NULL,
from_date DATE NOT NULL,
to_date DATE,
KEY (emp_no),
PRIMARY KEY (emp_no,title, from_date)
) partition by hash(emp_no)
partitions 4;
(2)刪除分區
mysql> alter table titles_hash coalesce partition 3 ;
(3)添加分區
mysql> alter table titles_hash add partition partitions 3 ;
//測試key分區
(1)建立key分區
CREATE TABLE titles_key(
emp_no INT NOT NULL,
title VARCHAR(50) NOT NULL,
from_date DATE NOT NULL,
to_date DATE,
KEY (emp_no),
PRIMARY KEY (emp_no,title, from_date)
) partition by hash(emp_no)
partitions 4;
(2)刪除分區
mysql> alter table titles_key coalesce partition 3 ;
(3)添加分區
mysql> alter table titles_key add partition partitions 3 ;mysql