热门标签 | HotTags
当前位置:  开发笔记 > 后端 > 正文

MySQL分区之RANGE分区

MySQL分区之RANGE分区环境:www.2cto.com[sql]mysql>selectversion()\G;***************************1.row***************************version():5.5.28㈠主要应用场景...

MySQL分区之RANGE分区
 
环境:  www.2cto.com  
[sql] 
mysql> select version()\G;  
*************************** 1. row ***************************  
version(): 5.5.28  
 
         ㈠ 主要应用场景
         
         RANGE分区主要用于日期列的分区
         例如销售类的表,可以根据年份来分区存储销售记录
         如下是对sales表进行分区
[sql] 
mysql> create table sales(money int unsigned not null,  
    -> date datetime  
    -> )engine=innodb  
    -> partition by range (year(date)) (  
    -> partition p2008 values less than (2009),  
    -> partition p2009 values less than (2010),  
    -> partition p2010 values less than (2011)  
    -> );  
Query OK, 0 rows affected (0.06 sec)  
  
mysql> insert into sales SELECT 100,'2008-01-01';  
Query OK, 1 row affected (0.02 sec)  
Records: 1  Duplicates: 0  Warnings: 0  
  
mysql> insert into sales SELECT 100,'2008-02-01';  
Query OK, 1 row affected (0.00 sec)  
Records: 1  Duplicates: 0  Warnings: 0  
  
mysql> insert into sales SELECT 200,'2008-01-02';  
Query OK, 1 row affected (0.00 sec)  
Records: 1  Duplicates: 0  Warnings: 0  
  
mysql> insert into sales SELECT 100,'2008-03-01';  
Query OK, 1 row affected (0.01 sec)  
Records: 1  Duplicates: 0  Warnings: 0  
  
mysql> insert into sales SELECT 100,'2009-03-01';  
Query OK, 1 row affected (0.00 sec)  
Records: 1  Duplicates: 0  Warnings: 0  
  
mysql> insert into sales SELECT 200,'2010-03-01';  
Query OK, 1 row affected (0.00 sec)  
Records: 1  Duplicates: 0  Warnings: 0  
  
mysql> select * from sales;  
+-------+---------------------+  
| money | date                |  
+-------+---------------------+  
|   100 | 2008-01-01 00:00:00 |  
|   100 | 2008-02-01 00:00:00 |  
|   200 | 2008-01-02 00:00:00 |  
|   100 | 2008-03-01 00:00:00 |  
|   100 | 2009-03-01 00:00:00 |  
|   200 | 2010-03-01 00:00:00 |  
+-------+---------------------+  
6 rows in set (0.00 sec)  
 
          ① 便于对sales表管理,如果要删除2008年的数据,我们就不需要执行:
             delete from sales where date>= &#39;2008-01-01&#39; and date<&#39;2009-01-01&#39;
             而只需删除2008年数据所在的分区即可
[sql] 
mysql> alter table sales drop partition p2008;  
Query OK, 0 rows affected (0.10 sec)  
Records: 0  Duplicates: 0  Warnings: 0  
  
mysql> select * from sales;  
+-------+---------------------+  
| money | date                |  
+-------+---------------------+  
|   100 | 2009-03-01 00:00:00 |  
|   200 | 2010-03-01 00:00:00 |  
+-------+---------------------+  
2 rows in set (0.00 sec)  
 
          ② 另一个好处是加快某些查询操作,例如,我们只需要查询2009年整年的销售额
[sql] 
mysql> explain partitions  
    -> select * from sales  
    -> where date>=&#39;2009-01-01&#39; and date<=&#39;2009-12-31&#39;\G;  
*************************** 1. row ***************************  
           id: 1  
  select_type: SIMPLE  
        table: sales  
   partitions: p2009  
         type: ALL  
possible_keys: NULL  
          key: NULL  
      key_len: NULL  
          ref: NULL  
         rows: 4  
        Extra: Using where  
