mysql 分区实验

一、试验一
    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';
           
        结果:分区表比未分区表的执行时间少很多
        分析:
           
    6、创建索引后,比较性能
            create index idx_of_c3 on no_part_tab(c3);
            create index idx_of_c3 on part_tab(c3);
           
    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';
           
  
四、innodb表数据结构
       
 
       
五、innodb表怎么分区
        只有把innodb设置成独立表空间后,才能创建成功innodb表引擎的表分区
posted @ 2016-09-02 10:03  rhythm0121  阅读(243)  评论(0编辑  收藏  举报