一、试验一
1、在test数据库中创建t2表,并向表中插入千万条数据,insert into t2 select * from t2;
2、新开一个终端,进入/usr/local/mysql/data/test 下执行:watch -n1 ls -lh 每一秒刷新一次
3、free -m 查看内存的使用
4、top 查看cpu
二、实验二
1、create table t3(id int);
2、插入10000条数据,创建存储过程:
mysql> \d //
mysql> create procedure p3()
-> begin
-> set @i=1;
-> while @i<=10000 do
-> insert into t3 values(@i);
-> set @i=@i+1;
-> end while;
-> end //
3、查看存储状态
mysql> show procedure status\G;
查看存储语句:
show create procedure p3;
4、呼叫p3:call p3();
三、创建分区表,按日期的年份拆分
1、创建part_tab表:
mysql> create table part_tab (c1 int default NULL,c2 varchar(30) default NULL,c3 date default NULL)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 (2010),
-> PARTITION p11 VALUES LESS THAN MAXVALUE);
create table part_tab (c1 int default NULL,c2 varchar(30) default NULL,c3 date default NULL)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 (2010), PARTITION p11 VALUES LESS THAN MAXVALUE);
2、创建no_part_tab表
create table no_part_tab (c1 int default NULL,c2 varchar(30) default NULL,c3 date default NULL)engine=MyISAM default charset=utf8;
3、创建load_part_tab存储
通过存储过程灌入800万条测试数据
mysql> create procedure load_part_tab()
-> begin
-> declar v int default 0;
-> while v<8000000
-> 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 //
create procedure load_part_tab() begin declare v int default 0; while v<8000000 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//
4、insert into no_part_tab select * from part_tab;
5、测试sql性能
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';
![](https://i-blog.csdnimg.cn/blog_migrate/e41be105fce4c6a2f315f038ec9443e2.png)
结果:分区表比未分区表的执行时间少很多
分析:
![](https://i-blog.csdnimg.cn/blog_migrate/e247e1a7101f651cf5f1d7680d48d2b0.png)
6、创建索引后,比较性能
create index idx_of_c3 on no_part_tab(c3);
create index idx_of_c3 on part_tab(c3);
![](https://i-blog.csdnimg.cn/blog_migrate/f4be0cfcec15c5fe023246d7f463e968.png)
7、增加未索引字段查询
select count(*) from part_tab where c3>date '1995-01-01' and c3 < date '1996-12-31' and c2='hello';
select count(*) from no_part_tab where c3>date '1995-01-01' and c3 < date '1996-12-31' and c2='hello';
![](https://i-blog.csdnimg.cn/blog_migrate/4b0c39512169c82742d357543b392112.png)
四、innodb表数据结构
![](https://i-blog.csdnimg.cn/blog_migrate/9806c1ea9662ddb989c48a93a906b668.png)
![](https://i-blog.csdnimg.cn/blog_migrate/de7fddbd90f53355cbc1a283638c7ece.png)
五、innodb表怎么分区
只有把innodb设置成独立表空间后,才能创建成功innodb表引擎的表分区