mysql的分區

來源:互聯網
上載者:User

標籤:mysql分區

查看mysql的資料分割函數是否啟用:

mysql> show variables like ‘%part%‘;+-------------------+-------+| Variable_name     | Value |+-------------------+-------+| have_partitioning | YES   |+-------------------+-------+1 row in set (0.00 sec)

建立一個分區表:

CREATE TABLE `test100` (   `xuhao` int(11) NOT NULL,   `xuhao2` int(11) NOT NULL,) ENGINE=innodb DEFAULT CHARSET=utf8 PARTITION BY RANGE (xuhao) (PARTITION p201303 VALUES LESS THAN (unix_timestamp(‘2013-04-01‘)),PARTITION p201304 VALUES LESS THAN (unix_timestamp(‘2013-05-01‘)),PARTITION p201305 VALUES LESS THAN (unix_timestamp(‘2013-06-01‘)),PARTITION p201306 VALUES LESS THAN (unix_timestamp(‘2013-07-01‘)),PARTITION p201307 VALUES LESS THAN (unix_timestamp(‘2013-08-01‘)),PARTITION p201308 VALUES LESS THAN (unix_timestamp(‘2013-09-01‘)),PARTITION p201309 VALUES LESS THAN (unix_timestamp(‘2013-10-01‘)),PARTITION p201310 VALUES LESS THAN (unix_timestamp(‘2013-11-01‘)),PARTITION p201311 VALUES LESS THAN (unix_timestamp(‘2013-12-01‘)),PARTITION p201312 VALUES LESS THAN (unix_timestamp(‘2014-01-01‘)),PARTITION p201401 VALUES LESS THAN (unix_timestamp(‘2014-02-01‘)));

添加分區:

alter table xxxxxxx add partition (partition p0 values less than(1991));  //只能添加大於分區鍵的分區

刪除分區:

alter table xxx drop partition p0; //可以刪除任意分區

刪除分區資料

alter table xxxxxx  truncate partition p1,p2;alter table xxxxxx  truncate partition all;或delete from xxxxxx where separated < ‘2006-01-01‘ or (separated >= ‘2006-01-01‘ and separated<‘2011-01-01‘);

重定義分區(包括重新命名分區,伴隨移動資料;合并分區)

alter table xxxxx reorganize partition p1,p3,p4 into (partition pm1 values less than(2006),partition pm2 values less than(2011));

  
rebuild重建分區

alter  table xxxxxx rebuild partition pm1/all; //相當於drop所有記錄,然後再reinsert;可以解決磁碟片段

最佳化表

alter  table tt2 optimize partition pm1; //在大量delete表資料後,可以回收空間和磁碟重組。但在5.5.30後支援。在5.5.30之前可以通過recreate+analyze來替代,如果用rebuild+analyze速度慢

analzye表

alter  table xxxxxx analyze partition pm1/all;

check表

alter  table xxxxxx check partition pm1/all;
show create table employees2;  //查看分區表的定義show table status like ‘employees2‘\G;    //查看錶時候是分區表 如“Create_options: partitioned”select * from information_schema.KEY_COLUMN_USAGE where table_name=‘employees2‘;   //查看索引SELECT * FROM information_schema.partitions WHERE table_name=‘employees2‘   //查看分區表explain partitions select * from employees2 where separated < ‘1990-01-01‘ or separated > ‘2


本文出自 “王家東哥” 部落格,謝絕轉載!

mysql的分區

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.