mysql分区
前提条件
mysql版本 5.1以上
查看是否装载分区插件
show plugins;
+------------+--------+----------------+---------+---------+
| Name | Status | Type | Library | License |
+------------+--------+----------------+---------+---------+
| binlog | ACTIVE | STORAGE ENGINE | NULL | GPL |
| partition | ACTIVE | STORAGE ENGINE | NULL | GPL |
| CSV | ACTIVE | STORAGE ENGINE | NULL | GPL |
| MEMORY | ACTIVE | STORAGE ENGINE | NULL | GPL |
| InnoDB | ACTIVE | STORAGE ENGINE | NULL | GPL |
| MyISAM | ACTIVE | STORAGE ENGINE | NULL | GPL |
| MRG_MYISAM | ACTIVE | STORAGE ENGINE | NULL | GPL |
+------------+--------+----------------+---------+---------+
有partition就行
按照区域分区range**************
这种分区时,分区字段要是递增的一种顺序排列,这个字段可以间隔,但不能重复,否则按分区查询的时候只会显示一条,类似groupby
创建分区表
create table demo(id int,name char(20))
partition by range(id)(
partition p0 values less than(1000),
partition p1 values less than(2000),
partition p2 values less than maxvalue
);
d $
创建存储过程
create procedure insert_data()
begin
declare i int default 5000;
while i>0 do
insert into demo(id,name) values (i,left(md5(i),5));
set i=i-1;
end while;
end
$
create procedure insert_da()
begin
declare i int default 5000;
while i>0 do
insert into testhash(id,name) values (i,left(md5(i),5));
set i=i-5;
end while;
end
$
call insert_data()$
查询数据看走什么分区
explain partitions select * from demo where id=100G
清空表
truncate demo;
create table demo(id int,name char(20))
partitions by range(id)(
partition p1 values less than(100),
partition p2 values less than(200),
partition p3 values less than maxvalue
);
list分组
这个就是通用于归类的情况了 分类cid之类的
create table test_list(id int,name char(20))
partition by list(id)(
partition p1 values in(1,3,5,7,9),
partition p2 values in(2,4,6,8,0)
);
循环插入数据
create procedure insert_list()
begin
declare i int default 100;
declare y int default 1;
while i>0 do
set y = floor(rand()*10);
insert into test_list(id,name) values(y,left(md5(i),5));
set i=i-1;
end while;
end
$
hash分组
用户自定义分组, 如果分3组的话 第一个添加到第一组 第二个到第二组 第三个到第三组 第四个到第一组...以此类推
create table testhash(id int,name char(20))
partition by hash (id)
partitions 10;
还有个KEY 分组 和hash分组很类似
create table test(id int,name varchar(100),addtime date)engine myisam charset utf8
partition BY KEY (addtime)
PARTITIONS 10;