数据准备
1.1 创建分区表 part_tab
create table part_tab(
c1 int DEFAULT null,
c2 varchar(30) DEFAULT null,
c3 date default null
) engine = myisam
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 LESSTHAN (2010),
PARTITION p11 VALUES LESS THAN MAXVALUE
)
1.2 创建非分区表 no_part_tab
创建非分区表
create table no_part_tab
(
c1 int(11) default NULL,
c2 varchar(30) default NULL,
c3 date default NULL
) engine=myisam
1.3 利用存储过程,向分区表 part_tab 中插入 800万条数据
delimiter //
CREATE PROCEDURE load_part_tab()
begin
declare v int default 0;
while v
do
insert into part_tab
values (v,'testing partitions',adddate('1995-01-01',(rand(v)*36520) mod 3652));
set v = v + 1;
end while;
end
//
delimiter ;
调用 load_part_tab()
call load_part_tab();
1.4 向非分区表中插入同样的数据
insert into no_part_tab select * from part_tab
比较
2.1 查询有分区的表
select count(*) from part_tab where c3 > date '1995-01-01' and c3
效果,用时0.209s:
2.2 查询无分区的表:
select count(*) from no_part_tab where c3 > date '1995-01-01' and c3
效果,用时1.806s:
分析
对比很明显,如果where 条件与分区匹配的话,分区表的效果是极好的。