MYSQL版本:windows下mysql5.5.23,存储引擎:INNODB
参考《MYSQL技术内幕SQL编程》一书
在这里总结一下mysql分区的使用,理解也不是很深,暂时停留在基本使用上
MYSQL分区方式:
1.RANGE:行数据基于属于一个给定连续区间的列值放入分区。MySql 5.5开始
支持RANGE COLUMNS分区。
2.LIST:和RANGE分区类型一样,只是LIST分区面向的是离散的值。MySql 5.5开始
支持LIST COLUMNS分区。
3.HASH:根据用户自定义表达式的返回值来进行分区,返回值不能为负数。
4.KEY:根据MYSQL数据库提供的散列函数来进行分区。
5.COLUMNS:MYSQL5.5开始支持COLUMNS分区,可视为RANGE和LIST分区的一个进化。COLUMNS分区可以直接使用非整型的数据进行分区,分区根据类型直接比较而得到,不需要转化为整型。
COLUMNS分区支持以下数据类型:
- 所有的整型类型,如INT,TINYINT,SMALLINT,BIGINT。对FLOAT和DECIMAL不支持
- 日期类型,DATE,DATETIME。其余日期类型不支持。
- 字符串类型,如CHAR,VARCHAR,BINARY,VARBINARY。对BLOB和TEXT不支持。
(一)RANGE分区:
(1)数字分区
创建表和分区:
两个分区,P0为小于10的分区,P1为10到19的分区
CREATE TABLE t (
id int
)
PARTITION BY RANGE (id)(
PARTITION p0 VALUES LESS THAN (10) ,
PARTITION p1 VALUES LESS THAN (20)
) ;
插入数据:
mysql> select * from t;
Empty set (0.00 sec)
mysql> insert into t select 1;
Query OK, 1 row affected (0.12 sec)
Records: 1 Duplicates: 0 Warnings: 0
mysql> insert into t select 2;
Query OK, 1 row affected (0.06 sec)
Records: 1 Duplicates: 0 Warnings: 0
mysql> insert into t select 3;
Query OK, 1 row affected (0.16 sec)
Records: 1 Duplicates: 0 Warnings: 0
mysql> insert into t select 10;
Query OK, 1 row affected (0.06 sec)
Records: 1 Duplicates: 0 Warnings: 0
mysql> insert into t select 11;
Query OK, 1 row affected (0.08 sec)
Records: 1 Duplicates: 0 Warnings: 0
mysql> insert into t select 12;
Query OK, 1 row affected (0.08 sec)
Records: 1 Duplicates: 0 Warnings: 0
mysql> select * from t;
+------+
| id |
+------+
| 1 |
| 2 |
| 3 |
| 10 |
| 11 |
| 12 |
+------+
6 rows in set (0.00 sec)
查看分区具体信息:
mysql> select * from information_schema.partitions
-> where table_schema=database() and table_name='t'\G;
*************************** 1. row ***************************
TABLE_CATALOG: def
TABLE_SCHEMA: test
TABLE_NAME: t
PARTITION_NAME: p0
SUBPARTITION_NAME: NULL
PARTITION_ORDINAL_POSITION: 1
SUBPARTITION_ORDINAL_POSITION: NULL
PARTITION_METHOD: RANGE
SUBPARTITION_METHOD: NULL
PARTITION_EXPRESSION: id
SUBPARTITION_EXPRESSION: NULL
PARTITION_DESCRIPTION: 10
TABLE_ROWS: 3
AVG_ROW_LENGTH: 5461
DATA_LENGTH: 16384
MAX_DATA_LENGTH: NULL
INDEX_LENGTH: 0
DATA_FREE: 26214400
CREATE_TIME: NULL
UPDATE_TIME: NULL
CHECK_TIME: NULL
CHECKSUM: NULL
PARTITION_COMMENT:
*************************** 2. row ***************************
TABLE_CATALOG: def
TABLE_SCHEMA: test
TABLE_NAME: t
PARTITION_NAME: p1
SUBPARTITION_NAME: NULL
PARTITION_ORDINAL_POSITION: 2
SUBPARTITION_ORDINAL_POSITION: NULL
PARTITION_METHOD: RANGE
SUBPARTITION_METHOD: NULL
PARTITION_EXPRESSION: id
SUBPARTITION_EXPRESSION: NULL
PARTITION_DESCRIPTION: 20
TABLE_ROWS: 3
AVG_ROW_LENGTH: 5461
DATA_LENGTH: 16384
MAX_DATA_LENGTH: NULL
INDEX_LENGTH: 0
DATA_FREE: 0
CREATE_TIME: NULL
UPDATE_TIME: NULL
CHECK_TIME: NULL
CHECKSUM: NULL
PARTITION_COMMENT:
NODEGROUP: default
TABLESPACE_NAME: NULL
2 rows in set (0.04 sec)
可以看到PARTITION_NAME:P0分区插入TABLE_ROWS:3条数据,P1分区插入3条数据
如果此时插入一个30,那么报错
mysql> insert into t select 30;
ERROR 1526 (HY000): Table has no partition for value 30
增加分区:
mysql> alter table t add partition(partition p2 values less than (40));
Query OK, 0 rows affected (0.36 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> insert into t select 30;
Query OK, 1 row affected (0.14 sec)
Records: 1 Duplicates: 0 Warnings: 0
mysql> select * from t;
+------+
| id |
+------+
| 1 |
| 2 |
| 3 |
| 10 |
| 11 |
| 12 |
| 30 |
+------+
7rows in set (0.00 sec)
还可以对分区添加一个MAXVALUE值的分区,MAXVALUE可以理解为正无穷大,大于30的并且小于MAXVALUE的值都可以放入P3区,如下:
mysql> alter table t add partition(partition p3 values less than maxvalue);
Query OK, 0 rows affected (0.36 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> insert into t select 50;
Query OK, 1 row affected (0.10 sec)
Records: 1 Duplicates: 0 Warnings: 0
mysql> select * from t;
+------+
| id |
+------+
| 1 |
| 2 |
| 3 |
| 10 |
| 11 |
| 12 |
| 30 |
| 50 |
+------+
8rows in set (0.01 sec)
分析:
mysql> explain partitions select * from t where id>=1 and id<10\G;
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: t
partitions: p0
type: ALL
possible_keys: NULL
key: NULL
key_len: NULL
ref: NULL
rows: 3
Extra: Using where
1 row in set (0.00 sec)
可以看到只在P0分区使用
因为id范围在p0分区内,但是:
mysql> explain partitions select * from t where id>=1 and id<12\G;
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: t
partitions: p0,p1
type: ALL
possible_keys: NULL
key: NULL
key_len: NULL
ref: NULL
rows: 6
Extra: Using where
1 row in set (0.00 sec)
使用了两个分区,所以根据实际情况进行分区。
(2)日期分区
在对RANGE按日期分区的查询,优化器只能对YEAR(),TO_DAYS(),TO_SECONDS()和
UNIX_TIMESTAMP()进行优化选择。
创建表和分区
P201001 分区小于2010年2月份
P201002 分区小于2010年3月份
P201003 分区小于2010年4月份
CREATE TABLE sales2 (
money int(11) NOT NULL,
date datetime DEFAULT NULL
)
PARTITION BY RANGE (YEAR(date)*100+MONTH(date))
(PARTITION P201001 VALUES LESS THAN (201002),
PARTITION P201002 VALUES LESS THAN (201003) ,
PARTITION P201003 VALUES LESS THAN (201004) ) ;
插入数据:
mysql> insert into sales2 select 1,'20100102';
Query OK, 1 row affected (0.09 sec)
Records: 1 Duplicates: 0 Warnings: 0
mysql> insert into sales2 select 1,'20100103';
Query OK, 1 row affected (0.09 sec)
Records: 1 Duplicates: 0 Warnings: 0
mysql> insert into sales2 select 1,'20100203';
Query OK, 1 row affected (0.12 sec)
Records: 1 Duplicates: 0 Warnings: 0
mysql> insert into sales2 select 1,'20100303';
Query OK, 1 row affected (0.08 sec)
Records: 1 Duplicates: 0 Warnings: 0
分析:
mysql> explain partitions select * from sales2
-> where date>='2010-01-01' and date<='2010-1-31'\G;
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: sales2
partitions: P201001,P201002,P201003
type: ALL
possible_keys: NULL
key: NULL
key_len: NULL
ref: NULL
rows: 4
Extra: Using where
1 row in set (0.00 sec)
看到这里,可以发现对三个分区都进行了搜索,并不是我们想要的,这样处理:
CREATE TABLE sales (
money int(11) NOT NULL,
date datetime DEFAULT NULL
)
PARTITION BY RANGE (TO_DAYS(date))
(PARTITION P201001 VALUES LESS THAN (TO_DAYS('2010-02-01')),
PARTITION P201002 VALUES LESS THAN ( TO_DAYS('2010-03-01') ),
PARTITION P201003 VALUES LESS THAN ( TO_DAYS('2010-04-01') )) ;
mysql> explain partitions select * from sales
-> where date>='2010-01-01' and date<='2010-1-31'\G;
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: sales
partitions: P201001
type: ALL
possible_keys: NULL
key: NULL
key_len: NULL
ref: NULL
rows: 3
Extra: Using where
1 row in set (0.00 sec)
可以看到只在P0分区进行搜索
相关推荐
mysql-connector-java-8.0.27 jar包 mysql-connector-java-8.0.27 jar包 mysql-connector-java-8.0.27 jar包 mysql-connector-java-8.0.27 jar包 mysql-connector-java-8.0.27 jar包 mysql-connector-java-8.0.27 ...
mysql57驱动jar包mysql-connector-java-5.1.49 mysql57驱动jar包mysql-connector-java-5.1.49 mysql57驱动jar包mysql-connector-java-5.1.49 mysql57驱动jar包mysql-connector-java-5.1.49 mysql57驱动jar包mysql-...
mysql-connector-net-0.1.0.msi mysql-connector-net-6.3.6.msi mysql-connector-net-6.3.7.msi mysql-connector-net-6.3.8.msi mysql-connector-net-6.3.9.msi mysql-connector-net-6.4.0.msi mysql-connector-...
mysql-installer-community-5.7.31.0 mysql-installer-community-5.7.31.0 mysql-installer-community-5.7.31.0 mysql-installer-community-5.7.31.0 mysql-installer-community-5.7.31.0 mysql-installer-community...
amoeba-mysql-binary-2.2.0.tar.gz amoeba-mysql-binary-2.2.0.tar.gz amoeba-mysql-binary-2.2.0.tar.gz amoeba-mysql-binary-2.2.0.tar.gzamoeba-mysql-binary-2.2.0.tar.gz amoeba-mysql-binary-2.2.0.tar.gz ...
JavaEE源代码 mysql-connector-java-5.0.8-binJavaEE源代码 mysql-connector-java-5.0.8-binJavaEE源代码 mysql-connector-java-5.0.8-binJavaEE源代码 mysql-connector-java-5.0.8-binJavaEE源代码 mysql-connector...
java连接mysql数据库的驱动,里边包含两个版本:5.1.47、8.0.28 也可从以下两个地址免费下载: ...3. https://cdn.mysql.com//archives/mysql-connector-java-5.1/mysql-connector-java-5.1.47.zip
mysql-connector-net-6.8.3.msi mysql-connector-net-6.9.9.msi mysql-for-visualstudio-1.2.6.msi mysql-for-visualstudio-1.2.7.msi
本文将深入探讨这两个文件:"mysql-connector-java-5.1.40.zip" 和 "mysql-connector-java-5.1.10.jar",以及它们在Java开发中的作用。 首先,`mysql-connector-java-5.1.40.zip` 是一个压缩文件,包含了MySQL ...
mysql-connector-java-5.0.5.jar mysql-connector-java-5.1.6.jar mysql-connector-java-5.1.18.jar mysql-connector-java-5.1.21.jar mysql-connector-java-5.1.35.jar mysql-connector-java-5.1.39.jar mysql-...
包含mysql-connector-java-8.0.22.jar包含mysql-connector-java-8.0.22.jar包含mysql-connector-java-8.0.22.jar包含mysql-connector-java-8.0.22.jar包含mysql-connector-java-8.0.22.jar包含mysql-connector-java-...
解压缩后会有两个文件:mysql-8.0.18-1.el7.x86_64.rpm-bundle.tar和mysql8安装说明.txt 1、安装 rpm -i 需要安装的包文件名 举例如下: rpm -i example.rpm 安装 example.rpm 包; rpm -iv example.rpm 安装 ...
centos 下hive连接mysql驱动 ,mysql-connector-java-8.0.26-1.el7.noarch. 使用方法: 1.下载本rpm文件后,上传至你的服务器(虚拟机) 2. rpm -ivh mysql-connector-java-8.0.26-1.el7.noarch进行安装 3.安装完成...
mysql驱动器mysql-connector-j-8.2.0.jar
MySQL是世界上最流行的开源关系型数据库管理系统之一,而`mysql-connector-java`是MySQL官方提供的用于Java应用程序连接到MySQL服务器的驱动程序。`mysql-connector-java-5.1.27.jar`是这个驱动的一个特定版本,它...
mysql-community-client(x86-64) >= 5.7.9 被 mysql-community-server-5.7.18-1.el7.x86_64 需要 mysql-community-common(x86-64) = 5.7.18-1.el7 被 mysql-community-server-5.7.18-1.el7.x86_64 需要 有人说...
mysql57驱动jar包,jar包...mysql-connector-java是一款由mysql推出的官方驱动,它是java通过JDBC连接操作mysql的驱动,解压之后是jar包,需安装Java环境方可使用。需要mysql connector java的朋友们可以前来下载使用。
在这个"mysql-connector-java-8.0.28.tar.gz"压缩包中,包含了版本为8.0.28的MySQL Connector/J,这是一个稳定且功能丰富的驱动,适用于各种Java应用,包括大数据处理平台如Hive。 Hadoop是一个开源的分布式计算...
`mysql-connector-java-5.1.44`是这个驱动的一个特定版本,发布于2016年,适用于MySQL 5.x系列数据库系统。 1. **MySQL Connector/J介绍** MySQL Connector/J是官方提供的纯Java实现的MySQL JDBC驱动,支持JDBC 3...
mysql安装包:mysql-installer-community-5.7.28.0版本