search
HomeDatabaseMysql TutorialMysql实现分区功能(二)_MySQL

bitsCN.com 相信有很多人经常会问同样的一个问题:当 MySQL的总记录数超过了100万后,会出现性能的大幅度下降吗?答案是肯定的,但是性能下 降的比率不一而同,要看系统的架构、应用程序、还有包括索引、服务器硬件等多种因素而定。当有网友问我这个问题的时候,我最常见的回 答就是:分表,可以根据id区间或者时间先后顺序等多种规则来分表。分表很容易,然而由此所带来的应用程序甚至是架构方面的改动工作却不容小觑,还包括将来的扩展性等。

在以前,一种解决方案就是使用 MERGE类型,这是一个非常方便的做饭。架构和程序基本上不用做改动,不过,它的缺点是显见的:

    只能在相同结构的 MyISAM 表上使用无法享受到 MyISAM 的全部功能,例如无法在 MERGE 类型上执行 FULLTEXT 搜索它需要使用更多的文件描述符读取索引更慢

    这个时候,MySQL 5.1 中新增的分区(Partition)功能的优势也就很明显了:

      与单个磁盘或文件系统分区相比,可以存储更多的数据很容易就能删除不用或者过时的数据一些查询可以得到极大的优化涉及到 SUM()/COUNT() 等聚合函数时,可以并行进行IO吞吐量更大

      分区允许可以设置为任意大小的规则,跨文件系统分配单个表的多个部分。实际上,表的不同部分在不同的位置被存储为单独的表。

      二、分区的类型

        RANGE 分区:基于属于一个给定连续区间的列值,把多行分配给分区。LIST 分区:类似于按RANGE分区,区别在于LIST分区是基于列值匹配一个离散值集合中的某个值来进行选择。HASH分区:基于用户定义的表达式的返回值来进行选择的分区,该表达式使用将要插入到表中的这些行的列值进行计算。这个函数可以包>含MySQL中有效的、产生非负整数值的任何表达式。KEY分区:类似于按HASH分区,区别在于KEY分区只支持计算一列或多列,且MySQL服务器提供其自身的哈希函数。必须有一列或多列包含>整数值。

        三、分区例子:

          RANGE 类型
          CREATE TABLE users (    uid INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,    name VARCHAR(30) NOT NULL DEFAULT '',    email VARCHAR(30) NOT NULL DEFAULT '')PARTITION BY RANGE (uid) (    PARTITION p0 VALUES LESS THAN (3000000)    DATA DIRECTORY = '/data0/data'    INDEX DIRECTORY = '/data1/idx',    PARTITION p1 VALUES LESS THAN (6000000)    DATA DIRECTORY = '/data2/data'    INDEX DIRECTORY = '/data3/idx',    PARTITION p2 VALUES LESS THAN (9000000)    DATA DIRECTORY = '/data4/data'    INDEX DIRECTORY = '/data5/idx',    PARTITION p3 VALUES LESS THAN MAXVALUE    DATA DIRECTORY = '/data6/data'    INDEX DIRECTORY = '/data7/idx');

          在这里,将用户表分成4个分区,以每300万条记录为界限,每个分区都有自己独立的数据、索引文件的存放目录,与此同时,这些目录所在的物理磁盘分区可能也都是完全独立的,可以多大提高了磁盘IO吞吐量。

          LIST 类型
          CREATE TABLE category (    cid INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,    name VARCHAR(30) NOT NULL DEFAULT '')PARTITION BY LIST (cid) (    PARTITION p0 VALUES IN (0,4,8,12)    DATA DIRECTORY = '/data0/data'    INDEX DIRECTORY = '/data1/idx',    PARTITION p1 VALUES IN (1,5,9,13)    DATA DIRECTORY = '/data2/data'    INDEX DIRECTORY = '/data3/idx',    PARTITION p2 VALUES IN (2,6,10,14)    DATA DIRECTORY = '/data4/data'    INDEX DIRECTORY = '/data5/idx',    PARTITION p3 VALUES IN (3,7,11,15)    DATA DIRECTORY = '/data6/data'    INDEX DIRECTORY = '/data7/idx');

          分成4个区,数据文件和索引文件单独存放。

          HASH 类型
          CREATE TABLE users (    uid INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,    name VARCHAR(30) NOT NULL DEFAULT '',    email VARCHAR(30) NOT NULL DEFAULT '')PARTITION BY HASH (uid) PARTITIONS 4 (    PARTITION p0    DATA DIRECTORY = '/data0/data'    INDEX DIRECTORY = '/data1/idx',    PARTITION p1    DATA DIRECTORY = '/data2/data'    INDEX DIRECTORY = '/data3/idx',    PARTITION p2    DATA DIRECTORY = '/data4/data'    INDEX DIRECTORY = '/data5/idx',    PARTITION p3    DATA DIRECTORY = '/data6/data'    INDEX DIRECTORY = '/data7/idx');

          分成4个区,数据文件和索引文件单独存放。

          KEY 类型
          REATE TABLE users (    uid INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,    name VARCHAR(30) NOT NULL DEFAULT '',    email VARCHAR(30) NOT NULL DEFAULT '')PARTITION BY KEY (uid) PARTITIONS 4 (    PARTITION p0    DATA DIRECTORY = '/data0/data'    INDEX DIRECTORY = '/data1/idx',    PARTITION p1    DATA DIRECTORY = '/data2/data'    INDEX DIRECTORY = '/data3/idx',    PARTITION p2    DATA DIRECTORY = '/data4/data'    INDEX DIRECTORY = '/data5/idx',    PARTITION p3    DATA DIRECTORY = '/data6/data'    INDEX DIRECTORY = '/data7/idx');

          分成4个区,数据文件和索引文件单独存放。

          子分区
          子分区是针对 RANGE/LIST 类型的分区表中每个分区的再次分割。再次分割可以是 HASH/KEY 等类型。例如:

          CREATE TABLE users (    uid INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,    name VARCHAR(30) NOT NULL DEFAULT '',    email VARCHAR(30) NOT NULL DEFAULT '')PARTITION BY RANGE (uid) SUBPARTITION BY HASH (uid % 4) SUBPARTITIONS 2(    PARTITION p0 VALUES LESS THAN (3000000)    DATA DIRECTORY = '/data0/data'    INDEX DIRECTORY = '/data1/idx',    PARTITION p1 VALUES LESS THAN (6000000)    DATA DIRECTORY = '/data2/data'    INDEX DIRECTORY = '/data3/idx');

          对 RANGE 分区再次进行子分区划分,子分区采用 HASH 类型。

          或者

          CREATE TABLE users (    uid INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,    name VARCHAR(30) NOT NULL DEFAULT '',    email VARCHAR(30) NOT NULL DEFAULT '')PARTITION BY RANGE (uid) SUBPARTITION BY KEY(uid) SUBPARTITIONS 2(    PARTITION p0 VALUES LESS THAN (3000000)    DATA DIRECTORY = '/data0/data'    INDEX DIRECTORY = '/data1/idx',    PARTITION p1 VALUES LESS THAN (6000000)    DATA DIRECTORY = '/data2/data'    INDEX DIRECTORY = '/data3/idx');

          对 RANGE 分区再次进行子分区划分,子分区采用 KEY 类型。

          四、分区管理

          • 删除分区
            ALERT TABLE users DROP PARTITION p0;

            删除分区 p0。

          • 重建分区
              RANGE 分区重建
              ALTER TABLE users REORGANIZE PARTITION p0,p1 INTO (PARTITION p0 VALUES LESS THAN (6000000));

              将原来的 p0,p1 分区合并起来,放到新的 p0 分区中。

              LIST 分区重建
              ALTER TABLE users REORGANIZE PARTITION p0,p1 INTO (PARTITION p0 VALUES IN(0,1,4,5,8,9,12,13));

              将原来的 p0,p1 分区合并起来,放到新的 p0 分区中。

              HASH/KEY 分区重建
              ALTER TABLE users REORGANIZE PARTITION COALESCE PARTITION 2;

              用 REORGANIZE 方式重建分区的数量变成2,在这里数量只能减少不能增加。想要增加可以用 ADD PARTITION 方法。

              新增分区
              • 新增 RANGE 分区
                ALTER TABLE category ADD PARTITION (PARTITION p4 VALUES IN (16,17,18,19)DATA DIRECTORY = '/data8/data'INDEX DIRECTORY = '/data9/idx');

                新增一个RANGE分区。

                新增 HASH/KEY 分区
                ALTER TABLE users ADD PARTITION PARTITIONS 8;

                将分区总数扩展到8个。

                  新增 HASH/KEY 分区
                  ALTER TABLE users ADD PARTITION PARTITIONS 8;

                  将分区总数扩展到8个。

                  bitsCN.com
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
How does MySQL differ from SQLite?How does MySQL differ from SQLite?Apr 24, 2025 am 12:12 AM

