首页 > 数据库技术 > 详细

MySQL分区实验

时间:2014-04-21 08:00:21      阅读:454      评论:0      收藏:0      [点我收藏+]

环境 CentOS5.5,内存256MB,MySQL5.5.16

一、创建测试表

bubuko.com,布布扣
CREATE TABLE part_tab(
    c1 INT DEFAULT NULL,
    c2 VARCHAR(30) DEFAULT NULL,
    c3 DATE DEFAULT NULL
) ENGINE=myisam 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)
bubuko.com,布布扣
bubuko.com,布布扣
CREATE TABLE `no_part_tab` (
  `c1` int(11) DEFAULT NULL,
  `c2` varchar(30) DEFAULT NULL,
  `c3` date DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
bubuko.com,布布扣

二、创建测试数据

bubuko.com,布布扣
CREATE PROCEDURE `p1`()
begin
    declare v int default 0;
    while v<8000000 do
    insert into part_tab values (v,testing partition,adddate(1995-01-01,(rand(v)*36520) mod 3652));
    set v=v+1;
    end while;
end
bubuko.com,布布扣

  call p1;

  insert into no_part_tab select * from part_tab

三、测试

  select count(*) from part_tab where c3>date ‘1995-01-01‘ and c3< date ‘1995-12-31‘;  #1.875s

  select count(*) from no_part_tab where c3>date ‘1995-01-01‘ and c3< date ‘1995-12-31‘;  #9.269s

四、分析

  desc select count(*) from part_tab where c3>date ‘1995-01-01‘ and c3< date ‘1995-12-31‘ \G;

  bubuko.com,布布扣

  desc select count(*) from no_part_tab where c3>date ‘1995-01-01‘ and c3< date ‘1995-12-31‘ \G;

  bubuko.com,布布扣

  可以看出慢的原因了

五、创建索引再测试

  create index idx_of_c3 on part_tab(c3);

  create index idx_of_c3 on no_part_tab(c3);

  select count(*) from no_part_tab where c3>date ‘1995-01-01‘ and c3< date ‘1995-12-31‘;  #1.49s

  select count(*) from part_tab where c3>date ‘1995-01-01‘ and c3< date ‘1995-12-31‘;    #1.48s

  查询时间差不多,分析图就想想就知道样子了

六、使用未索引字段查询

  select count(*) from no_part_tab where c3>date ‘1995-01-01‘ and c3< date ‘1996-12-31‘ and c2=‘hello‘;  #12.274s

  select count(*) from part_tab where c3>date ‘1995-01-01‘ and c3< date ‘1996-12-31‘ and c2=‘hello‘;    #2.74s

七、分析

  bubuko.com,布布扣

  bubuko.com,布布扣

八、总结

  在数据量较大的情况 使用被作为查询条件较频繁的字段 作为分区的依据,今天才知道\d 可以代替delimiter 。

MySQL分区实验,布布扣,bubuko.com

MySQL分区实验

原文:http://www.cnblogs.com/hwt987/p/3677237.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!