mysql partition(mysql range partition,对历史数据建分区)

对于分区的类型,以及新建表时就写好分区的话,按照例子做就好了。
这边文章主要是为了处理历史数据,也就是表里已经有海量的数据,比如两年的,一天的数据就是G+的,像这样的话,我们必须及早处理这些没有提前分区的表。
按照range 分区,主要是按照月和天来分区。
e.g.1:
(1)
CREATE TABLE sale_mast (bill_no INT NOT NULL, bill_date date NOT NULL, cust_code VARCHAR(15) NOT NULL, amount DECIMAL(8,2) NOT NULL);
(2)
INSERT INTO sale_mast VALUES (1, '2013-01-02', 'C001', 125.56), (2, '2013-01-25', 'C003', 456.50), (3, '2013-02-15', 'C012', 365.00), (4, '2013-03-26', 'C345', 785.00), (5, '2013-04-19', 'C234', 656.00), (6, '2013-05-31', 'C743', 854.00), (7, '2013-06-11', 'C234', 542.00), (8, '2013-07-24', 'C003', 300.00), (8, '2013-08-02', 'C456', 475.20);
注意:这个表sale_mast是已经有数据的表,称为历史数据;
查看分区前表的信息:
mysql> select partition_name,table_rows from information_schema.partitions where table_name='sale_mast';+----------------+------------+
| partition_name | table_rows |
+----------------+------------+
| NULL | 9 |
+----------------+------------+
1 row in set (0.00 sec)
注意:range 分区的 values必须是int类型.
第一种分区:然后对sale_mast 进行按月分区:
ALTER TABLE sale_mast PARTITION BY RANGE (year(bill_date)*100+month(bill_date)) ( PARTITION p0 VALUES LESS THAN ((201304)), PARTITION p1 VALUES LESS THAN ((201307)), PARTITION p2 VALUES LESS THAN ((201310)), PARTITION p3 VALUES LESS THAN ((201401)));
查看分区后的信息:
mysql> select partition_name,table_rows from information_schema.partitions where table_name='sale_mast';+----------------+------------+
| partition_name | table_rows |
+----------------+------------+
| p0 | 4 |
| p1 | 3 |
| p2 | 2 |
| p3 | 0 |
+----------------+------------+
4 rows in set (0.01 sec)
第二种分区:然后对sale_mast 进行按天分区:
删除表,重新新建表和inset into后,进行按天分区:
ALTER TABLE sale_mast PARTITION BY RANGE (to_days(bill_date)) ( PARTITION p0 VALUES LESS THAN (to_days('2013-04-01')), PARTITION p1 VALUES LESS THAN (to_days('2013-07-01')), PARTITION p2 VALUES LESS THAN (to_days('2013-10-01')), PARTITION p3 VALUES LESS THAN (to_days('2014-01-01')));
mysql> select partition_name,table_rows from information_schema.partitions where table_name='sale_mast';+----------------+------------+
| partition_name | table_rows |
+----------------+------------+
| p0 | 4 |
| p1 | 3 |
| p2 | 2 |
| p3 | 0 |
+----------------+------------+
4 rows in set (0.00 sec)。
还可以建立子分区,这个下次再更新。
 
posted @ 2014-09-15 18:00  度娘818  阅读(840)  评论(0编辑  收藏  举报