转 http://blog.csdn.net/m582445672/article/details/7800694
1.创建一个测试表
- CREATE TABLE test (
- id VARCHAR(20) NOT NULL,
- name VARCHAR(20) NOT NULL,
- submit_time DATETIME NOT NULL,
- index time_index (submit_time),
- index id_index (id)
- )ENGINE=MyISAM
- PARTITION BY RANGE COLUMNS(submit_time)
- (
- PARTITION p1 VALUES LESS THAN ('2010-02-01'),
- PARTITION p2 VALUES LESS THAN ('2010-03-01'),
- PARTITION p3 VALUES LESS THAN ('2010-04-01'),
- PARTITION p4 VALUES LESS THAN ('2010-05-01'),
- PARTITION p5 VALUES LESS THAN ('2010-06-01'),
- PARTITION p6 VALUES LESS THAN ('2010-07-01'),
- PARTITION p7 VALUES LESS THAN ('2010-08-01'),
- PARTITION p8 VALUES LESS THAN ('2010-09-01'),
- PARTITION p9 VALUES LESS THAN ('2010-10-01'),
- PARTITION p10 VALUES LESS THAN ('2010-11-01'),
- PARTITION p11 VALUES LESS THAN ('2010-12-01')
- );
CREATE TABLE test (
id VARCHAR(20) NOT NULL,
name VARCHAR(20) NOT NULL,
submit_time DATETIME NOT NULL,
index time_index (submit_time),
index id_index (id)
)ENGINE=MyISAM
PARTITION BY RANGE COLUMNS(submit_time)
(
PARTITION p1 VALUES LESS THAN ('2010-02-01'),
PARTITION p2 VALUES LESS THAN ('2010-03-01'),
PARTITION p3 VALUES LESS THAN ('2010-04-01'),
PARTITION p4 VALUES LESS THAN ('2010-05-01'),
PARTITION p5 VALUES LESS THAN ('2010-06-01'),
PARTITION p6 VALUES LESS THAN ('2010-07-01'),
PARTITION p7 VALUES LESS THAN ('2010-08-01'),
PARTITION p8 VALUES LESS THAN ('2010-09-01'),
PARTITION p9 VALUES LESS THAN ('2010-10-01'),
PARTITION p10 VALUES LESS THAN ('2010-11-01'),
PARTITION p11 VALUES LESS THAN ('2010-12-01')
);
2.写一个存储过程,插入数据
- delimiter //
- CREATE PROCEDURE mark_test()
- begin
- declare v int default 0;
- while v < 8000
- do
- insert into test values (v,'testing partitions',adddate('2010-01-01', INTERVAL v hour));
- set v = v + 1;
- end while;
- end //
- delimiter ;
delimiter //
CREATE PROCEDURE mark_test()
begin
declare v int default 0;
while v < 8000
do
insert into test values (v,'testing partitions',adddate('2010-01-01', INTERVAL v hour));
set v = v + 1;
end while;
end //
delimiter ;
3.实验开始
上面可以看到,这个是查某一个分区里面的某一些内容,所以完全可以用到index.效果很好..
上面可以看到,跨分区查询,效果也非常不错.
上面可以到看,跨分区查询是,如果某个分区没有用到索引(p4就是全表扫描),整个也没有用到index.但好的是,只扫描需要的分区
上面可以看到,如果你不用分区的字段查询,是很杯具的,因为MySQL不知道你分区的index是分别存放到哪个分区上,所以要全index扫描,
3.顺便看看表结构
a. 图中test3 是innodb的存储引擎,
test3.frm是表结构.
test3.par是分区表的信息.
数据和索引都是存放在表空间里面在
b.图中test是myisam的存储引擎,
test.frm是表结构,
test.par是分区表的信息.
test#P#p10.MYD是数据文件之一,
test#P#p10.MYI是索引文件之一