Table of Contents
二、分区的类型
三、分区例子:
四、分区管理
Home Database Mysql Tutorial Mysql实现分区功能(二)_MySQL

Mysql实现分区功能(二)_MySQL

May 27, 2016 pm 02:12 PM
app server hardware

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');
          Copy after login

          在这里,将用户表分成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');
          Copy after login

          分成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');
          Copy after login

          分成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');
          Copy after login

          分成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');
          Copy after login

          对 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');
          Copy after login

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

          四、分区管理

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

            删除分区 p0。

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

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

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

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

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

              用 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');
                Copy after login

                新增一个RANGE分区。

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

                将分区总数扩展到8个。

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

                  将分区总数扩展到8个。

                  bitsCN.com
Statement of this Website
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

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

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Unable to save changes to Photos app error in Windows 11 Unable to save changes to Photos app error in Windows 11 Mar 04, 2024 am 09:34 AM

If you encounter the Unable to save changes error while using the Photos app for image editing in Windows 11, this article will provide you with solutions. Unable to save changes. An error occurred while saving. Please try again later. This problem usually occurs due to incorrect permission settings, file corruption, or system failure. So, we’ve done some deep research and compiled some of the most effective troubleshooting steps to help you resolve this issue and ensure you can continue to use the Microsoft Photos app seamlessly on your Windows 11 device. Fix Unable to Save Changes to Photos App Error in Windows 11 Many users have been talking about Microsoft Photos app error on different forums

Photos cannot open this file because the format is not supported or the file is corrupted Photos cannot open this file because the format is not supported or the file is corrupted Feb 22, 2024 am 09:49 AM

In Windows, the Photos app is a convenient way to view and manage photos and videos. Through this application, users can easily access their multimedia files without installing additional software. However, sometimes users may encounter some problems, such as encountering a "This file cannot be opened because the format is not supported" error message when using the Photos app, or file corruption when trying to open photos or videos. This situation can be confusing and inconvenient for users, requiring some investigation and fixes to resolve the issues. Users see the following error when they try to open photos or videos on the Photos app. Sorry, Photos cannot open this file because the format is not currently supported, or the file

How to connect Apple Vision Pro to PC How to connect Apple Vision Pro to PC Apr 08, 2024 pm 09:01 PM

The Apple Vision Pro headset is not natively compatible with computers, so you must configure it to connect to a Windows computer. Since its launch, Apple Vision Pro has been a hit, and with its cutting-edge features and extensive operability, it's easy to see why. Although you can make some adjustments to it to suit your PC, and its functionality depends heavily on AppleOS, so its functionality will be limited. How do I connect AppleVisionPro to my computer? 1. Verify system requirements You need the latest version of Windows 11 (Custom PCs and Surface devices are not supported) Support 64-bit 2GHZ or faster fast processor High-performance GPU, most

MS Paint not working properly in Windows 11 MS Paint not working properly in Windows 11 Mar 09, 2024 am 09:52 AM

Microsoft Paint not working in Windows 11/10? Well, this seems to be a common problem and we have some great solutions to fix it. Users have been complaining that when trying to use MSPaint, it doesn't work or open. Scrollbars in the app don't work, paste icons don't show up, crashes, etc. Luckily, we've collected some of the most effective troubleshooting methods to help you resolve issues with Microsoft Paint app. Why doesn't Microsoft Paint work? Some possible reasons why MSPaint is not working on Windows 11/10 PC are as follows: The security identifier is corrupted. hung system

Fix caa90019 Microsoft Teams error Fix caa90019 Microsoft Teams error Feb 19, 2024 pm 02:30 PM

Many users have been complaining about encountering error code caa90019 every time they try to log in using Microsoft Teams. Even though this is a convenient communication app, this mistake is very common. Fix Microsoft Teams Error: caa90019 In this case, the error message displayed by the system is: "Sorry, we are currently experiencing a problem." We have prepared a list of ultimate solutions that will help you resolve Microsoft Teams error caa90019. Preliminary steps Run as administrator Clear Microsoft Teams application cache Delete settings.json file Clear Microsoft from Credential Manager

Shazam app not working in iPhone: Fix Shazam app not working in iPhone: Fix Jun 08, 2024 pm 12:36 PM

Having issues with the Shazam app on iPhone? Shazam helps you find songs by listening to them. However, if Shazam isn't working properly or doesn't recognize the song, you'll have to troubleshoot it manually. Repairing the Shazam app won't take long. So, without wasting any more time, follow the steps below to resolve issues with Shazam app. Fix 1 – Disable Bold Text Feature Bold text on iPhone may be the reason why Shazam is not working properly. Step 1 – You can only do this from your iPhone settings. So, open it. Step 2 – Next, open the “Display & Brightness” settings there. Step 3 – If you find that “Bold Text” is enabled

High CPU usage of Feature Access Manager service in Windows 11 High CPU usage of Feature Access Manager service in Windows 11 Feb 19, 2024 pm 03:06 PM

Some PC users and gamers may experience abnormally high CPU usage when using Windows 11 or Windows 10, especially when running certain applications or games. This article provides some suggestions to help users alleviate this problem. Some affected PC users noted that when experiencing this issue, they observed Task Manager showing other applications using only 0% to 5% of the CPU, while the Service Host: Capability Access Manager service was seeing usage as high as 80%. % to 100%. What is the Service Host: Feature Access Manager service? The function of the Function Access Manager service is to confirm whether the application has permission to access the camera and microphone and grant the necessary permissions. It facilitates the management of UWP applications

How to configure Dnsmasq as a DHCP relay server How to configure Dnsmasq as a DHCP relay server Mar 21, 2024 am 08:50 AM

The role of a DHCP relay is to forward received DHCP packets to another DHCP server on the network, even if the two servers are on different subnets. By using a DHCP relay, you can deploy a centralized DHCP server in the network center and use it to dynamically assign IP addresses to all network subnets/VLANs. Dnsmasq is a commonly used DNS and DHCP protocol server that can be configured as a DHCP relay server to help manage dynamic host configurations in the network. In this article, we will show you how to configure dnsmasq as a DHCP relay server. Content Topics: Network Topology Configuring Static IP Addresses on a DHCP Relay D on a Centralized DHCP Server

See all articles