【MYSQL】分区表
对于MYSQL的态度一直都是会基本SQL和简单命令就行,最近处理一个数据量很大的项目,为了提高效率,在数据库方面的瓶颈上,选择了使用分区表来提高查询效率。至此和大家一起分享一下。 1.引言 本文初略的讲述了mysql数据库如何分区表。 2.环境要求 在5.1版本
对于MYSQL的态度一直都是会基本SQL和简单命令就行,最近处理一个数据量很大的项目,为了提高效率,在数据库方面的瓶颈上,选择了使用分区表来提高查询效率。至此和大家一起分享一下。
1.引言
本文初略的讲述了mysql数据库如何分区表。
2.环境要求
在5.1版本中不是默认就安装了,而在之后版本中一般默认选择了安装分区表支持。可以通过如下方式查看当前数据库是否支持分区表操作:
使用show variables like '%partition%';如果不支持分区,那么value字段值为No。
3.重要概念描述
3.1 分区字段
1)当仅存在单一主键时,不存在唯一键,那么分区字段必须是主键字段;
2)当存在复合主键时,不存在唯一键,那么分区字段必须是主键组合的一部分字段,一个或多个。
3)当主键和唯一键都存在时,那么分区字段必须同时包括主键字段和唯一键字段。
4.分区表类型
4.1 range分区
1)语法展示:
# 语法 # 在创建表单的最后,添加partitions by range(分区字段)( # partition 分区名 values less than(阀值1), # partition 分区名 values less than(阀值2), # ... # partition 分区名 values less than(阀值n), # )
示例展示:
create table test_range( id int auto_increment, description varchar(50), primary key(id) ) ENGINE=InnoDB auto_increment=1 default charset=utf8 partition by range(id)( partition p1 values less than(6), #id<6的存放在p1分区 partition p2 values less than(11) #6 <= id < 11 存放在p2分区 );
查看分区情况:
show create table test_range;
650) this.width=650;" title="p1png.png" alt="wKioL1R6z4qC137DAADITZNS8H8149.jpg" />
注意到,在显示的表结构添加了分区表的信息。
数据测试:
insert into test_range values(null, "test1"); insert into test_range values(null, "test2"); insert into test_range values(null, "test3"); insert into test_range values(null, "test4"); insert into test_range values(null, "test5"); insert into test_range values(null, "test6"); insert into test_range values(null, "test7"); insert into test_range values(null, "test8"); insert into test_range values(null, "test9"); insert into test_range values(null, "test10");
插入10条数据,此时我们来查看其查询执行过程:
650) this.width=650;" title="p2.png" alt="wKioL1R60LDBET1bAADRpczcpSo931.jpg" />
从结果可以发现,其只是在p1分区执行的查询,那么此时就减少了查询扫描的数据量,从而提高了查询效率。
如果此时,我们插入第11条数据会发生什么情况呢?
insert into test_range values(null, "test11"); 会发错:insert into test_range values(null, "test11") Error Code: 1526. Table has no partition for value 11 0.015 sec
原因很简单,因为在我们创建表单时,仅仅指定了1 - 10的id数值分区,当插入id=11时的分区时,此时没有分区提供,那么就引发错误,那么如果解决这样的问题呢,采取如下方式,修改表的分区方式:
alter table test_range add partition( partition p3 values less than(MAXVALUE) ); # 添加一个分区,也就是p3是id从11到maxValue的存放区域
此时插入id=11的数据,并执行查询解析:
650) this.width=650;" title="p3.png" alt="wKiom1R60nnwSlUUAADL0geAo20618.jpg" /> 发现,已经将其分配到p3分区中了。
还需要特别注意的时,使用partition by range(分区字段),其中的分区字段可以是分区字段的表单式,但是必须是返回的整数,在5.5版本中,可以使用partition by range column/columns语法,指定某个字段。这里不做介绍。大家可以自己尝试一下。
4.2 list分区
list分区可以理解为集合分区方式,意思就是指定某个集合来分区。
语法展示:
partition by list(分区字段表达式)( partition 分区名 values in(value1, value2,...,valuen)#分区集合 );
示例展示:
create table test_list( id int auto_increment, description varchar(50), primary key(id) )ENGINE=InnoDB auto_increment=1 default charset=utf8 partition by list(id)( partition p1 values in (1, 3, 5, 7, 9),#id=1,3,5,7,9分配至p1区 partition p2 values in (2, 4, 6, 8, 10)#id=2,4,6,8,10分配至p2区 ); #可以如4.1中使用show create table test_list查看表创建结构。
数据测试:使用4.1中数据测试sql,插入10条数据。
650) this.width=650;" title="p4.png" alt="wKioL1R61Y-RjP7aAADWGviW2FI351.jpg" /> 可以发现其查询的仅仅是p1区。如果需要添加分区,可以使用4.1中使用的add partition来添加分区。
4.3 hash分区
使用hash函数得到取模,分配到不同的分区中。分区表达式必须返回整数。
语法展示:
partition by hash(分区表达式) partitions 表数量(模数).
示例展示:
create table test_hash( id int auto_increment, description varchar(50), primary key(id) ) ENGINE=InnoDB auto_increment=1 default charset=utf8 partition by hash(id) partitions 3; #以id分区,分配到3张表中
数据测试:插入4.1类同10条数据
650) this.width=650;" title="p5.png" alt="wKioL1R615mA8FC8AADLcG2rGPs147.jpg" />
你也可以尝试修改id值,查看其分配的分区。hash分区还有一种叫做linear hash线性分区,这里不做介绍,
4.4 key分区
在本次开发中,我选择的是key分区,因为其是针对一个或多个字段作为分区字段,不要求是正整数,其内部调用的是自己的hash函数,计算出hash整数值,然后取模分表。
语法展示:
partition by key(分区字段组合) partitions 表数(模数)。
操作和Hash分区一致,这里就不做累赘的展示了。
5.额外扩展
5.1 在实际开发中,经常出现的情况是表已经上线使用,那么必须动态添加分区类型。
alter table 表名 partition by hash/key (分区字段表达式) [partitions 表数]#如果不加partitions那么默认为1. alter table 表名 partition by range/list(分区字段表达式)(具体分区设置)。
5.2 当发现之前的分区需要添加新的分区时,采取如下方式:
list/range : alter table 表名 add partition (partition 分区名 [values in|values less than] [集合|数值]); hash/key : alter table 表名 add partition partitions 表数;
例如:修改上述test_hash的分区数量
alter table test_hash add partition partitions 6;
5.3 删除某个分区/删除所有分区
# 删除某个分区 list/range : alter table 表名 drop partition 分区名1, 分区名2,...; #例如: alter table test_list drop partition p1; hash/key : 上述语法不成立 # 删除整个分区 alter table test_hash remove partitioning;
还有诸如合并分区,以及5.5的一些新特性,list/range 增加column,columns支持。本文不做过多阐述。

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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



