Home Database Mysql Tutorial MySQL分区表的使用_MySQL

MySQL分区表的使用_MySQL

Jun 01, 2016 pm 01:26 PM
server driver

bitsCN.com

MySQL使用分区表的好处:

1,可以把一些归类的数据放在一个分区中,可以减少服务器检查数据的数量加快查询。2,方便维护,通过删除分区来删除老的数据。3,分区数据可以被分布到不同的物理位置,可以做分布式有效利用多个硬盘驱动器。
MySQL可以建立四种分区类型的分区:          RANGE 分区:基于属于一个给定连续区间的列值,把多行分配给分区。·         LIST 分区:类似于按RANGE分区,区别在于LIST分区是基于列值匹配一个离散值集合中的某个值来进行选择。  www.bitsCN.com  ·         HASH分区:基于用户定义的表达式的返回值来进行选择的分区,该表达式使用将要插入到表中的这些行的列值进行计算。这个函数可以包含MySQL 中有效的、产生非负整数值的任何表达式。·         KEY 分区:类似于按HASH分区,区别在于KEY分区只支持计算一列或多列,且MySQL 服务器提供其自身的哈希函数。必须有一列或多列包含整数值。一般用得多的是range分区和list分区。RANGE分区这里以一个销售的业务来做测试销售表有日期/商品/销售额三个字段测试数据从2010年1月1日至2010年9月31日以“月”为单位进行分区初期分区定义首先需要查看,当前数据库是否支持分区mysql>SHOW VARIABLES LIKE '%partition%';+-------------------+-------+| Variable_name     | Value |     +-------------------+-------+| have_partitioning | YES   | +-------------------+-------+1 row in set (0.03 sec) 创建分区表,按照年月的方式分区。
 1 mysql> CREATE TABLE sale_data ( 2     ->   sale_date  DATETIME NOT NULL, 3   4     ->   sale_item  VARCHAR(2) NOT NULL , 5   6     ->   sale_money DECIMAL(10,2) NOT NULL 7   8     -> )  www.bitsCN.com   9  10     -> PARTITION BY RANGE (YEAR(sale_date)*100+MONTH(sale_date)) (11  12     ->   PARTITION p201001 VALUES LESS THAN (201002),13  14     ->   PARTITION p201002 VALUES LESS THAN (201003),15  16     ->   PARTITION p201003 VALUES LESS THAN (201004),17  18     ->   PARTITION p201004 VALUES LESS THAN (201005),19  20     ->   PARTITION p201005 VALUES LESS THAN (201006),21  22     ->   PARTITION p201006 VALUES LESS THAN (201007),23  24     ->   PARTITION p201007 VALUES LESS THAN (201008),25  26     ->   PARTITION p201008 VALUES LESS THAN (201009),27  28     ->   PARTITION p201009 VALUES LESS THAN (201010),29  30     ->   PARTITION pcatchall VLAUES LESS THAN MAXVALUE31     -> );32  33 Query OK, 0 rows affected (0.20 sec)
Copy after login

新增分区

mysql> ALTER TABLE sale_data    ->   ADD PARTITION (PARTITION p201010 VALUES LESS THAN (201011)); Query OK, 0 rows affected (0.36 sec)Records: 0  Duplicates: 0  Warnings: 0
Copy after login

删除分区

--当删除了一个分区,也同时删除了该分区中所有的数据。mysql> ALTER TABLE sale_data DROP PARTITION p201010;Query OK, 0 rows affected (0.22 sec)  www.bitsCN.com  Records: 0  Duplicates: 0  Warnings: 0
Copy after login

分区的合并

下面的SQL,将p201001 - p201009 合并为3个分区p2010Q1 - p2010Q3

mysql> ALTER TABLE sale_data    ->   REORGANIZE PARTITION p201001,p201002,p201003,     ->                        p201004,p201005,p201006,     ->                        p201007,p201008,p201009 INTO     -> (     ->   PARTITION p2010Q1 VALUES LESS THAN (201004),     ->   PARTITION p2010Q2 VALUES LESS THAN (201007),     ->   PARTITION p2010Q3 VALUES LESS THAN (201010)     -> ); Query OK, 0 rows affected (1.14 sec)Records: 0  Duplicates: 0  Warnings: 0
Copy after login

分区的拆分

下面的SQL,将p2010Q1 分区,拆分为s2009 与s2010 两个分区

mysql> ALTER TABLE sale_data REORGANIZE PARTITION p2010Q1 INTO (     ->     PARTITION s2009 VALUES LESS THAN (201001),             www.bitsCN.com      ->     PARTITION s2010 VALUES LESS THAN (201004)     -> ); Query OK, 0 rows affected (0.36 sec)Records: 0  Duplicates: 0  Warnings: 0
Copy after login

一个利用不同物理位置数据源做分区的例子:

