1. 程式人生 > >Mysql --分割槽(3)range分割槽

Mysql --分割槽(3)range分割槽

3.分割槽型別

RANGE分割槽

按照range分割槽的表是利用取值範圍將資料分成分割槽,區間要連續並且不能互相重疊,使用values less than操作符進行分割槽定義

CREATE TABLE tnp (
    id INT NOT NULL AUTO_INCREMENT,
    name VARCHAR(255),
    PRIMARY KEY pk (id)
)
partition by range (id) (
    partition p0 values less than(10),
    partition p1 values less than(20),
    partition p2 values
less than(30) );

當插入ID大於30的值是,會出現錯誤
可以在設定分割槽時使用values less than maxvalue子句

alter table tnp add partition(partition p3 values less than maxvalue);

MySQL支援在values less than子句中使用表示式,比如,以日期作為range分割槽的分割槽列:

CREATE TABLE emp_date (
    id INT NOT NULL auto_increment,
    name VARCHAR(255),
    hiredate date
not null default '1970-01-01', key (id) ) partition by range (year(hiredate)) ( partition p0 values less than(1995), partition p1 values less than(2000), partition p2 values less than(2005) );

MySQL也支援對TIMESTAMP列進行range分割槽

CREATE TABLE quarterly_report_status (
    report_id INT NOT NULL,
    report_status VARCHAR(20
) NOT NULL, report_updated TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP ) PARTITION BY RANGE ( UNIX_TIMESTAMP(report_updated) ) ( PARTITION p0 VALUES LESS THAN ( UNIX_TIMESTAMP('2008-01-01 00:00:00') ), PARTITION p1 VALUES LESS THAN ( UNIX_TIMESTAMP('2008-04-01 00:00:00') ), PARTITION p2 VALUES LESS THAN ( UNIX_TIMESTAMP('2008-07-01 00:00:00') ), PARTITION p3 VALUES LESS THAN ( UNIX_TIMESTAMP('2008-10-01 00:00:00') ), PARTITION p4 VALUES LESS THAN ( UNIX_TIMESTAMP('2009-01-01 00:00:00') ), PARTITION p5 VALUES LESS THAN ( UNIX_TIMESTAMP('2009-04-01 00:00:00') ), PARTITION p6 VALUES LESS THAN ( UNIX_TIMESTAMP('2009-07-01 00:00:00') ), PARTITION p7 VALUES LESS THAN ( UNIX_TIMESTAMP('2009-10-01 00:00:00') ), PARTITION p8 VALUES LESS THAN ( UNIX_TIMESTAMP('2010-01-01 00:00:00') ), PARTITION p9 VALUES LESS THAN (MAXVALUE) ); 實際是是將TMESTAMP型別通過UNIX_TIMESTAMP函式轉換成INT型 mysql> select UNIX_TIMESTAMP('2008-01-01 00:00:00'); +---------------------------------------+ | UNIX_TIMESTAMP('2008-01-01 00:00:00') | +---------------------------------------+ | 1199116800 | +---------------------------------------+ 1 row in set (0.00 sec) delimiter $$ drop procedure if exists pr_insertdate_2$$ create procedure pr_insertdate_2(in begindate date,in enddate date,in tabname varchar(40)) begin set begindate = timestamp(begindate); while begindate<enddate do set @s=concat_ws(' ','insert into',tabname,'values(1,''true'',''',begindate,''')'); prepare stmt from @s; execute stmt; drop prepare stmt; set begindate = date_add(begindate,interval 1 day); end while; end$$ delimiter ; mysql> call pr_insertdate_2('2007-01-01','2010-12-31','quarterly_report_status'); Query OK, 0 rows affected (0.32 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='quarterly_report_status'; +------+---------------------------------+------------+------------+ | part | expr | descr | table_rows | +------+---------------------------------+------------+------------+ | p0 | UNIX_TIMESTAMP(report_updated) | 1199116800 | 365 | | p1 | UNIX_TIMESTAMP(report_updated) | 1206979200 | 91 | | p2 | UNIX_TIMESTAMP(report_updated) | 1214841600 | 91 | | p3 | UNIX_TIMESTAMP(report_updated) | 1222790400 | 92 | | p4 | UNIX_TIMESTAMP(report_updated) | 1230739200 | 92 | | p5 | UNIX_TIMESTAMP(report_updated) | 1238515200 | 90 | | p6 | UNIX_TIMESTAMP(report_updated) | 1246377600 | 91 | | p7 | UNIX_TIMESTAMP(report_updated) | 1254326400 | 92 | | p8 | UNIX_TIMESTAMP(report_updated) | 1262275200 | 92 | | p9 | UNIX_TIMESTAMP(report_updated) | MAXVALUE | 364 | +------+---------------------------------+------------+------------+ 10 rows in set (0.00 sec)

涉及TIMESTAMP值的任何其他表示式不允許 (See Bug #42849.)

當一個或多個下列條件為真時,範圍分割槽是特別有用的:
1.需要解除安裝舊資料
此時你就可以使用 ALTER TABLE employees DROP PARTITION p0;解除安裝舊資料,效率要比delete高很多

2.你想用一列包含日期或時間值,或包含從其他系列所產生的價值

You want to use a column containing date or time values, or containing values arising from some other series

3.where條件列經常為分割槽列。這個不用多說了,利用分割槽裁剪特性