1 row in set (0.00 sec)  
 
          SQL优化器会进行分区修剪,即只搜索p2009
          也请注意分区的边界,如date<&#39;2010-01-01&#39;,那么优化器会连带搜索p2010分区
          
          ㈡ 常见相关问题
          
          ① 插入了一个不在分区中定义的值
[sql] 
mysql> insert into sales select 200,&#39;2012-12-3&#39;;  
ERROR 1526 (HY000): Table has no partition for value 2012  
mysql> show create table sales \G;  
*************************** 1. row ***************************  
       Table: sales  
Create Table: CREATE TABLE `sales` (  
  `money` int(10) unsigned NOT NULL,  
  `date` datetime DEFAULT NULL  
) ENGINE=InnoDB DEFAULT CHARSET=latin1  
/*!50100 PARTITION BY RANGE (year(date))  
(PARTITION p2009 VALUES LESS THAN (2010) ENGINE = InnoDB,  
 PARTITION p2010 VALUES LESS THAN (2011) ENGINE = InnoDB) */  
1 row in set (0.00 sec)  
  
ERROR:   
No query specified  
  
mysql> alter table sales add partition(  
    -> partition p2012 values less than maxvalue);  
Query OK, 0 rows affected (0.04 sec)  
Records: 0  Duplicates: 0  Warnings: 0  
  
mysql> insert into sales select 200,&#39;2012-12-3&#39;;  
Query OK, 1 row affected (0.01 sec)  
Records: 1  Duplicates: 0  Warnings: 0  
  
mysql> select * from sales where date=&#39;2012-12-3&#39;;  
+-------+---------------------+  
| money | date                |  
+-------+---------------------+  
|   200 | 2012-12-03 00:00:00 |  
+-------+---------------------+  
1 row in set (0.00 sec)  
 
          ② 对RANGE分区的查询,优化器只能对year(),to_days(),to_seconds()和unix_timestamp()这类函数进行优化选择
[sql] 
mysql> create table t (date datetime)  
    -> engine=innodb  
    -> partition by range (year(date)*100+month(date)) (  
    -> partition p201201 values less than (201202),  
    -> partition p201202 values less than (201203),  
    -> partition p201203 values less than (201204)  
    -> );  
Query OK, 0 rows affected (0.02 sec)  
  
mysql> insert into t select &#39;2012-01-01&#39;;  
Query OK, 1 row affected (0.00 sec)  
Records: 1  Duplicates: 0  Warnings: 0  
  
mysql> insert into t select &#39;2012-01-06&#39;;  
Query OK, 1 row affected (0.00 sec)  
Records: 1  Duplicates: 0  Warnings: 0  
  
mysql> insert into t select &#39;2012-02-06&#39;;  
Query OK, 1 row affected (0.01 sec)  
Records: 1  Duplicates: 0  Warnings: 0  
  
mysql> insert into t select &#39;2012-01-06&#39;;  
Query OK, 1 row affected (0.00 sec)  
Records: 1  Duplicates: 0  Warnings: 0  
  
mysql> insert into t select &#39;2012-03-06&#39;;  
Query OK, 1 row affected (0.00 sec)  
Records: 1  Duplicates: 0  Warnings: 0  
  
mysql> insert into t select &#39;2012-02-01&#39;;  
Query OK, 1 row affected (0.01 sec)  
Records: 1  Duplicates: 0  Warnings: 0  
  
mysql> select * from t;  
+---------------------+  
| date                |  
+---------------------+  
| 2012-01-01 00:00:00 |  
| 2012-01-06 00:00:00 |  
| 2012-01-06 00:00:00 |  
| 2012-02-06 00:00:00 |  
| 2012-02-01 00:00:00 |  
| 2012-03-06 00:00:00 |  
+---------------------+  
6 rows in set (0.00 sec)  
  
mysql> explain partitions  
    -> select * from t  
    -> where date>=&#39;2012-01-01&#39; and date<=&#39;2012-01-31&#39;\G;  