In MySQL database, the relationship between the user and the database is defined by permissions and tables. The user has a username and password to access the database. Permissions are granted through the GRANT command, while the table is created by the CREATE TABLE command. To establish a relationship between a user and a database, you need to create a database, create a user, and then grant permissions.

MySQL is suitable for beginners because it is simple to install, powerful and easy to manage data. 1. Simple installation and configuration, suitable for a variety of operating systems. 2. Support basic operations such as creating databases and tables, inserting, querying, updating and deleting data. 3. Provide advanced functions such as JOIN operations and subqueries. 4. Performance can be improved through indexing, query optimization and table partitioning. 5. Support backup, recovery and security measures to ensure data security and consistency.

Data Integration Simplification: AmazonRDSMySQL and Redshift's zero ETL integration Efficient data integration is at the heart of a data-driven organization. Traditional ETL (extract, convert, load) processes are complex and time-consuming, especially when integrating databases (such as AmazonRDSMySQL) with data warehouses (such as Redshift). However, AWS provides zero ETL integration solutions that have completely changed this situation, providing a simplified, near-real-time solution for data migration from RDSMySQL to Redshift. This article will dive into RDSMySQL zero ETL integration with Redshift, explaining how it works and the advantages it brings to data engineers and developers.

To fill in the MySQL username and password: 1. Determine the username and password; 2. Connect to the database; 3. Use the username and password to execute queries and commands.

1. Use the correct index to speed up data retrieval by reducing the amount of data scanned select*frommployeeswherelast_name='smith'; if you look up a column of a table multiple times, create an index for that column. If you or your app needs data from multiple columns according to the criteria, create a composite index 2. Avoid select * only those required columns, if you select all unwanted columns, this will only consume more server memory and cause the server to slow down at high load or frequency times For example, your table contains columns such as created_at and updated_at and timestamps, and then avoid selecting * because they do not require inefficient query se

Detailed explanation of database ACID attributes ACID attributes are a set of rules to ensure the reliability and consistency of database transactions. They define how database systems handle transactions, and ensure data integrity and accuracy even in case of system crashes, power interruptions, or multiple users concurrent access. ACID Attribute Overview Atomicity: A transaction is regarded as an indivisible unit. Any part fails, the entire transaction is rolled back, and the database does not retain any changes. For example, if a bank transfer is deducted from one account but not increased to another, the entire operation is revoked. begintransaction; updateaccountssetbalance=balance-100wh

Navicat itself does not store the database password, and can only retrieve the encrypted password. Solution: 1. Check the password manager; 2. Check Navicat's "Remember Password" function; 3. Reset the database password; 4. Contact the database administrator.

SQLLIMIT clause: Control the number of rows in query results. The LIMIT clause in SQL is used to limit the number of rows returned by the query. This is very useful when processing large data sets, paginated displays and test data, and can effectively improve query efficiency. Basic syntax of syntax: SELECTcolumn1,column2,...FROMtable_nameLIMITnumber_of_rows;number_of_rows: Specify the number of rows returned. Syntax with offset: SELECTcolumn1,column2,...FROMtable_nameLIMIToffset,number_of_rows;offset: Skip