The main difference between MySQL and SQLite is the design concept and usage scenarios: 1. MySQL is suitable for large applications and enterprise-level solutions, supporting high performance and high concurrency; 2. SQLite is suitable for mobile applications and desktop software, lightweight and easy to embed.

What are indexes in MySQL, and how do they improve performance?What are indexes in MySQL, and how do they improve performance?Apr 24, 2025 am 12:09 AM

Indexes in MySQL are an ordered structure of one or more columns in a database table, used to speed up data retrieval. 1) Indexes improve query speed by reducing the amount of scanned data. 2) B-Tree index uses a balanced tree structure, which is suitable for range query and sorting. 3) Use CREATEINDEX statements to create indexes, such as CREATEINDEXidx_customer_idONorders(customer_id). 4) Composite indexes can optimize multi-column queries, such as CREATEINDEXidx_customer_orderONorders(customer_id,order_date). 5) Use EXPLAIN to analyze query plans and avoid

Explain how to use transactions in MySQL to ensure data consistency.Explain how to use transactions in MySQL to ensure data consistency.Apr 24, 2025 am 12:09 AM

Using transactions in MySQL ensures data consistency. 1) Start the transaction through STARTTRANSACTION, and then execute SQL operations and submit it with COMMIT or ROLLBACK. 2) Use SAVEPOINT to set a save point to allow partial rollback. 3) Performance optimization suggestions include shortening transaction time, avoiding large-scale queries and using isolation levels reasonably.

