Mysql實現分割槽功能(一)

2021-12-30 12:46:08 字數 4709 閱讀 5195

1. 建立分割槽表

create table `表名` (

`equipmentid` char(17) not null,

`attributeid` char(4) not null,

`value` varchar(20) not null,

`collecttime` datetime not null

)

engine=innodb (適用大部分引擎,可根據需要調整)

default charset=gbk(編碼可根據需要修改)

partition by range (to_days(collecttime))

(partition pmin values less than (to_days('2010-01-01')),

partition p201001 values less than (to_days('2010-02-01')) ,

partition p201002 values less than (to_days('2010-03-01')) ,

partition p201003 values less than (to_days('2010-04-01')) ,

partition p201004 values less than (to_days('2010-05-01')) ,

partition p201005 values less than (to_days('2010-06-01')) ,

partition p201006 values less than (to_days('2010-07-01')) ,

partition p201007 values less than (to_days('2010-08-01')) ,

partition p201008 values less than (to_days('2010-09-01')) ,

partition p201009 values less than (to_days('2010-10-01')) ,

partition p201010 values less than (to_days('2010-11-01')),

partition p201011 values less than (to_days('2010-12-01')),

partition p201012 values less than (to_days('2011-01-01')),

partition p201101 values less than (to_days('2011-02-01')),

partition p201102 values less than (to_days('2011-03-01')),

partition p201103 values less than (to_days('2011-04-01')),

partition p201104 values less than (to_days('2011-05-01')),

partition p201105 values less than (to_days('2011-06-01')),

partition p201106 values less than (to_days('2011-07-01')),

partition p201107 values less than (to_days('2011-08-01')),

partition p201108 values less than (to_days('2011-09-01')),

partition p201109 values less than (to_days('2011-10-01')),

partition p201110 values less than (to_days('2011-11-01')),

partition p201111 values less than (to_days('2011-12-01')),

partition p201112 values less than (to_days('2012-01-01')),

partition pmax values less than maxvalue );2. 為現有表建立分割槽alter table 表名

partition by range (to_days(collecttime))

(partition pmin values less than (to_days('2010-01-01')),

partition p201001 values less than (to_days('2010-02-01')) ,

partition p201002 values less than (to_days('2010-03-01')) ,

partition p201003 values less than (to_days('2010-04-01')) ,

partition p201004 values less than (to_days('2010-05-01')) ,

partition p201005 values less than (to_days('2010-06-01')) ,

partition p201006 values less than (to_days('2010-07-01')) ,

partition p201007 values less than (to_days('2010-08-01')) ,

partition p201008 values less than (to_days('2010-09-01')) ,

partition p201009 values less than (to_days('2010-10-01')) ,

partition p201010 values less than (to_days('2010-11-01')),

partition p201011 values less than (to_days('2010-12-01')),

partition p201012 values less than (to_days('2011-01-01')),

partition p201101 values less than (to_days('2011-02-01')),

partition p201102 values less than (to_days('2011-03-01')),

partition p201103 values less than (to_days('2011-04-01')),

partition p201104 values less than (to_days('2011-05-01')),

partition p201105 values less than (to_days('2011-06-01')),

partition p201106 values less than (to_days('2011-07-01')),

partition p201107 values less than (to_days('2011-08-01')),

partition p201108 values less than (to_days('2011-09-01')),

partition p201109 values less than (to_days('2011-10-01')),

partition p201110 values less than (to_days('2011-11-01')),

partition p201111 values less than (to_days('2011-12-01')),

partition p201112 values less than (to_days('2012-01-01')),

partition pmax values less than maxvalue );如果表中已有資料,分割槽時會自動進行分割槽儲存,不必擔心資料丟失或者手動分類資料. 

3. 刪除表中的指定分割槽

alter table 表名 drop partition 分割槽名;4. 追加表分割槽alter table 表名 drop partition pmax;

alter table 表名

add partition (

partition p201201 values less than (to_days('2012-2-1')),

partition pmax values less than maxvalue);5. 檢視表分割槽資訊select

partition_name part,

partition_expression expr,

partition_description descr,

table_rows

from

information_schema.partitions

where

table_name='表名';6. 檢視查詢語句涉及分割槽資訊explain partitions

select … from 表名 where …;

Mysql表分割槽實現

一 分割槽的限制 1.主鍵或者唯一索引必須包含分割槽字段,如primary key id,username 不過innodb的大組建效能不好。2.很多時候,使用分割槽就不要在使用主鍵了,否則可能影響效能。3.只能通過int型別的字段或者返回int型別的表示式來分割槽,通常使用year或者to day...

Mysql表分割槽實現

一 分割槽的限制 1.主鍵或者唯一索引必須包含分割槽字段,如primary key id,username 不過innodb的大組建效能不好。2.很多時候,使用分割槽就不要在使用主鍵了,否則可能影響效能。3.只能通過int型別的字段或者返回int型別的表示式來分割槽,通常使用year或者to day...

mysql 子分割槽的寫法 MySQL子分割槽一例

這是現實中的乙個例子,但是我不推薦使用mysql分割槽用於生產create table t log id int 11 not null auto increment comment 主鍵id verson int 11 not null default 0 comment 版本號 actions ...