一、分区概念
分区允许根据指定的规则,跨文件系统分配单个表的多个部分。表的不同部分在不同的位置被存储为单独的表。MySQL从5.1.3开始支持Partition。
分区和手动分表对比
手动分表 ----------------- 分区
多张数据表------------一张数据表
重复数据的风险 ------------没有数据重复的风险
写入多张表------------写入一张表
没有统一的约束限制------------强制的约束限制
MySQL支持RANGE,LIST,HASH,KEY分区类型,其中以RANGE最为常用:
Range(范围)– 这种模式允许将数据划分不同范围。例如可以将一个表通过年份划分成若干个分区。
List(预定义列表)– 这种模式允许系统通过预定义的列表的值来对数据进行分割。
Hash(哈希)– 这中模式允许通过对表的一个或多个列的Hash Key进行计算,最后通过这个Hash码不同数值对应的数据区域进行分区。例如可以建立一个对表主键进行分区的表。
Key(键值)- 上面Hash模式的一种延伸,这里的Hash Key是MySQL系统产生的。
Composite(复合模式) – 以上模式的组合使用。比如,在初始化已经进行了Range范围分区的表上,我们可以对其中一个分区再进行hash哈希分区。
二、分区能做什么
逻辑数据分割
提高单一的写和读应用速度
提高分区范围读查询的速度
分割数据能够有多个不同的物理文件路径
高效的保存历史数据
一个表上的约束检查
不同的主从服务器分区策略,例如master按Hash分区,slave按range分区
Tips: 如果需要子分区的话,一般只针对range和list进行子分区,而子分区采用的模式通常为hash或者key
1.shell/批处理代码
#这里修改成分区表 ALTER TABLE sign PARTITION by RANGE(id)( PARTITION sign_p0 VALUES less than (10000000), PARTITION sign_p1 VALUES less than (20000000), PARTITION sign_p2 VALUES less than (30000000), PARTITION sign_p3 VALUES less than (40000000), PARTITION sign_p4 VALUES less than (50000000), PARTITION sign_p5 VALUES less than (60000000), PARTITION sign_p6 VALUES less than (70000000), PARTITION sign_p7 VALUES less than (80000000), PARTITION sign_p8 VALUES less than (90000000), PARTITION sign_p9 VALUES less than MAXVALUE ); #如果创建时分区表 CREATE TABLE sign(表结构)engine=MyISAM PARTITION BY RANGE(id)( PARTITION sign_p0 VALUES less than (10000000), PARTITION sign_p1 VALUES less than (20000000), PARTITION sign_p2 VALUES less than (30000000), PARTITION sign_p3 VALUES less than (40000000), PARTITION sign_p4 VALUES less than (50000000), PARTITION sign_p5 VALUES less than (60000000), PARTITION sign_p6 VALUES less than (70000000), PARTITION sign_p7 VALUES less than (80000000), PARTITION sign_p8 VALUES less than (90000000), PARTITION sign_p9 VALUES less than MAXVALUE );
2.截图
3.批处理代码
#上图所示 #通常的PC机,一亿条记录,修改时耗时接近12分钟; #分区前,大概2.5G多数据,分区后有接近3.9G

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Dreamweaver Mac version
Visual web development tools

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

SublimeText3 Chinese version
Chinese version, very easy to use

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.
