search
HomeDatabaseMysql TutorialMySQL分区技术(一)_MySQL

4:MySQL 分区技术(是mysql 5.1以版本后开始用->是甲骨文mysql技术团队维护人员以插件形式插入到mysql里面的技术)

目前,针对海量数据的优化主要有2中方法:

1:大表拆成小表的方式(物理上)

一:垂直分表->一张垂直切成几张

二:水平分表(一般重点)->横切,意思就是一张表有100个数据横切10张表,一张表存10条(字段一致)

2:SQL语句的优化(可以通过增加索引等来调整,但是数据量大的增大会导致索引的维护代价增大)

水平分区技术将一个表拆成多个表,比较常用的方式是将表中的记录按照某种hash算法进行拆分,简单的拆分方法如取摸

方式。同样,这种分区方法也必须对前端的应用程序中的SQL进行修改方可以使用。而且对于一个SQL,它可能会修改两个

表,那么你必须地写出2个SQL语句从而可以完成一个逻辑事务,使得程序的判断逻辑越来越复杂,这样也导致程序的维护代价

高,也就失去了采用数据库的优势。

*因此:分区技术可以有力地避免如上的弊端,成为解决海量数据存储的有力方法。

分区技术:

->>有效解决了:物理上拆分多个表,逻辑上操作一个表表明不变

->>MySQL分区技术介绍(*主要用的是range 和 list 分区*):

-----分区在逻辑上是一张表,在硬件/物理上是多张表,就是拆分表索引和数据-----

MySQL的分区技术不同与之前的分表技术,它与水平分表有点类似,但是它在逻辑层进行的水平分表,

对与应用程序而言它还是一张表,

MySQL5.1版本后有4中分区类型:

一:RANGE分区(用的最多):基于属于一个给定连续区间的列值(字段),把多行分配给分区  -->基于女字段为参考点来进行分区

--将一个表拆分成:索引文件,数据文件分片存储

二:LIST分区:类似于按range分区,区别在于list分区是基于列值匹配一个离散值集合中的某个值来进行选择(列里面的值是固定值时候来进行分区,而且是枚举类型的值适合用list分区  -->比如说 性别:男,女)

三:HASH分区:基于用户定义的表达式的返回值来进行选择的分区,改表达式使用将要插入到表中的这些行的列值计算,这个函数可以包含MySQL中有效的、产生负整数值的任何表达式

--->把每次插入的数据随机的平均的分配到多个分区里面,最终多个分区里面的数据时平均分配的,但是每个分区里面的数值肯能不太一样,因为是随机分配的(一般可以用来做MySQL分区的测试来使用)

四:KEY分区:类似于按hash分区,区别在于key分区只支持计算一列或多列,且MySQL服务器提供其自身的哈希函数

测试一下(测试用hash类型的):->myisam增删改查的速度快
create table t2(id int)engine=myisam
partition by hash(id)
partitions 5;                 ->能后当你插入数据的时候就会随机分配插入个个分区中
建立一个存储
\d //         ->修改结束符号  之前是;号改成 //
create procedure p5()
begin
set @i=1;
while @i insert into t4 values(@i);
set @i=@i+1;
end while;
end //

执行刚才建立的存储
call p3()     ->表p3就插入了9999条数据

innodb的数据结构:

分为:共享表空间及其独占表空间

一:innodb表结构共享表空间不能做成分区表:

所有文件的数据和索引都在ibddata1(比如你建了2个表会对应生成frm文件,但是2个表的所有数据和索引全部在这个文件里面共用,所有不能对表做正真的分区,初始值是10M)

原因:数据和索引全都是放在一个文件里面 .ibddata1文件

二:innodb表结构要想做出分区表必须是“独占表空间”

原因:数据和索引全都是独立的一个文件

开启独占空间:(*必须配置文件中开启文件才能做出独占表空间,才能做成分区表*)
innodb_data_home_dir = C:\mysql\data\
innodb_data_file_path = ibdata1:10M:autoextend
innodb_log_group_home_dir = C:\mysql\data\
innodb_file_per_table=1   ->配置文件中innodb下方加上
重启:MySQL   -->pkill mysqld 关闭进程  重新启动MySQL-bin/mysqld_safe --user=mysql &

测试:
create table t4(id int)engine=innodb
partition by RANGE(id)(
partition p0 values less than(10000),
partition p1 values less than(20000),
PARTITION p2 VALUES less than MAXVALUE);

