您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

MySQL 分区之RANGE HASH

2024/6/17 0:54:36发布16次查看
就讨论range 跟hash 以及range 结合hash进行的分区操作。从mysql5.1之后,分区功能出现了,表分区就像是将一个大表分成了若干个小
从mysql5.1之后,分区功能出现了,表分区就像是将一个大表分成了若干个小表,用户在执行查询的时候无需进行全表扫描,只需要对满足要求的表分区中进行查询即可,极大的提高了查询速率,另外,表分区的实现也方便了对数据的管理,比如产品需要删除去年的所有数据,那么只需要将去年数据所在的表分区删除即可。
mysql 表分区有很多种,详情点击:
此处就讨论range 跟hash 以及range 结合hash进行的分区操作。
注意:所有的表分区使用的列均需要使用源表中存在的主键或者唯一索引列,否则创建失败,如果源表中本来就不存在任何的主键或者唯一索引列,那么可以在分区的时候根据需要选取任意列。
range:顾名思义,通过确定选取列的值的范围的方式进行分区。
推荐阅读:
mysql分区表实践
mysql分区表未建导致rsyslog写入数据失败
如下是创建普通表的语句:
为了实验的方便,此处date 字段使用的时间类型为:datetime,而非timestamp,原因是timestamp不支持在分区的时候使用year(),month(),to_days()等操作,只能使用unix_timstamp()函数,所以在设计表的时候需要考虑到这点:
create  table t1  ( id int, date datetime default current_timestamp) engine=innodb;
插入一些测试数据:
mysql> select * from t1;
+------+---------------------+
| id  | date                |
+------+---------------------+
|    1 | 2013-05-23 12:59:39 |
|    2 | 2013-05-23 12:59:43 |
|    3 | 2013-05-23 12:59:44 |
|    4 | 2013-07-04 19:35:45 |
|    5 | 2014-04-04 19:35:45 |
|    6 | 2014-05-04 19:35:45 |
|    7 | 2015-05-04 19:35:45 |
|    8 | 2015-05-05 19:35:45 |
|    9 | 2017-05-05 19:35:45 |
|  10 | 2018-05-05 19:35:45 |
+------+---------------------+
10 rows in set (0.00 sec)
查看查询语句执行计划:发现进行了全表扫描
mysql> explain select * from t1;
+----+-------------+-------+------+---------------+------+---------+------+------+-------+
| id | select_type | table | type | possible_keys | key  | key_len | ref  | rows | extra |
+----+-------------+-------+------+---------------+------+---------+------+------+-------+
|  1 | simple      | t1    | all  | null          | null | null    | null |  10 | null  |
+----+-------------+-------+------+---------------+------+---------+------+------+-------+
1 row in set (0.00 sec)
mysql> explain select * from t1 where date >= '2014-03-05 19:00:12'
    -> and date +----+-------------+-------+------+---------------+------+---------+------+------+-------------+
| id | select_type | table | type | possible_keys | key  | key_len | ref  | rows | extra      |
+----+-------------+-------+------+---------------+------+---------+------+------+-------------+
|  1 | simple      | t1    | all  | null          | null | null    | null |  10 | using where |
+----+-------------+-------+------+---------------+------+---------+------+------+-------------+
1 row in set (0.00 sec)
创建新表t2,并根据年份进行表分区
mysql> create  table t2  ( id int, date datetime default current_timestamp) engine=innodb
    ->    partition by range (year(date)) (
    ->    partition p2013 values less than(2014),
    ->    partition p2014 values less than(2015),
    ->    partition p2015 values less than(2016),
    ->    partition p2016 values less than(2017),
    ->    partition p2017 values less than(2018),
    ->    partition p2099 values less than maxvalue
    -> ) ;
query ok, 0 rows affected (2.47 sec)
查看数据分布状态并导入t1表的数据:
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          |          0 |
| t2        | p2014          |          0 |
| t2        | p2015          |          0 |
| t2        | p2016          |          0 |
| t2        | p2017          |          0 |
| t2        | p2099          |          0 |
+------------+----------------+------------+
6 rows in set (0.00 sec)
mysql> select * from t2;
empty set (0.00 sec)
mysql> explain select * from t2;
+----+-------------+-------+------+---------------+------+---------+------+------+-------+
| id | select_type | table | type | possible_keys | key  | key_len | ref  | rows | extra |
+----+-------------+-------+------+---------------+------+---------+------+------+-------+
|  1 | simple      | t2    | all  | null          | null | null    | null |    6 | null  |
+----+-------------+-------+------+---------------+------+---------+------+------+-------+
1 row in set (0.00 sec)
mysql> insert into t2 select * from t1;
query ok, 10 rows affected (0.36 sec)
records: 10  duplicates: 0  warnings: 0
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product