博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
谨慎使用MYSQL表分区
阅读量:6785 次
发布时间:2019-06-26

本文共 5233 字,大约阅读时间需要 17 分钟。

test1表是未分区

test2表是hash分区

以下是两个表的表结构:

mysql> show create table test1;

+-------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table                                                                                                                                                                                      |
+-------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| test1 | CREATE TABLE `test1` (
  `id` int(11) NOT NULL,
  `tid` int(11) DEFAULT NULL,
  `name` varchar(10) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `tid` (`tid`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+-------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

 

mysql> show create table test2;

+-------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table                                                                                                                                                                                                                                       |
+-------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| test2 | CREATE TABLE `test2` (
  `id` int(11) NOT NULL,
  `tid` int(11) DEFAULT NULL,
  `name` varchar(10) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `tid` (`tid`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
/*!50100 PARTITION BY HASH (id)
PARTITIONS 10 */ |
+-------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.01 sec)

在主键id列上用上hash分区。

 

如果是在主键上查询,那么分区要比不分区性能高,例如:

mysql> explain partitions select * from test1 where id in (86,777,432345);

+----+-------------+-------+------------+-------+---------------+---------+---------+------+------+-------------+
| id | select_type | table | partitions | type  | possible_keys | key     | key_len | ref  | rows | Extra       |
+----+-------------+-------+------------+-------+---------------+---------+---------+------+------+-------------+
|  1 | SIMPLE      | test1 | NULL       | range | PRIMARY       | PRIMARY | 4       | NULL |    3 | Using where |
+----+-------------+-------+------------+-------+---------------+---------+---------+------+------+-------------+
1 row in set (0.06 sec)
mysql> explain partitions select * from test2 where id in (86,777,432345); 
+----+-------------+-------+------------+-------+---------------+---------+---------+------+------+-------------+
| id | select_type | table | partitions | type  | possible_keys | key     | key_len | ref  | rows | Extra       |
+----+-------------+-------+------------+-------+---------------+---------+---------+------+------+-------------+
|  1 | SIMPLE      | test2 | p5,p6,p7   | range | PRIMARY       | PRIMARY | 4       | NULL |    3 | Using where |
+----+-------------+-------+------------+-------+---------------+---------+---------+------+------+-------------+
1 row in set (0.01 sec)
 

只在分区P5/6/7上就扫描到了记录,减少了过多的磁盘IO。

但如果在索引tid上查询,性能就很低了。

mysql>  explain partitions select * from test1 where tid = 86419804; 

+----+-------------+-------+------------+------+---------------+------+---------+-------+------+-------------+
| id | select_type | table | partitions | type | possible_keys | key  | key_len | ref   | rows | Extra       |
+----+-------------+-------+------------+------+---------------+------+---------+-------+------+-------------+
|  1 | SIMPLE      | test1 | NULL       | ref  | tid           | tid  | 5       | const |    1 | Using where |
+----+-------------+-------+------------+------+---------------+------+---------+-------+------+-------------+
1 row in set (0.00 sec)
mysql>  explain partitions select * from test2 where tid = 86419804; 
+----+-------------+-------+-------------------------------+------+---------------+------+---------+-------+------+-------------+
| id | select_type | table | partitions                    | type | possible_keys | key  | key_len | ref   | rows | Extra       |
+----+-------------+-------+-------------------------------+------+---------------+------+---------+-------+------+-------------+
|  1 | SIMPLE      | test2 | p0,p1,p2,p3,p4,p5,p6,p7,p8,p9 | ref  | tid           | tid  | 5       | const |   10 | Using where |
+----+-------------+-------+-------------------------------+------+---------------+------+---------+-------+------+-------------+
1 row in set (0.00 sec)
 

要扫描10个分区,才能得到结果。

mysql> select sql_no_cache * from test1 where tid=86419804; 

+----+----------+------+
| id | tid      | name |
+----+----------+------+
| 11 | 86419804 | abc2 |
+----+----------+------+
1 row in set (0.40 sec)
mysql> select sql_no_cache * from test2 where tid=86419804; 
+----+----------+------+
| id | tid      | name |
+----+----------+------+
| 11 | 86419804 | abc2 |
+----+----------+------+
1 row in set (1.98 sec)

分区后的查询结果要比未分区的慢很多。

结论:分区规则仅限制在主键上使用,性能会有提高,在其他字段上使用,即使有索引,性能也会很差。

 

本文转自 liang3391 51CTO博客,原文链接:http://blog.51cto.com/liang3391/844523

转载地址:http://vvdgo.baihongyu.com/

你可能感兴趣的文章
python socket 原汁原味代码
查看>>
Kubernetes的service mesh——第一部分:Service的重要指标
查看>>
全链路监控
查看>>
我的友情链接
查看>>
我的IT博客之路
查看>>
深入理解javascript原型和闭包(10)——this
查看>>
系统集成资质培训-论文写作-几个题目如何写?(updated)
查看>>
搭建自己的框架之1:Rxjava2+Retrofit2 实现Android Http请求
查看>>
排序算法-快速排序
查看>>
CSS3 Background 属性介绍
查看>>
frameset 的一些小应用
查看>>
eclipse自动换行
查看>>
Android PDF 阅读器源码
查看>>
我的友情链接
查看>>
silverlight渐隐效果
查看>>
使用Docker实现php代码在线测试执行工具-toolfk.com
查看>>
簡單範例 mergecap,wireshark 付屬程式
查看>>
网络文件传输学习
查看>>
Installation Oracle11gR2 RAC One Node ---创建数据库
查看>>
spring 通过EsClientFactory注入elasticsearch
查看>>