mysql分割槽表簡單例子

2021-07-16 14:21:45 字數 2768 閱讀 9363

mysql分表可以有三種辦法實現:

1.直接物理表實現分割槽 (主要由程式控制);

2.通過merge儲存引擎

3.通過分割槽字段進行分割槽

當然也可以通過上面3種方式進行自由組合

下面的例子說的是第三種-分割槽表:

a--建立分割槽表

create table test_log

(id bigint(20) not null auto_increment,

name varchar(20) null,

gmt_create datetime not null,

primary key (id,gmt_create),

key idx_name (name)

)engine=innodb default charset=utf8

partition by range(to_days(gmt_create))

(partition p20141115 values less than (to_days('2014-11-15')),

partition p20141130 values less than (to_days('2014-11-30')),

partition p20141215 values less than (to_days('2014-12-15')),

partition p20141231 values less than (to_days('2014-12-31')),

partition p20150115 values less than (to_days('2015-01-15')),

partition p20150131 values less than (to_days('2015-01-31')),

partition p20150215 values less than (to_days('2015-02-15')),

partition p20150228 values less than (to_days('2015-02-28')),

partition p20150315 values less than (to_days('2015-03-15')),

partition p20150331 values less than (to_days('2015-03-31')),

partition p20150415 values less than (to_days('2015-04-15')),

partition p20150430 values less than (to_days('2015-04-30'))

);b--新增分割槽

alter table test_log add partition (partition p20150515 values less than (to_days('2015-05-15')));

alter table test_log add partition (partition p20150531 values less than (to_days('2015-05-31')));

alter table test_log add partition (partition p20150615 values less than (to_days('2015-06-15')));

alter table test_log add partition (partition p20150630 values less than (to_days('2015-06-30')));

alter table test_log add partition (partition p20150715 values less than (to_days('2015-07-15')));

alter table test_log add partition (partition p20150731 values less than (to_days('2015-07-31')));

alter table test_log add partition (partition p20150815 values less than (to_days('2015-08-15')));

alter table test_log add partition (partition p20150831 values less than (to_days('2015-08-31')));

alter table test_log add partition (partition p20150915 values less than (to_days('2015-09-15')));

alter table test_log add partition (partition p20150930 values less than (to_days('2015-09-30')));

alter table test_log add partition (partition p20151015 values less than (to_days('2015-10-15')));

c--刪除分割槽

alter table test_log drop partition p20150515;

d--插入資料

insert into test_log

select null,'jack',now()

e-- 指定分割槽查詢

select * from test_log partition(p20150930);

mysql分割槽表簡單例子

mysql分表可以有三種辦法實現 1.直接物理表實現分割槽 主要由程式控制 2.通過merge儲存引擎 3.通過分割槽字段進行分割槽 當然也可以通過上面3種方式進行自由組合 下面的例子說的是第三種 分割槽表 a 建立分割槽表 create table test log id bigint 20 no...

MySQL分割槽表

分割槽表是一種粗粒度,簡易的索引策略,適用於大資料的過濾場景.最適合的場景是,沒有合適的索引時,對其中幾個分割槽表進行全表掃瞄.或者只有乙個分割槽表和索引是熱點,而且這個分割槽和索引能夠全部儲存在記憶體中.限制單錶分割槽數不要超過150個,並且注意某些導致無法做分割槽過濾的細節,分割槽表對於單條記錄...

mysql分割槽表

對使用者來說,分割槽表是乙個獨立的邏輯表,但是底層由多個物理子表組成。實現分割槽的 實際上是對一組底層表的控制代碼物件的封裝。mysql在建立表時使用partition by子句定義每個分割槽存放的資料。在執行查詢的時候,優化器會根據分割槽定義過濾那些沒有我們需要資料的分割槽,這樣查詢就無須掃瞄所有...