CREATE TABLE ts (id INT, purchased DATE)     ENGINE=innodb     PARTITION BY RANGE(YEAR(purchased))     SUBPARTITION BY HASH(id)     (         PARTITION p0 VALUES LESS THAN (1990)         (             SUBPARTITION s0                  //在大的分区下又有小的分区            DATA DIRECTORY='/usr/local/mysql/data0'      //数据源            INDEX DIRECTORY='/usr/local/mysql/index0',   //索引数据源            SUBPARTITION s1             DATA DIRECTORY='/usr/local/mysql/data1'             INDEX DIRECTORY='/usr/local/mysql/index1'         ),         PARTITION p1 VALUES LESS THAN (MAXVALUE)         (             SUBPARTITION s2             DATA DIRECTORY='/usr/local/mysql/data1'             INDEX DIRECTORY='/usr/local/mysql/index1',             SUBPARTITION s3             DATA DIRECTORY='/usr/local/mysql/data2'             INDEX DIRECTORY='/usr/local/mysql/index2'         )     ); 
Copy after login

 

分区索引的局限:1,所有分区都要使用同样的引擎。2,分区表的每一个唯一索引必须包含由分区函数引用的列。3,mysql能避免查询所有的分区,但仍然锁定了所有分区。4,分区函数能使用的函数和表达式有限,例如函数有上面的4种。5,分区不支持外键。  www.bitsCN.com  6,不能使用LOAD INDEX INTO CACHE7,分区并不能总是改善性能,要进行性能评测。例如可以使用expalin partitions 来查看查询语句是否使用分区过滤了数据:mysql> explain partitions select * from fenqubiao where day
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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

How to update network drive login credentials in Windows 11 How to update network drive login credentials in Windows 11 Feb 19, 2024 pm 12:18 PM

To enhance network drive security, Microsoft supports password-protecting shared folders or network drives. However, to ensure continued protection, we need to change the password for the network drive regularly. This article will introduce how to update the login credentials of a network drive in Windows 11/10. How to Update Network Drive Login Credentials in Windows In order to update the network driver’s login credentials, we need to use Credential Manager. This is a pre-installed feature on Windows computers and can be accessed through the Control Panel. So, follow the steps below to perform the same. Click Win+S, search for "Control Panel" and click "Open". Change the screen view proportionally to large icons.

How to solve the problem that eMule search cannot connect to the server How to solve the problem that eMule search cannot connect to the server Jan 25, 2024 pm 02:45 PM

Solution: 1. Check the eMule settings to make sure you have entered the correct server address and port number; 2. Check the network connection, make sure the computer is connected to the Internet, and reset the router; 3. Check whether the server is online. If your settings are If there is no problem with the network connection, you need to check whether the server is online; 4. Update the eMule version, visit the eMule official website, and download the latest version of the eMule software; 5. Seek help.

Solution to the inability to connect to the RPC server and the inability to enter the desktop Solution to the inability to connect to the RPC server and the inability to enter the desktop Feb 18, 2024 am 10:34 AM

What should I do if the RPC server is unavailable and cannot be accessed on the desktop? In recent years, computers and the Internet have penetrated into every corner of our lives. As a technology for centralized computing and resource sharing, Remote Procedure Call (RPC) plays a vital role in network communication. However, sometimes we may encounter a situation where the RPC server is unavailable, resulting in the inability to enter the desktop. This article will describe some of the possible causes of this problem and provide solutions. First, we need to understand why the RPC server is unavailable. RPC server is a

Detailed explanation of CentOS installation fuse and CentOS installation server Detailed explanation of CentOS installation fuse and CentOS installation server Feb 13, 2024 pm 08:40 PM

As a LINUX user, we often need to install various software and servers on CentOS. This article will introduce in detail how to install fuse and set up a server on CentOS to help you complete the related operations smoothly. CentOS installation fuseFuse is a user space file system framework that allows unprivileged users to access and operate the file system through a customized file system. Installing fuse on CentOS is very simple, just follow the following steps: 1. Open the terminal and Log in as root user. 2. Use the following command to install the fuse package: ```yuminstallfuse3. Confirm the prompts during the installation process and enter `y` to continue. 4. Installation completed

Enable 256-bit Bitlocker encryption on Windows 11 for increased security Enable 256-bit Bitlocker encryption on Windows 11 for increased security Nov 26, 2023 am 11:21 AM

Bitlocker is the default encryption technology for Windows operating systems. It is widely used on Windows, but some users prefer third-party solutions such as VeraCrypt. What many users of Bitlocker don't know is that it defaults to 128-bit encryption, even though 256-bit is available. Without going into too much detail about the differences; the core difference between AES 128-bit and 256-bit encryption is the length of the security key. Longer keys make brute force attacks more difficult. While the default is 128-bit, even Microsoft recommends 256-bit for better security. The problem is, most users probably don't know about the weaker defaults or how to change them. First, you might want to know W

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

Best Practice Guide for Building IP Proxy Servers with PHP Best Practice Guide for Building IP Proxy Servers with PHP Mar 11, 2024 am 08:36 AM

In network data transmission, IP proxy servers play an important role, helping users hide their real IP addresses, protect privacy, and improve access speeds. In this article, we will introduce the best practice guide on how to build an IP proxy server with PHP and provide specific code examples. What is an IP proxy server? An IP proxy server is an intermediate server located between the user and the target server. It acts as a transfer station between the user and the target server, forwarding the user's requests and responses. By using an IP proxy server

What should I do if I can't enter the game when the epic server is offline? Solution to why Epic cannot enter the game offline What should I do if I can't enter the game when the epic server is offline? Solution to why Epic cannot enter the game offline Mar 13, 2024 pm 04:40 PM

What should I do if I can’t enter the game when the epic server is offline? This problem must have been encountered by many friends. When this prompt appears, the genuine game cannot be started. This problem is usually caused by interference from the network and security software. So how should it be solved? The editor of this issue will explain I would like to share the solution with you, I hope today’s software tutorial can help you solve the problem. What to do if the epic server cannot enter the game when it is offline: 1. It may be interfered by security software. Close the game platform and security software and then restart. 2. The second is that the network fluctuates too much. Try restarting the router to see if it works. If the conditions are OK, you can try to use the 5g mobile network to operate. 3. Then there may be more

See all articles