mysql分区表创建思路

mysql分区表有range,list,hash,key,其中每个分区又有特殊的类型。对于RANGE分区,有RANGE COLUMNS分区。对于LIST分区,有LIST COLUMNS分区。对于HASH分区,有LINEAR HASH分区。对于KEY分区,有LINEAR KEY分区。具体想知道是什么看下面链接

https://www.cnblogs.com/ivictor/p/5033708.html

 

 

二话不话,下面是直接处理分区表的操作。

1、直接使用语句增加分区表,这个操作会自动将数据弄到新的分区表,然后删除原表。如果数据量很大,很占服务器资源(??其实我真没有测试过,不知道到底是不是的)

ALTER TABLE tbl_rtdata PARTITION BY RANGE (Month(fld_date))
( 
PARTITION p_Apr VALUES LESS THAN (TO_DAYS('2012-05-01')),
PARTITION p_May VALUES LESS THAN (TO_DAYS('2012-06-01')), 
PARTITION p_Dec VALUES LESS THAN MAXVALUE );

  

2、新建一个跟原来的表一样的结果,将新的表进行分区,然后将原来的表导出,将原来的表改名为另一上名,将新的表改为原来的表的名字,然后导入数据

CREATE TABLE `apdailysts_p` (
  `id` INT(11) NOT NULL AUTO_INCREMENT,
  `ap_id` INT(11) NOT NULL,
  `mac` VARCHAR(17) NOT NULL,
  `liveinfo` LONGTEXT NOT NULL,
  `livetime` INT(11) NOT NULL,
  `stsdate` DATE NOT NULL,
  `lastmodified` DATETIME NOT NULL,
   PRIMARY KEY (`id`, `stsdate`)
) 
PARTITION BY RANGE COLUMNS(stsdate) (
    PARTITION p0 VALUES LESS THAN ('2016-06-01'),
    PARTITION p1 VALUES LESS THAN ('2016-07-01'),
    PARTITION p2 VALUES LESS THAN ('2016-08-01'),
    PARTITION p3 VALUES LESS THAN ('2016-09-01'),
    PARTITION p4 VALUES LESS THAN ('2016-10-01'),
    PARTITION p5 VALUES LESS THAN ('2016-11-01'),
    PARTITION p6 VALUES LESS THAN ('2016-12-01'),
    PARTITION p7 VALUES LESS THAN ('2017-01-01'),
    PARTITION p8 VALUES LESS THAN ('2017-02-01'),
    PARTITION p9 VALUES LESS THAN ('2017-03-01'),
    PARTITION p10 VALUES LESS THAN ('2017-05-01'),
    PARTITION p11 VALUES LESS THAN ('2017-06-01'),
    PARTITION p12 VALUES LESS THAN ('2017-07-01'),
    PARTITION p13 VALUES LESS THAN ('2017-08-01'),
    PARTITION p14 VALUES LESS THAN ('2017-09-01'),
    PARTITION p15 VALUES LESS THAN MAXVALUE
);

  导出数据

mysqldump -u dbname -p --no-create-info dbname apdailysts  > apdailysts.sql

  导入数据

mysql-u dbname -p  dbname < apdailysts.sql

  

不知道为毛,我1G的数据导入N分钟都没导完。应该可加些参数。等我找到答案再来更新

 

posted @ 2020-08-21 14:47  你好啊,再见了  阅读(1838)  评论(0编辑  收藏  举报