能后你在创建innodb类型的数据表后,你会发现建一个x表就有x.frm x.ibd文件,就不会和其他表放到一起从而做表分区

*重点总结:只有把innodb设置成独立的表空间后,才能创建innodb表引擎的表分区

相关命令:
/s;   查看详细信息版本啊编码啊 什么的。。。
show engines; 查看默认表引擎
show plugins; 查看当前MySQL的所有插件,可以查看是否支持分区partition
show index from from tabName;  查看索引
show procedure status; 查看简历的存储

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
What Are the Limitations of Using Views in MySQL?What Are the Limitations of Using Views in MySQL?May 14, 2025 am 12:10 AM

MySQLviewshavelimitations:1)Theydon'tsupportallSQLoperations,restrictingdatamanipulationthroughviewswithjoinsorsubqueries.2)Theycanimpactperformance,especiallywithcomplexqueriesorlargedatasets.3)Viewsdon'tstoredata,potentiallyleadingtooutdatedinforma

Securing Your MySQL Database: Adding Users and Granting PrivilegesSecuring Your MySQL Database: Adding Users and Granting PrivilegesMay 14, 2025 am 12:09 AM

ProperusermanagementinMySQLiscrucialforenhancingsecurityandensuringefficientdatabaseoperation.1)UseCREATEUSERtoaddusers,specifyingconnectionsourcewith@'localhost'or@'%'.2)GrantspecificprivilegeswithGRANT,usingleastprivilegeprincipletominimizerisks.3)

What Factors Influence the Number of Triggers I Can Use in MySQL?What Factors Influence the Number of Triggers I Can Use in MySQL?May 14, 2025 am 12:08 AM

MySQLdoesn'timposeahardlimitontriggers,butpracticalfactorsdeterminetheireffectiveuse:1)Serverconfigurationimpactstriggermanagement;2)Complextriggersincreasesystemload;3)Largertablesslowtriggerperformance;4)Highconcurrencycancausetriggercontention;5)M

MySQL: Is it safe to store BLOB?MySQL: Is it safe to store BLOB?May 14, 2025 am 12:07 AM

Yes,it'ssafetostoreBLOBdatainMySQL,butconsiderthesefactors:1)StorageSpace:BLOBscanconsumesignificantspace,potentiallyincreasingcostsandslowingperformance.2)Performance:LargerrowsizesduetoBLOBsmayslowdownqueries.3)BackupandRecovery:Theseprocessescanbe

MySQL: Adding a user through a PHP web interfaceMySQL: Adding a user through a PHP web interfaceMay 14, 2025 am 12:04 AM

Adding MySQL users through the PHP web interface can use MySQLi extensions. The steps are as follows: 1. Connect to the MySQL database and use the MySQLi extension. 2. Create a user, use the CREATEUSER statement, and use the PASSWORD() function to encrypt the password. 3. Prevent SQL injection and use the mysqli_real_escape_string() function to process user input. 4. Assign permissions to new users and use the GRANT statement.

MySQL: BLOB and other no-sql storage, what are the differences?MySQL: BLOB and other no-sql storage, what are the differences?May 13, 2025 am 12:14 AM

MySQL'sBLOBissuitableforstoringbinarydatawithinarelationaldatabase,whileNoSQLoptionslikeMongoDB,Redis,andCassandraofferflexible,scalablesolutionsforunstructureddata.BLOBissimplerbutcanslowdownperformancewithlargedata;NoSQLprovidesbetterscalabilityand

MySQL Add User: Syntax, Options, and Security Best PracticesMySQL Add User: Syntax, Options, and Security Best PracticesMay 13, 2025 am 12:12 AM

ToaddauserinMySQL,use:CREATEUSER'username'@'host'IDENTIFIEDBY'password';Here'showtodoitsecurely:1)Choosethehostcarefullytocontrolaccess.2)SetresourcelimitswithoptionslikeMAX_QUERIES_PER_HOUR.3)Usestrong,uniquepasswords.4)EnforceSSL/TLSconnectionswith

MySQL: How to avoid String Data Types common mistakes?MySQL: How to avoid String Data Types common mistakes?May 13, 2025 am 12:09 AM

ToavoidcommonmistakeswithstringdatatypesinMySQL,understandstringtypenuances,choosetherighttype,andmanageencodingandcollationsettingseffectively.1)UseCHARforfixed-lengthstrings,VARCHARforvariable-length,andTEXT/BLOBforlargerdata.2)Setcorrectcharacters

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Article

Hot Tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SecLists

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.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor