MySQL集锦2-分区

1、分区类型RANGE,LIST,HASH
CREATE TABLE t2 (  
  dt date DEFAULT NULL 
) ENGINE=MyISAM DEFAULT CHARSET=utf8 
PARTITION BY RANGE (TO_DAYS(dt))  
(PARTITION p01 VALUES LESS THAN (733042) ENGINE = MyISAM,  
 PARTITION p02 VALUES LESS THAN (733407) ENGINE = MyISAM,  
 PARTITION p03 VALUES LESS THAN (733773) ENGINE = MyISAM,  
 PARTITION p04 VALUES LESS THAN MAXVALUE ENGINE = MyISAM);

show create table t2;


MySQL集锦2-分区


2、RANGE分区
建立表
create table t1(id int) ENGINE=MyISAM DEFAULT CHARSET=utf8 


插入id从1~10000的1w条数据

\d // 

create procedure p1()
begin
set @i=1;
while @i<=10000 do
insert into t1 values(@i);
set @i=@i+1;
end while;
end //

\d ;
show procedure status;



create table part_tab(c1 int,c2 varchar(30),c3 date) ENGINE=MyISAM DEFAULT CHARSET=utf8 
PARTITION BY RANGE (year(c3)) (
PARTITION p0 values less than(1995),
PARTITION p1 values less than(1996),
PARTITION p2 values less than(1997),
PARTITION p3 values less than(1998),
PARTITION p4 values less than(1999),
PARTITION p5 values less than(2000),
PARTITION p6 values less than(2001),
PARTITION p7 values less than(2002),
PARTITION p8 values less than(2003),
PARTITION p9 values less than(2004),
PARTITION p10 values less than(2005),
PARTITION p11 values less than MAXVALUE
);


由于是按时间来做分区的,所以需要写个存储做测试
\d // 

create procedure load_part_tab()
begin
declare v int default 0;
while v<=8000000 do
insert into part_tab values(v,'testing_partition',adddate('1995-01-01',(rand(v)*36520) mod 3652));
set v=v+1;
end while;
end //

\d ;
show procedure status;

cd /var/lib/mysql/test
watch -n1 ls -lh #查看变化情况


创建没有分区的表no_part_tab做对比
create table no_part_tab(c1 int,c2 varchar(30),c3 date) ENGINE=MyISAM DEFAULT CHARSET=utf8 ;

insert into no_part_tab select * from part_tab;

select count(*) from part_tab where c3 > date '1995-01-01' and c3 < date '1995-12-31';
select count(*) from no_part_tab where c3 > date '1995-01-01' and c3 < date '1995-12-31';


MySQL集锦2-分区

你可能感兴趣的:(mysql)