Mysql 섹션 기능 구현 (1)

1. 파티션 테이블 만들기
CREATE TABLE `  ` (   
    `EQUIPMENTID` char(17) NOT NULL,   
    `ATTRIBUTEID` char(4) NOT NULL,   
    `VALUE` varchar(20) NOT NULL,   
    `COLLECTTIME` datetime NOT NULL  
)    
ENGINE=InnoDB (       ,       )   
DEFAULT CHARSET=gbk(         )    
PARTITION BY RANGE (to_days(COLLECTTIME))    
(PARTITION pmin VALUES LESS THAN (to_days('2010-01-01')),   
PARTITION p201001 VALUES LESS THAN (to_days('2010-02-01')) ,    
PARTITION p201002 VALUES LESS THAN (to_days('2010-03-01')) ,   
PARTITION p201003 VALUES LESS THAN (to_days('2010-04-01')) ,    
PARTITION p201004 VALUES LESS THAN (to_days('2010-05-01')) ,   
PARTITION p201005 VALUES LESS THAN (to_days('2010-06-01')) ,    
PARTITION p201006 VALUES LESS THAN (to_days('2010-07-01')) ,   
PARTITION p201007 VALUES LESS THAN (to_days('2010-08-01')) ,    
PARTITION p201008 VALUES LESS THAN (to_days('2010-09-01')) ,   
PARTITION p201009 VALUES LESS THAN (to_days('2010-10-01')) ,    
PARTITION p201010 VALUES LESS THAN (to_days('2010-11-01')),   
PARTITION p201011 VALUES LESS THAN (to_days('2010-12-01')),   
PARTITION p201012 VALUES LESS THAN (to_days('2011-01-01')),   
PARTITION p201101 VALUES LESS THAN (to_days('2011-02-01')),   
PARTITION p201102 VALUES LESS THAN (to_days('2011-03-01')),   
PARTITION p201103 VALUES LESS THAN (to_days('2011-04-01')),   
PARTITION p201104 VALUES LESS THAN (to_days('2011-05-01')),   
PARTITION p201105 VALUES LESS THAN (to_days('2011-06-01')),   
PARTITION p201106 VALUES LESS THAN (to_days('2011-07-01')),   
PARTITION p201107 VALUES LESS THAN (to_days('2011-08-01')),   
PARTITION p201108 VALUES LESS THAN (to_days('2011-09-01')),   
PARTITION p201109 VALUES LESS THAN (to_days('2011-10-01')),   
PARTITION p201110 VALUES LESS THAN (to_days('2011-11-01')),   
PARTITION p201111 VALUES LESS THAN (to_days('2011-12-01')),   
PARTITION p201112 VALUES LESS THAN (to_days('2012-01-01')),   
PARTITION pmax VALUES LESS THAN MAXVALUE );   

2. 기존 테이블의 구역 만들기
alter table      
PARTITION BY RANGE (to_days(COLLECTTIME))    
(PARTITION pmin VALUES LESS THAN (to_days('2010-01-01')),   
PARTITION p201001 VALUES LESS THAN (to_days('2010-02-01')) ,    
PARTITION p201002 VALUES LESS THAN (to_days('2010-03-01')) ,   
PARTITION p201003 VALUES LESS THAN (to_days('2010-04-01')) ,    
PARTITION p201004 VALUES LESS THAN (to_days('2010-05-01')) ,   
PARTITION p201005 VALUES LESS THAN (to_days('2010-06-01')) ,    
PARTITION p201006 VALUES LESS THAN (to_days('2010-07-01')) ,   
PARTITION p201007 VALUES LESS THAN (to_days('2010-08-01')) ,    
PARTITION p201008 VALUES LESS THAN (to_days('2010-09-01')) ,   
PARTITION p201009 VALUES LESS THAN (to_days('2010-10-01')) ,    
PARTITION p201010 VALUES LESS THAN (to_days('2010-11-01')),   
PARTITION p201011 VALUES LESS THAN (to_days('2010-12-01')),   
PARTITION p201012 VALUES LESS THAN (to_days('2011-01-01')),   
PARTITION p201101 VALUES LESS THAN (to_days('2011-02-01')),   
PARTITION p201102 VALUES LESS THAN (to_days('2011-03-01')),   
PARTITION p201103 VALUES LESS THAN (to_days('2011-04-01')),   
PARTITION p201104 VALUES LESS THAN (to_days('2011-05-01')),   
PARTITION p201105 VALUES LESS THAN (to_days('2011-06-01')),   
PARTITION p201106 VALUES LESS THAN (to_days('2011-07-01')),   
PARTITION p201107 VALUES LESS THAN (to_days('2011-08-01')),   
PARTITION p201108 VALUES LESS THAN (to_days('2011-09-01')),   
PARTITION p201109 VALUES LESS THAN (to_days('2011-10-01')),   
PARTITION p201110 VALUES LESS THAN (to_days('2011-11-01')),   
PARTITION p201111 VALUES LESS THAN (to_days('2011-12-01')),   
PARTITION p201112 VALUES LESS THAN (to_days('2012-01-01')),   
PARTITION pmax VALUES LESS THAN MAXVALUE );   

만약 표에 이미 데이터가 있다면, 구역을 구분할 때 자동으로 구역 저장을 할 것이니, 데이터가 분실되거나 수동으로 분류되는 것을 걱정할 필요가 없다.
3. 테이블에서 지정된 파티션 삭제
ALTER TABLE    DROP PARTITION    ;

4. 테이블 분할 영역 추가
ALTER TABLE    DROP PARTITION pmax;   
ALTER TABLE      
ADD PARTITION (   
PARTITION p201201 VALUES LESS THAN (to_days('2012-2-1')),   
PARTITION pmax VALUES LESS THAN MAXVALUE);   

5. 테이블 파티션 정보 보기
SELECT    
        partition_name part,     
        partition_expression expr,     
        partition_description descr,     
        table_rows     
FROM    
        INFORMATION_SCHEMA.partitions     
WHERE    
				TABLE_NAME='  '; 

6. 조회문이 구역 정보와 관련된 것을 보기
explain partitions   
select … from    where …;

좋은 웹페이지 즐겨찾기