mysql單表分區
源 https://blog.csdn.net/apple001100/article/details/75451999
目的:為了了解mysql單表分區方法,特此作為學習筆記記錄一下。
一。準備表,創建一個學生表,包含主鍵sid和名稱sname字段
create table students(
sid int(5) primary key,
sname varchar(24)
);
二。準備數據
insert into students(sid,sname) values(10003,‘tom‘);
insert into students(sid,sname) values(10005,‘jerry‘);
insert into students(sid,sname) values(10006,‘hengte‘);
insert into students(sid,sname) values(10000,‘tom1‘);
insert into students(sid,sname) values(10001,‘jerry2‘);
insert into students(sid,sname) values(10002,‘hengte3‘);
insert into students(sid,sname) values(10004,‘weilian4‘);
三。查詢結果
select * from sutdents
四。建立分區,按照主鍵ID的值進行設定分區規則如下:
alter table students partition by range(sid)
(
partition p0 values less than (10001),
partition p1 values less than (10003),
partition p2 values less than (10005),
partition p3 values less than maxvalue
);
五。查詢結果,可以看到查詢結果分布到不同的分區裏
select * from students partition (p0);
select * from students partition (p1);
select * from students partition (p2);
select * from students partition (p3);
六。驗證新插入數據
insert into students(sid,sname) values(10011,‘tom12‘);
insert into students(sid,sname) values(10012,‘jerry13‘);
insert into students(sid,sname) values(10013,‘hengte14‘);
insert into students(sid,sname) values(10014,‘weilian15‘);
insert into students(sid,sname) values(10015,‘tom116‘);
insert into students(sid,sname) values(10016,‘jerry22‘);
insert into students(sid,sname) values(10017,‘hengte32‘);
insert into students(sid,sname) values(10018,‘weilian42‘);
七。再次查詢分區數據,會看到新插入的數據按照分區規則劃分到對應的分區裏了
select * from students partition (p0);
select * from students partition (p1);
select * from students partition (p2);
select * from students partition (p3);
mysql單表分區