mysql Partition(分区)初探_MySQL

php中文网
发布: 2016-06-01 13:36:29
原创
1094人浏览过

bitsCN.com

mysql partition(分区)初探

 

表数据量大的时候一般都考虑水平拆分,即所谓的sharding.不过mysql本身具有分区功能,可以实现一定程度 的水平切分. 

mysql是具有MERGE这种引擎的,就是把一些结构相同的MyIASM表作为一个表使用,但是我觉得 MERGE不如partition实用, 

因为MERGE会在所有的底层表上查询,而partition只在相应的分区上查询. 

建立了两个表,分别为分区和未分区的,分区表按年进行分区. 

Sql代码  

CREATE TABLE `20130117date_par` (  

  `content` varchar(20) NOT NULL,  

  `create_time` datetime NOT NULL,  

  KEY `20130117date_idx_date` (`create_time`)  

) ENGINE=InnoDB DEFAULT CHARSET=utf8  

PARTITION BY RANGE (YEAR(create_time))  

(PARTITION p2009 VALUES LESS THAN (2010),  

 PARTITION p2010 VALUES LESS THAN (2011),  

 PARTITION p2011 VALUES LESS THAN (2012),  

 PARTITION p2012 VALUES LESS THAN (2013),  

 PARTITION p2013 VALUES LESS THAN (2014))  

  

CREATE TABLE `20130117date` (  

  `content` varchar(20) NOT NULL,  

  `create_time` datetime NOT NULL,  

  KEY `20130117date_idx_date` (`create_time`)  

) ENGINE=InnoDB  

 

用sp向分区表和普通表各插入了90w条随机数据. 

用mysqlslap进行下测试 

 

不用分区表 

Sql代码  

select SQL_NO_CACHE * from 20130117date  

where create_time BETWEEN '2013-01-01' and '2013-01-02';  

select SQL_NO_CACHE * from 20130117date  

where create_time BETWEEN '2012-12-25' and '2013-01-05';  

 

引用

 

Benchmark 

        Average number of seconds to run all queries: 0.881 seconds 

        Minimum number of seconds to run all queries: 0.062 seconds 

        Maximum number of seconds to run all queries: 3.844 seconds 

        Number of clients running queries: 1 

        Average number of queries per client: 2 

Benchmark 

        Average number of seconds to run all queries: 0.703 seconds 

        Minimum number of seconds to run all queries: 0.062 seconds 

        Maximum number of seconds to run all queries: 1.922 seconds 

        Number of clients running queries: 1 

        Average number of queries per client: 2 

Benchmark 

        Average number of seconds to run all queries: 1.250 seconds 

        Minimum number of seconds to run all queries: 0.109 seconds 

        Maximum number of seconds to run all queries: 4.032 seconds 

citySHOP多用户商城系统
citySHOP多用户商城系统

citySHOP是一款集CMS、网店、商品、分类信息、论坛等为一体的城市多用户商城系统,已完美整合目前流行的Discuz! 6.0论坛,采用最新的5.0版PHP+MYSQL技术。面向对象的数据库连接机制,缓存及80%静态化处理,使它能最大程度减轻服务器负担,为您节约建设成本。多级店铺区分及联盟商户地图标注,实体店与虚拟完美结合。个性化的店铺系统,会员后台一体化管理。后台登陆初始网站密匙:LOVES

citySHOP多用户商城系统 0
查看详情 citySHOP多用户商城系统

        Number of clients running queries: 1 

        Average number of queries per client: 2 

 

 

用分区表 

Sql代码  

select SQL_NO_CACHE * from 20130117date_par  

where create_time BETWEEN '2013-01-01' and '2013-01-02';  

select SQL_NO_CACHE * from 20130117date_par  

where create_time BETWEEN '2012-12-25' and '2013-01-05';  

 

引用

 

Benchmark 

        Average number of seconds to run all queries: 0.068 seconds 

        Minimum number of seconds to run all queries: 0.047 seconds 

        Maximum number of seconds to run all queries: 0.110 seconds 

        Number of clients running queries: 1 

        Average number of queries per client: 2 

Benchmark 

        Average number of seconds to run all queries: 0.250 seconds 

        Minimum number of seconds to run all queries: 0.031 seconds 

        Maximum number of seconds to run all queries: 1.078 seconds 

        Number of clients running queries: 1 

        Average number of queries per client: 2 

Benchmark 

        Average number of seconds to run all queries: 0.046 seconds 

        Minimum number of seconds to run all queries: 0.046 seconds 

        Maximum number of seconds to run all queries: 0.047 seconds 

        Number of clients running queries: 1 

        Average number of queries per client: 2 

           

看来性能还是有一定的提升的. 

       

执行 

Sql代码  

explain PARTITIONS select * from 20130117date_par  

where create_time BETWEEN '2012-01-01' and '2012-01-02';  

 

可以看出这个query只扫描了p2012这个分区. 

而且分区表的好处在于维护比较方便.比如2009年的数据不需要了,分区表的方法为 

Sql代码  

alter table 20130117date_par drop PARTITION p2009  

 

不到1s就行了 

普通表为 

Sql代码  

delete from 20130117date  

where create_time BETWEEN '2009-01-01' and '2010-01-01'  

 

用了10.25s左右

bitsCN.com
相关标签:
最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号