大橙子网站建设,新征程启航

为企业提供网站建设、域名注册、服务器等服务

mysql5.6分区表应用

MySQL 5.6分区表测试:
DROP TABLE IF EXISTS `my_orders`;
CREATE TABLE `my_orders` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT '表主键',
  `pid` int(10) unsigned NOT NULL COMMENT '产品ID',
  `price` decimal(15,2) NOT NULL COMMENT '单价',
  `num` int(11) NOT NULL COMMENT '购买数量',
  `uid` int(10) unsigned NOT NULL COMMENT '客户ID',
  `atime` datetime NOT NULL COMMENT '下单时间',
  `utime` int(10) unsigned NOT NULL DEFAULT 0 COMMENT '修改时间',
  `isdel` tinyint(4) NOT NULL DEFAULT '0' COMMENT '软删除标识',
  PRIMARY KEY (`id`,`atime`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8


/*********分区信息**************/

站在用户的角度思考问题,与客户深入沟通,找到昭化网站设计与昭化网站推广的解决方案,凭借多年的经验,让设计与互联网技术结合,创造个性化、用户体验好的作品,建站类型包括:成都做网站、网站建设、企业官网、英文网站、手机端网站、网站推广、主机域名网站空间、企业邮箱。业务覆盖昭化地区。

PARTITION BY RANGE (YEAR(atime))
(
   PARTITION p0 VALUES LESS THAN (2016),
   PARTITION p1 VALUES LESS THAN (2017),
   PARTITION p2 VALUES LESS THAN MAXVALUE
);

DROP TABLE IF EXISTS `my_order`;
CREATE TABLE `my_order` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT '表主键',
  `pid` int(10) unsigned NOT NULL COMMENT '产品ID',
  `price` decimal(15,2) NOT NULL COMMENT '单价',
  `num` int(11) NOT NULL COMMENT '购买数量',
  `uid` int(10) unsigned NOT NULL COMMENT '客户ID',
  `atime` datetime NOT NULL COMMENT '下单时间',
  `utime` int(10) unsigned NOT NULL DEFAULT 0 COMMENT '修改时间',
  `isdel` tinyint(4) NOT NULL DEFAULT '0' COMMENT '软删除标识',
  PRIMARY KEY (`id`,`atime`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

/**************************向分区表插入数据****************************/

INSERT INTO my_orders(`pid`,`price`,`num`,`uid`,`atime`) VALUES(1,12.23,1,89757,CURRENT_TIMESTAMP());
INSERT INTO my_orders(`pid`,`price`,`num`,`uid`,`atime`) VALUES(1,12.23,1,89757,'2016-05-01 00:00:00');
INSERT INTO my_orders(`pid`,`price`,`num`,`uid`,`atime`) VALUES(1,12.23,1,89757,'2017-05-01 00:00:00');
INSERT INTO my_orders(`pid`,`price`,`num`,`uid`,`atime`) VALUES(1,12.23,1,89757,'2018-05-01 00:00:00');
INSERT INTO my_orders(`pid`,`price`,`num`,`uid`,`atime`) VALUES(1,12.23,1,89756,'2015-05-01 00:00:00');
INSERT INTO my_orders(`pid`,`price`,`num`,`uid`,`atime`) VALUES(1,12.23,1,89756,'2016-05-01 00:00:00');
INSERT INTO my_orders(`pid`,`price`,`num`,`uid`,`atime`) VALUES(1,12.23,1,89756,'2017-05-01 00:00:00');
INSERT INTO my_orders(`pid`,`price`,`num`,`uid`,`atime`) VALUES(1,12.23,1,89756,'2018-05-01 00:00:00');

/**************************向未分区表插入数据****************************/

INSERT INTO my_order(`pid`,`price`,`num`,`uid`,`atime`) VALUES(1,12.23,1,89757,CURRENT_TIMESTAMP());
INSERT INTO my_order(`pid`,`price`,`num`,`uid`,`atime`) VALUES(1,12.23,1,89757,'2016-05-01 00:00:00');
INSERT INTO my_order(`pid`,`price`,`num`,`uid`,`atime`) VALUES(1,12.23,1,89757,'2017-05-01 00:00:00');
INSERT INTO my_order(`pid`,`price`,`num`,`uid`,`atime`) VALUES(1,12.23,1,89757,'2018-05-01 00:00:00');
INSERT INTO my_order(`pid`,`price`,`num`,`uid`,`atime`) VALUES(1,12.23,1,89756,'2015-05-01 00:00:00');
INSERT INTO my_order(`pid`,`price`,`num`,`uid`,`atime`) VALUES(1,12.23,1,89756,'2016-05-01 00:00:00');
INSERT INTO my_order(`pid`,`price`,`num`,`uid`,`atime`) VALUES(1,12.23,1,89756,'2017-05-01 00:00:00');
INSERT INTO my_order(`pid`,`price`,`num`,`uid`,`atime`) VALUES(1,12.23,1,89756,'2018-05-01 00:00:00';

/**********************************主从复制大量数据******************************/

INSERT INTO `my_orders`(`pid`,`price`,`num`,`uid`,`atime`) SELECT `pid`,`price`,`num`,`uid`,`atime` FROM `my_orders`;
INSERT INTO `my_order`(`pid`,`price`,`num`,`uid`,`atime`) SELECT `pid`,`price`,`num`,`uid`,`atime` FROM `my_order`;

/***************************查询性能分析**************************************/

SELECT * FROM `my_orders` WHERE `uid`=89757 AND `atime`< CURRENT_TIMESTAMP();
/****用时0.084s****/
SELECT * FROM `my_order` WHERE `uid`=89757 AND `atime`< CURRENT_TIMESTAMP();
/****用时0.284s****/
EXPLAIN PARTITIONS SELECT * FROM `my_orders` WHERE `uid`=89757 AND `atime`< CURRENT_TIMESTAMP();
+----+-------------+-----------+------------+------+---------------+------+---------+------+-------+-------------+
| id | select_type | table     | partitions | type | possible_keys | key  | key_len | ref  | rows  | Extra       |
+----+-------------+-----------+------------+------+---------------+------+---------+------+-------+-------------+
|  1 | SIMPLE      | my_orders | p0,p1      | ALL  | NULL          | NULL | NULL    | NULL | 16419 | Using where |
+----+-------------+-----------+------------+------+---------------+------+---------+------+-------+-------------+
EXPLAIN PARTITIONS SELECT * FROM `my_order` WHERE `uid`=89757 AND `atime`< '2018-05-01 00:00:00';
+----+-------------+----------+------------+------+---------------+------+---------+------+-------+-------------+
| id | select_type | table    | partitions | type | possible_keys | key  | key_len | ref  | rows  | Extra       |
+----+-------------+----------+------------+------+---------------+------+---------+------+-------+-------------+
|  1 | SIMPLE      | my_order | NULL       | ALL  | NULL          | NULL | NULL    | NULL | 32099 | Using where |
+----+-------------+----------+------------+------+---------------+------+---------+------+-------+-------------+
如果查询只有分区键,也可以用到分区裁剪,但没有用到索引
EXPLAIN PARTITIONS SELECT * FROM `my_orders` WHERE `id`=36 AND `atime`< CURRENT_TIMESTAMP();;
+----+-------------+-----------+------------+-------+---------------+---------+---------+------+------+-------------+
| id | select_type | table     | partitions | type  | possible_keys | key     | key_len | ref  | rows | Extra       |
+----+-------------+-----------+------------+-------+---------------+---------+---------+------+------+-------------+
|  1 | SIMPLE      | my_orders | p0,p1      | range | PRIMARY       | PRIMARY | 9       | NULL |    2 | Using where |
+----+-------------+-----------+------------+-------+---------------+---------+---------+------+------+-------------+
EXPLAIN PARTITIONS SELECT * FROM `my_order` WHERE `id`=36 AND `atime`< CURRENT_TIMESTAMP();;
+----+-------------+----------+------------+-------+---------------+---------+---------+------+------+-------------+
| id | select_type | table    | partitions | type  | possible_keys | key     | key_len | ref  | rows | Extra       |
+----+-------------+----------+------------+-------+---------------+---------+---------+------+------+-------------+
|  1 | SIMPLE      | my_order | NULL       | range | PRIMARY       | PRIMARY | 9       | NULL |    1 | Using where |
+----+-------------+----------+------------+-------+---------------+---------+---------+------+------+-------------+


如果采用where id and atime查询,则通过主键索引可以查询,且可以用到分区

/*****************HASH 分区表*****************/

CREATE TABLE `msgs` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '表主键',
  `sender` int(10) unsigned NOT NULL COMMENT '发送者ID',
  `reciver` int(10) unsigned NOT NULL COMMENT '接收者ID',
  `msg_type` tinyint(3) unsigned NOT NULL COMMENT '消息类型',
  `msg` varchar(225) NOT NULL COMMENT '消息内容',
  `atime` int(10) unsigned NOT NULL COMMENT '发送时间',
  `sub_id` tinyint(3) unsigned NOT NULL COMMENT '部门ID',
  PRIMARY KEY (`id`,`sub_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8

/*********分区信息**************/

PARTITION BY HASH(sub_id)
PARTITIONS 10;

/*****************LIST分区表*********************/

CREATE TABLE `products` (
`id`  bigint UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '表主键' ,
`name`  varchar(64) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '产品名称' ,
`metrial`  tinyint UNSIGNED NOT NULL COMMENT '材质' ,
`weight`  double UNSIGNED NOT NULL DEFAULT 0 COMMENT '重量' ,
`vol`  double UNSIGNED NOT NULL DEFAULT 0 COMMENT '容积' ,
`c_id`  tinyint UNSIGNED NOT NULL COMMENT '供货公司ID' ,
PRIMARY KEY (`id`,`c_id`)
)ENGINE=InnoDB DEFAULT CHARSET=utf8

/*********分区信息**************/

PARTITION BY LIST(c_id)
(
    PARTITION pA VALUES IN (1,3,11,13),
    PARTITION pB VALUES IN (2,4,12,14),
    PARTITION pC VALUES IN (5,7,15,17),
    PARTITION pD VALUES IN (6,8,16,18),
    PARTITION pE VALUES IN (9,10,19,20)
);
创建分区表:主键索引是分区键的一部分


分享题目:mysql5.6分区表应用
本文路径:http://dzwzjz.com/article/piipge.html
在线咨询
服务热线
服务热线:028-86922220
TOP