In what scenarios might you choose PostgreSQL over MySQL?In what scenarios might you choose PostgreSQL over MySQL?Apr 24, 2025 am 12:07 AM

Scenarios where PostgreSQL is chosen instead of MySQL include: 1) complex queries and advanced SQL functions, 2) strict data integrity and ACID compliance, 3) advanced spatial functions are required, and 4) high performance is required when processing large data sets. PostgreSQL performs well in these aspects and is suitable for projects that require complex data processing and high data integrity.

How can you secure a MySQL database?How can you secure a MySQL database?Apr 24, 2025 am 12:04 AM

The security of MySQL database can be achieved through the following measures: 1. User permission management: Strictly control access rights through CREATEUSER and GRANT commands. 2. Encrypted transmission: Configure SSL/TLS to ensure data transmission security. 3. Database backup and recovery: Use mysqldump or mysqlpump to regularly backup data. 4. Advanced security policy: Use a firewall to restrict access and enable audit logging operations. 5. Performance optimization and best practices: Take into account both safety and performance through indexing and query optimization and regular maintenance.

What are some tools you can use to monitor MySQL performance?What are some tools you can use to monitor MySQL performance?Apr 23, 2025 am 12:21 AM

How to effectively monitor MySQL performance? Use tools such as mysqladmin, SHOWGLOBALSTATUS, PerconaMonitoring and Management (PMM), and MySQL EnterpriseMonitor. 1. Use mysqladmin to view the number of connections. 2. Use SHOWGLOBALSTATUS to view the query number. 3.PMM provides detailed performance data and graphical interface. 4.MySQLEnterpriseMonitor provides rich monitoring functions and alarm mechanisms.

How does MySQL differ from SQL Server?How does MySQL differ from SQL Server?Apr 23, 2025 am 12:20 AM

The difference between MySQL and SQLServer is: 1) MySQL is open source and suitable for web and embedded systems, 2) SQLServer is a commercial product of Microsoft and is suitable for enterprise-level applications. There are significant differences between the two in storage engine, performance optimization and application scenarios. When choosing, you need to consider project size and future scalability.

In what scenarios might you choose SQL Server over MySQL?In what scenarios might you choose SQL Server over MySQL?Apr 23, 2025 am 12:20 AM

In enterprise-level application scenarios that require high availability, advanced security and good integration, SQLServer should be chosen instead of MySQL. 1) SQLServer provides enterprise-level features such as high availability and advanced security. 2) It is closely integrated with Microsoft ecosystems such as VisualStudio and PowerBI. 3) SQLServer performs excellent in performance optimization and supports memory-optimized tables and column storage indexes.

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 Tools

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

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.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)