*************************** 1. row ***************************  
           id: 1  
  select_type: SIMPLE  
        table: t  
   partitions: p201201,p201202,p201203  
         type: ALL  
possible_keys: NULL  
          key: NULL  
      key_len: NULL  
          ref: NULL  
         rows: 6  
        Extra: Using where  
1 row in set (0.00 sec)  
  
ERROR:   
No query specified  
  
mysql> drop table t;  
Query OK, 0 rows affected (0.01 sec)  
mysql> create table t (date datetime)  
    -> engine=innodb  
    -> partition by range (to_days(date)) (  
    -> partition p201201 values less than (to_days(&#39;2012-02-01&#39;)),  
    -> partition p201201 values less than (to_days(&#39;2012-03-01&#39;)),  
    -> partition p201201 values less than (to_days(&#39;2012-04-01&#39;))  
    -> );  
mysql> insert into t select &#39;2012-01-02&#39;;  
Query OK, 1 row affected (0.00 sec)  
Records: 1  Duplicates: 0  Warnings: 0  
  
mysql> insert into t select &#39;2012-01-03&#39;;  
Query OK, 1 row affected (0.00 sec)  
Records: 1  Duplicates: 0  Warnings: 0  
  
mysql> insert into t select &#39;2012-01-08&#39;;  
Query OK, 1 row affected (0.01 sec)  
Records: 1  Duplicates: 0  Warnings: 0  
  
mysql> insert into t select &#39;2012-02-08&#39;;  
Query OK, 1 row affected (0.00 sec)  
Records: 1  Duplicates: 0  Warnings: 0  
  
mysql> insert into t select &#39;2012-03-08&#39;;  
Query OK, 1 row affected (0.00 sec)  
Records: 1  Duplicates: 0  Warnings: 0  
  
mysql> select * from t;  
+---------------------+  
| date                |  
+---------------------+  
| 2012-01-02 00:00:00 |  
| 2012-01-03 00:00:00 |  
| 2012-01-08 00:00:00 |  
| 2012-02-08 00:00:00 |  
| 2012-03-08 00:00:00 |  
+---------------------+  
5 rows in set (0.00 sec)  
  
mysql> explain partitions  
    -> select * from t  
    -> where date>=&#39;2012-01-01&#39; and date<=&#39;2012-01-31&#39;\G;  
*************************** 1. row ***************************  
           id: 1  
  select_type: SIMPLE  
        table: t  
   partitions: p1  
         type: ALL  
possible_keys: NULL  
          key: NULL  
      key_len: NULL  
          ref: NULL  
         rows: 3  
        Extra: Using where  
1 row in set (0.00 sec)  
 

推荐阅读
  • 推荐一个ASP的内容管理框架(ASP Nuke)的优势和适用场景
    本文推荐了一个ASP的内容管理框架ASP Nuke,并介绍了其主要功能和特点。ASP Nuke支持文章新闻管理、投票、论坛等主要内容,并可以自定义模块。最新版本为0.8,虽然目前仍处于Alpha状态,但作者表示会继续更新完善。文章还分析了使用ASP的原因,包括ASP相对较小、易于部署和较简单等优势,适用于建立门户、网站的组织和小公司等场景。 ... [详细]
  • 本文介绍了在开发Android新闻App时,搭建本地服务器的步骤。通过使用XAMPP软件,可以一键式搭建起开发环境,包括Apache、MySQL、PHP、PERL。在本地服务器上新建数据库和表,并设置相应的属性。最后,给出了创建new表的SQL语句。这个教程适合初学者参考。 ... [详细]
  • 本文介绍了如何在MySQL中将零值替换为先前的非零值的方法,包括使用内联查询和更新查询。同时还提供了选择正确值的方法。 ... [详细]
  • 在数据分析工作中,我们通常会遇到这样的问题,一个业务部门由若干业务组构成,需要筛选出每个业务组里业绩前N名的业务员。这其实是一个分组排序的 ... [详细]
  • 本文介绍了如何使用php限制数据库插入的条数并显示每次插入数据库之间的数据数目,以及避免重复提交的方法。同时还介绍了如何限制某一个数据库用户的并发连接数,以及设置数据库的连接数和连接超时时间的方法。最后提供了一些关于浏览器在线用户数和数据库连接数量比例的参考值。 ... [详细]
  • Oracle Database 10g许可授予信息及高级功能详解
    本文介绍了Oracle Database 10g许可授予信息及其中的高级功能,包括数据库优化数据包、SQL访问指导、SQL优化指导、SQL优化集和重组对象。同时提供了详细说明,指导用户在Oracle Database 10g中如何使用这些功能。 ... [详细]
  • 在说Hibernate映射前,我们先来了解下对象关系映射ORM。ORM的实现思想就是将关系数据库中表的数据映射成对象,以对象的形式展现。这样开发人员就可以把对数据库的操作转化为对 ... [详细]
  • 本文详细介绍了MysqlDump和mysqldump进行全库备份的相关知识,包括备份命令的使用方法、my.cnf配置文件的设置、binlog日志的位置指定、增量恢复的方式以及适用于innodb引擎和myisam引擎的备份方法。对于需要进行数据库备份的用户来说,本文提供了一些有价值的参考内容。 ... [详细]
  • 本文由编程笔记小编整理,介绍了PHP中的MySQL函数库及其常用函数,包括mysql_connect、mysql_error、mysql_select_db、mysql_query、mysql_affected_row、mysql_close等。希望对读者有一定的参考价值。 ... [详细]
  • Spring特性实现接口多类的动态调用详解
    本文详细介绍了如何使用Spring特性实现接口多类的动态调用。通过对Spring IoC容器的基础类BeanFactory和ApplicationContext的介绍,以及getBeansOfType方法的应用,解决了在实际工作中遇到的接口及多个实现类的问题。同时,文章还提到了SPI使用的不便之处,并介绍了借助ApplicationContext实现需求的方法。阅读本文,你将了解到Spring特性的实现原理和实际应用方式。 ... [详细]
  • 本文详细介绍了SQL日志收缩的方法,包括截断日志和删除不需要的旧日志记录。通过备份日志和使用DBCC SHRINKFILE命令可以实现日志的收缩。同时,还介绍了截断日志的原理和注意事项,包括不能截断事务日志的活动部分和MinLSN的确定方法。通过本文的方法,可以有效减小逻辑日志的大小,提高数据库的性能。 ... [详细]
  • 本文介绍了adg架构设置在企业数据治理中的应用。随着信息技术的发展,企业IT系统的快速发展使得数据成为企业业务增长的新动力,但同时也带来了数据冗余、数据难发现、效率低下、资源消耗等问题。本文讨论了企业面临的几类尖锐问题,并提出了解决方案,包括确保库表结构与系统测试版本一致、避免数据冗余、快速定位问题等。此外,本文还探讨了adg架构在大版本升级、上云服务和微服务治理方面的应用。通过本文的介绍,读者可以了解到adg架构设置的重要性及其在企业数据治理中的应用。 ... [详细]
  • 本文介绍了使用postman进行接口测试的方法,以测试用户管理模块为例。首先需要下载并安装postman,然后创建基本的请求并填写用户名密码进行登录测试。接下来可以进行用户查询和新增的测试。在新增时,可以进行异常测试,包括用户名超长和输入特殊字符的情况。通过测试发现后台没有对参数长度和特殊字符进行检查和过滤。 ... [详细]
  • 使用Ubuntu中的Python获取浏览器历史记录原文: ... [详细]
  • 本文介绍了Oracle数据库中tnsnames.ora文件的作用和配置方法。tnsnames.ora文件在数据库启动过程中会被读取,用于解析LOCAL_LISTENER,并且与侦听无关。文章还提供了配置LOCAL_LISTENER和1522端口的示例,并展示了listener.ora文件的内容。 ... [详细]
author-avatar
多米音乐_34084632
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有