Mysql --分区表(5)Columns分区

COLUMNS分区

COLUMNS分区是RANGE和LIST分区的变种。COLUMNS分区支持多列作为分区键进行分区
RANGE COLUNMS分区和LIST COLUMNS都支持非INT型列作为分区键:
1.所有数据类型: TINYINT, SMALLINT, MEDIUMINT, INT (INTEGER), and BIGINT. (This is the same as with partitioning by RANGE and LIST.)
Decimal和Float不被支持
2.日期类型:date和datetime
3.字符类型: CHAR, VARCHAR, BINARY, and VARBINARY.
TEXT和BLOB不支持

RANGE COLUMNS分区

RANGE COLUMNS分区和RANGE分区的几点区别:
1.RANGE COLUMNS分区键不支持使用表达式,只可以使用列名
2.RANGE COLUMNS可以支持多列作为分区键进行分区
3.RANGE COLUMNS分区是基于元组进行比较的
4.RANGE COLUMNS分区不仅仅限于使用整数型列作为分区键,date和datetime列也可以

Culumns分区支持非整数分区,这样创建日期分区就不需要通过函数进行转换了。

CREATE TABLE members_mon (
    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 ('2016-02-01'),
    PARTITION p1 VALUES LESS THAN ('2016-03-01'),
    PARTITION p2 VALUES LESS THAN ('2016-04-01'),
    PARTITION p3 VALUES LESS THAN ('2016-05-01'),
    PARTITION p4 VALUES LESS THAN ('2016-06-01'),
    PARTITION p5 VALUES LESS THAN ('2016-07-01'),
    PARTITION p6 VALUES LESS THAN ('2016-08-01'),
    PARTITION p7 VALUES LESS THAN ('2016-09-01'),
    PARTITION p8 VALUES LESS THAN ('2016-10-01'),
    PARTITION p9 VALUES LESS THAN ('2016-11-01'),
    PARTITION p10 VALUES LESS THAN ('2016-12-01'),
    PARTITION p11 VALUES LESS THAN ('2017-01-01'),
    PARTITION p12 VALUES LESS THAN MAXVALUE
);


call pr_insertdate_1('2016-01-01','2016-12-31','members_mon');


select 
  partition_name part,  
  partition_expression expr,  
  partition_description descr,  
  table_rows  
from information_schema.partitions  where 
  table_schema = schema()  
  and table_name='members_mon';

+------+----------+--------------+------------+
| part | expr     | descr        | table_rows |
+------+----------+--------------+------------+
| p0   | `joined` | '2016-02-01' |         31 |
| p1   | `joined` | '2016-03-01' |         29 |
| p2   | `joined` | '2016-04-01' |         31 |
| p3   | `joined` | '2016-05-01' |         30 |
| p4   | `joined` | '2016-06-01' |         31 |
| p5   | `joined` | '2016-07-01' |         30 |
| p6   | `joined` | '2016-08-01' |         31 |
| p7   | `joined` | '2016-09-01' |         31 |
| p8   | `joined` | '2016-10-01' |         30 |
| p9   | `joined` | '2016-11-01' |         31 |
| p10  | `joined` | '2016-12-01' |         30 |
| p11  | `joined` | '2017-01-01' |         30 |
| p12  | `joined` | MAXVALUE     |          0 |
+------+----------+--------------+------------+
13 rows in set (0.00 sec)

mysql> explain partitions select * from members_mon where joined='2016-02-01';
+----+-------------+-------------+------------+------+---------------+------+---------+------+------+-------------+
| id | select_type | table       | partitions | type | possible_keys | key  | key_len | ref  | rows | Extra       |
+----+-------------+-------------+------------+------+---------------+------+---------+------+------+-------------+
|  1 | SIMPLE      | members_mon | p1         | ALL  | NULL          | NULL | NULL    | NULL |   29 | Using where |
+----+-------------+-------------+------------+------+---------------+------+---------+------+------+-------------+

RANGE Culumns分区还支持多列分区

create table rc3(
    a int,
    b int
)
partition by range columns(a,b)(
    partition p01 values less than (0,10),
    partition p02 values less than (10,10),
    partition p03 values less than (10,20),
    partition p04 values less than (10,35),
    partition p05 values less than (10,MAXVALUE),
    partition p06 values less than (MAXVALUE,MAXVALUE)
);
需要注意的是,RANGE Culumns分区键的比较是基于元组的比较,也就是基于字段组的比较,这和之前RANGE分区键的比较有些差异,我们写入几条测试数据并观察测试数据的分区情况来看一看

写入a=1,b=10的记录,实际写入了p02分区,也即是说元组(1,10)<(10,10)

mysql> insert into rc3 values(1,10);
Query OK, 1 row affected (0.00 sec)

mysql> select 
    ->   partition_name part,  
    ->   partition_expression expr,  
    ->   partition_description descr,  
    ->   table_rows  
    -> from information_schema.partitions  where 
    ->   table_schema = schema()  
    ->   and table_name='rc3';
+------+---------+-------------------+------------+
| part | expr    | descr             | table_rows |
+------+---------+-------------------+------------+
| p01  | `a`,`b` | 0,10              |          0 |
| p02  | `a`,`b` | 10,10             |          1 |
| p03  | `a`,`b` | 10,20             |          0 |
| p04  | `a`,`b` | 10,35             |          0 |
| p05  | `a`,`b` | 10,MAXVALUE       |          0 |
| p06  | `a`,`b` | MAXVALUE,MAXVALUE |          0 |
+------+---------+-------------------+------------+
6 rows in set (0.00 sec)

mysql> select (9,34)<(10,10);
+----------------+
| (9,34)<(10,10) |
+----------------+
|              1 |
+----------------+
1 row in set (0.00 sec)
所以insert into rc3 values(9,34);将被插入p02

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')
);

date datetime类型
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')
);
不过使用LIST COLUMNS分区进行按时间分区并不太好,还是RANGE COLUMNS好些
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')
);

你可能感兴趣的:(MySQL)