mysql> CREATE  TABLE t1  ( id INT, date DATETIME DEFAULT CURRENT_TIMESTAMP) ENGINE=Innodb;
Query OK, rows affected mysql> insert into t1 values(, '2013-05-23 12:59:39');
Query OK, row affected mysql> insert into t1 values(, '2013-05-23 12:59:43');
insert into t1 values(, '2013-05-23 12:59:44');
insert into t1 values(, '2013-07-04 19:35:45');
insert into t1 values(, '2014-04-04 19:35:45' );
insert into t1 values(, '2014-05-04 19:35:45' );
insert into t1 values(, '2015-05-04 19:35:45');
insert into t1 values(, '2015-05-05 19:35:45');
insert into t1 values(, '2017-05-05 19:35:45');
insert into t1 values(,'2018-05-05 19:35:45' );
Query OK, row affected Query OK, row affected Query OK, row affected Query OK, row affected Query OK, row affected Query OK, row affected Query OK, row affected Query OK, row affected Query OK, row affected mysql> select * from t1;
+----+---------------------+
| id | date |
+----+---------------------+
| | -- :: |
| | -- :: |
| | -- :: |
| | -- :: |
| | -- :: |
| | -- :: |
| | -- :: |
| | -- :: |
| | -- :: |
| | -- :: |
+----+---------------------+
rows in set mysql> explain select * from t1;
+----+-------------+-------+------+---------------+------+---------+------+------+-------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+------+---------------+------+---------+------+------+-------+
| | SIMPLE | t1 | ALL | NULL | NULL | NULL | NULL | | NULL |
+----+-------------+-------+------+---------------+------+---------+------+------+-------+
row in set mysql> EXPLAIN SELECT * FROM t1 WHERE date >= '2014-03-05 19:00:12' AND date <= '2016-03-05 18:45:12';
+----+-------------+-------+------+---------------+------+---------+------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+------+---------------+------+---------+------+------+-------------+
| | SIMPLE | t1 | ALL | NULL | NULL | NULL | NULL | | Using where |
+----+-------------+-------+------+---------------+------+---------+------+------+-------------+
row in set mysql> CREATE TABLE t2 ( id INT, date DATETIME DEFAULT CURRENT_TIMESTAMP) ENGINE=Innodb
PARTITION BY RANGE (YEAR(date)) (
PARTITION p2013 VALUES LESS THAN(),
PARTITION p2014 VALUES LESS THAN(),
PARTITION p2015 VALUES LESS THAN(),
PARTITION p2016 VALUES LESS THAN(),
PARTITION p2017 VALUES LESS THAN(),
PARTITION p2099 VALUES LESS THAN MAXVALUE
) ;
Query OK, rows affected mysql> show create table t2;
+-------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| t2 | CREATE TABLE `t2` (
`id` int() DEFAULT NULL,
`date` datetime DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8
/*!50100 PARTITION BY RANGE (YEAR(date))
(PARTITION p2013 VALUES LESS THAN (2014) ENGINE = InnoDB,
PARTITION p2014 VALUES LESS THAN (2015) ENGINE = InnoDB,
PARTITION p2015 VALUES LESS THAN (2016) ENGINE = InnoDB,
PARTITION p2016 VALUES LESS THAN (2017) ENGINE = InnoDB,
PARTITION p2017 VALUES LESS THAN (2018) ENGINE = InnoDB,
PARTITION p2099 VALUES LESS THAN MAXVALUE ENGINE = InnoDB) */ |
+-------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
row in set mysql> SELECT table_name,partition_name,table_rows FROM information_schema.PARTITIONS WHERE table_schema=database() AND table_name='t2';
+------------+----------------+------------+
| table_name | partition_name | table_rows |
+------------+----------------+------------+
| t2 | p2013 | |
| t2 | p2014 | |
| t2 | p2015 | |
| t2 | p2016 | |
| t2 | p2017 | |
| t2 | p2099 | |
+------------+----------------+------------+
rows in set mysql> EXPLAIN SELECT * FROM t2;
+----+-------------+-------+------+---------------+------+---------+------+------+-------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+------+---------------+------+---------+------+------+-------+
| | SIMPLE | t2 | ALL | NULL | NULL | NULL | NULL | | NULL |
+----+-------------+-------+------+---------------+------+---------+------+------+-------+
row in set mysql> insert into t2 select * from t1;
Query OK, rows affected
Records: Duplicates: Warnings: mysql> explain select * from t2;
+----+-------------+-------+------+---------------+------+---------+------+------+-------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+------+---------------+------+---------+------+------+-------+
| | SIMPLE | t2 | ALL | NULL | NULL | NULL | NULL | | NULL |
+----+-------------+-------+------+---------------+------+---------+------+------+-------+
row in set mysql> select * from t2;
+----+---------------------+
| id | date |
+----+---------------------+
| | -- :: |
| | -- :: |
| | -- :: |
| | -- :: |
| | -- :: |
| | -- :: |
| | -- :: |
| | -- :: |
| | -- :: |
| | -- :: |
+----+---------------------+
rows in set mysql> SELECT table_name,partition_name,table_rows FROM information_schema.PARTITIONS WHERE table_schema=database() AND table_name='t2';
+------------+----------------+------------+
| table_name | partition_name | table_rows |
+------------+----------------+------------+
| t2 | p2013 | |
| t2 | p2014 | |
| t2 | p2015 | |
| t2 | p2016 | |
| t2 | p2017 | |
| t2 | p2099 | |
+------------+----------------+------------+
rows in set mysql> EXPLAIN PARTITIONS SELECT * FROM t2 WHERE date >= '2014-03-05 19:00:12' AND date <= '2016-03-05 18:45:12';
+----+-------------+-------+-------------------+------+---------------+------+---------+------+------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+-------------------+------+---------------+------+---------+------+------+-------------+
| | SIMPLE | t2 | p2014,p2015,p2016 | ALL | NULL | NULL | NULL | NULL | | Using where |
+----+-------------+-------+-------------------+------+---------------+------+---------+------+------+-------------+
row in set mysql>
 
    SELECT *
    FROM tb_common_biz
    PARTITION (p201707);
 
 
如果可以通过代码确定使用的PARTITION,可以这样查:
SELECT * FROM B PARTITION(p1)
 
数据插入完成后,要验证是否对应id的数据保存在了对应的分区,可以使用查询分区的命令,如下:
 
SELECT partition_name,partition_expression,partition_description,table_rows
FROM information_schema.PARTITIONS
WHERE table_schema = SCHEMA() AND table_name='tb_common_biz'
 
 
 
 
 
 
DROP TABLE IF EXISTS `tb_common_biz`;
CREATE TABLE `tb_common_biz` (
  `data_id` bigint(20) NOT NULL,
  `data_category` varchar(32) DEFAULT NULL,
  `service_type` varchar(32) DEFAULT NULL,
  `group_id` varchar(32) DEFAULT NULL,
  `group_name` varchar(64) DEFAULT NULL,
  `create_person_id` varchar(16) DEFAULT NULL,
  `create_person_name` varchar(64) DEFAULT NULL,
  `create_person_telno` varchar(32) DEFAULT NULL,
  `create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `update_person` varchar(16) DEFAULT NULL,
  `update_time` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
  `result` varchar(256) DEFAULT NULL,
  `result_ext` varchar(256) DEFAULT NULL,
  `service_owner` varchar(64) DEFAULT NULL,
  `process_instance_id` varchar(64) DEFAULT NULL,
  `task_id` varchar(64) DEFAULT NULL,
  `ext_activiti_info` varchar(256) DEFAULT NULL,
  `status` varchar(256) DEFAULT NULL,
  `ext_status` varchar(256) DEFAULT NULL,
  `data1` varchar(1024) DEFAULT NULL,
  `data2` varchar(1024) DEFAULT NULL,
  `data3` varchar(1024) DEFAULT NULL,
  `data4` varchar(1024) DEFAULT NULL,
  `data5` varchar(1024) DEFAULT NULL,
  `data6` varchar(1024) DEFAULT NULL,
  `data7` varchar(1024) DEFAULT NULL,
  `data8` varchar(1024) DEFAULT NULL,
  `data9` varchar(1024) DEFAULT NULL,
  `data10` varchar(1024) DEFAULT NULL,
  PRIMARY KEY (`data_id`)
)
PARTITION BY RANGE (data_id) (
PARTITION p201704 VALUES LESS THAN (2017050100010000001),
PARTITION p201705 VALUES LESS THAN (2017060100010000001),
PARTITION p201706 VALUES LESS THAN (2017070100010000001),
PARTITION p201707 VALUES LESS THAN (2017080100010000001),
PARTITION p201708 VALUES LESS THAN (2017090100010000001),
PARTITION p201709 VALUES LESS THAN (2017100100010000001),
PARTITION p201710 VALUES LESS THAN (2017110100010000001),
PARTITION p201711 VALUES LESS THAN (2017120100010000001),
PARTITION p201712 VALUES LESS THAN (2018010100010000001),
PARTITION p201801 VALUES LESS THAN (2018020100010000001),
PARTITION p201802 VALUES LESS THAN (2018030100010000001),
PARTITION p201803 VALUES LESS THAN (2018040100010000001),
PARTITION p201804 VALUES LESS THAN (2018050100010000001),
PARTITION p201805 VALUES LESS THAN (2018060100010000001),
PARTITION p201806 VALUES LESS THAN (2018070100010000001),
PARTITION p201807 VALUES LESS THAN (2018080100010000001),
PARTITION p201808 VALUES LESS THAN (2018090100010000001),
PARTITION p201809 VALUES LESS THAN (2018100100010000001),
PARTITION p201810 VALUES LESS THAN (2018110100010000001),
PARTITION p201811 VALUES LESS THAN (2018120100010000001),
PARTITION p201812 VALUES LESS THAN (2019010100010000001),
PARTITION p2019 VALUES LESS THAN (MAXVALUE) )
;

mysql 5.6 分区与不分区的区别的更多相关文章

  1. MySQL学习笔记十三:表分区

    1.分区一般用于非常大的表,采用“分而治之”的策略,将一个很大的对象分成多个小对象进行管理,每个分区都是一个独立的对象. 分区使用分区键将数据根据范围值,特定列值或HASH值等规则分布在不同的分区中. ...

  2. Mysql --分区(4)List分区

    LIST分区 LIST分区是建立离散的值列表告诉数据库特定的值属于哪个分区,LIST分区在很多方面类似于RANGE分区,区别在LIST分区是从属于一个枚举列表的值得集合,RANGE分区是从属于一个连续 ...

  3. Mysql --分区(3)range分区

    3.分区类型 RANGE分区 按照range分区的表是利用取值范围将数据分成分区,区间要连续并且不能互相重叠,使用values less than操作符进行分区定义 CREATE TABLE tnp ...

  4. mysql的分区技术(建立分区)

    -- mysql建立表分区,使用range方法建立: create table t_range( id int(11), money int(11) unsigned not null, date d ...

  5. mysql表分区、查看分区

    原文地址:http://blog.csdn.net/feihong247/article/details/7885199 一.       mysql分区简介 数据库分区 数据库分区是一种物理数据库设 ...

  6. mysql分区之range分区

    随着互联网的发展,各方面的数据越来越多,从最近两年大数据越来越强的呼声中就可见一斑. 我们所做的项目虽算不上什么大项目,但是由于业务量的问题,数据也是相当的多. 数据一多,就很容易出现性能问题,而为了 ...

  7. MySQL 横向表分区之RANGE分区小结

    MySQL 横向表分区之RANGE分区小结 by:授客 QQ:1033553122 目录 简介 1 RANGE分区 1 创建分区表 1 查看表分区 2 新增表分区 2 新增数据 3 分区表查询 3 删 ...

  8. mysql数据库优化(三)--分区

    mysql的分区,分表 分区:把一个数据表的文件和索引分散存储在不同的物理文件中. 特点:业务层透明,无需任何修改,即使从新分表,也是在mysql层进行更改(业务层代码不动) 分表:把原来的表根据条件 ...

  9. mysql 表分区 查看表分区 修改表分区

    原文地址:http://blog.csdn.net/feihong247/article/details/7885199 一.       mysql分区简介 数据库分区 数据库分区是一种物理数据库设 ...

  10. mysql分区表之二:MySQL的表的四种分区类型介绍

    一.什么是表分区 通俗地讲表分区是将一大表,根据条件分割成若干个小表.mysql5.1开始支持数据表分区了.如:某用户表的记录超过了600万条,那么就可以根据入库日期将表分区,也可以根据所在地将表分区 ...

随机推荐

  1. Newtonsoft.Json 转换DateTime类型为字符串时,串内部会有一个T。解决方案

    使用Newtonsoft.Json 转换DateTime类型时,若使用标准转换,则字符串内会有一个T(虽然再转换成DateTime没有问题). 若要转换成DateTime没有T,可以加上特性: pub ...

  2. [knownledge][latex] LaTex入门

    序言 最近需要写一份文档, 时间也不是特别紧. 之前一直用markdown写文档. 始终想学一下LaTex, 毕竟是学术论文界的工具. 在提及LaTex的内容之前. 事必是一定要首先提及高德纳的. 他 ...

  3. 再探树形dp

    随着校oj终于刷进了第一页,可以不用去写那些水题了,开始认真学习自己的东西,当然包括文化课.努力.. 这道题呢是道树形dp,可看到了根本就不知道怎么写思考过程: 5min 终于看懂了题 画了样例的图把 ...

  4. AndroidStudio_RecyclerView

    在这里回顾一下RecyclerView的用法 RecyclerView的用法与Button的用法很类似,只是要增加一个Adapter.java文件和item.xml文件 具体用法: 1.在page1. ...

  5. Linux 下安装JDK和jmeter 及环境配置记录过程

    一.安装首先要查看linux系统的位数,用命令getconf LONG_BIT,我的是centOS 32位 二.官网下载32位的JDK8 http://www.oracle.com/technetwo ...

  6. 【PyQt5-Qt Designer】猜数字(小项目)

    参考:https://zhuanlan.zhihu.com/p/28287825 https://www.cnblogs.com/hhh5460/p/5174266.html 最终效果: 本次练习涉及 ...

  7. 使用Maven根据WSDL生成生成Java代码

    转载:https://blog.csdn.net/pzasdq/article/details/52601473 为便于自己学习,整理 修改pom.xml <project xmlns=&quo ...

  8. Redis分布式锁服务(转)

    原文:http://www.cnblogs.com/mushroom/p/4752499.html 概述 在多线程环境下,通常会使用锁来保证有且只有一个线程来操作共享资源.比如: object obj ...

  9. JavaScript学习(五)

  10. 【托业】【新东方全真模拟】01~02-----P5~6

    12.precisely precise precision preciseness 114. 116. favorable adj.赞同的; 称赞的; 有利的; 讨人喜欢的; favor n.好感; ...