[关闭]
@frank-shaw 2016-12-14T17:33:54.000000Z 字数 1167 阅读 1518

分区简单使用

DataAtlas


创建分区:

  1. CREATE TABLE IF NOT EXISTS `partition_test` (
  2. `stat_date` date NOT NULL,
  3. `user_name` VARCHAR(255)
  4. )
  5. PARTITION BY RANGE ( TO_DAYS(`stat_date`)) (
  6. PARTITION P_20161213 VALUES LESS THAN (TO_DAYS('20161213')),
  7. PARTITION P_20161214 VALUES LESS THAN (TO_DAYS('20161214')),
  8. PARTITION P_20161215 VALUES LESS THAN (TO_DAYS('20161215')),
  9. PARTITION P_20161216 VALUES LESS THAN (TO_DAYS('20161216')),
  10. PARTITION P_20161217 VALUES LESS THAN (TO_DAYS('20161217')),
  11. PARTITION P_MAX VALUES LESS THAN (MAXVALUE)
  12. );

可以以日期间隔来区分。
插入相关数据:

  1. INSERT INTO partition_test VALUES
  2. ('2016-12-13', 'haha'),
  3. ('2016-12-14', 'haha'),
  4. ('2016-12-15', 'haha'),
  5. ('2016-12-16', 'haha'),
  6. ('2016-12-17', 'haha');

使用方式说明

1.删除分区并且删除数据(值得注意):

  1. ALTER TABLE partition_test DROP PARTITION P_20161214;

2.仅仅删除分区而不删除数据(假设删除20161214):

  1. ALTER TABLE partition_test REORGANIZE PARTITION P_20161214, P_20161215 INTO(
  2. PARTITION P_20161215 VALUES LESS THAN (TO_DAYS('20161215'))
  3. );

3.添加分区方式1(如果采用前面的RANGE的方式,只能是往高处添加):

  1. ALTER TABLE partition_test ADD PARTITION (
  2. PARTITION P_20161217 VALUES LESS THAN (TO_DAYS('20161217'))
  3. );

4.添加分区方式2(假设插入20161214):

  1. ALTER TABLE partition_test REORGANIZE PARTITION P_20161215 INTO(
  2. PARTITION P_20161214 VALUES LESS THAN (TO_DAYS('20161214')),
  3. PARTITION P_20161215 VALUES LESS THAN (TO_DAYS('20161215'))
  4. );
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注