Detailed example of MySQL exchange partition

Detailed example of MySQL exchange partition

Detailed example of MySQL exchange partition

Preface

Before introducing the exchange partition, let's first understand the mysql partition.

There are two types of database partitioning: horizontal partitioning and vertical partitioning. MySQL does not currently support vertical partitioning, so what follows is all about horizontal partitioning. Horizontal partitioning means partitioning the table in rows. For example: partition by time, one partition for each year, etc.

In MySQL, partitions are exchangeable, and the data in a partition of a partition table can be interchanged with that in a normal table.

Implementation of swap partition

1. Syntax of swap partition

alter table pt exchange partition p with table nt;

explain:

Exchange the data in partition p of partition table pt and a normal table nt.

Prerequisites for exchange:

  • The ordinary table nt is not a temporary table and is not a partitioned table.
  • The table structure is consistent with the structure of the partition table pt.
  • The normal table nt has no foreign key references.
  • If the general table nt is not empty. In MySQL 5.6 and earlier versions, the data must be within the partition range. In MySQL 5.7 and later, the data can be outside the partition range and still be successfully stored in the partition.

2. Experimental swap partition

(1) Create a partition table and insert data

# Create a partition table
CREATE TABLE e (
  id INT NOT NULL,
  fname VARCHAR(30),
  lname VARCHAR(30)
)
  PARTITION BY RANGE (id) (
    PARTITION p0 VALUES LESS THAN (50),
    PARTITION p1 VALUES LESS THAN (100),
    PARTITION p2 VALUES LESS THAN (150),
    PARTITION p3 VALUES LESS THAN (MAXVALUE)
);

# Insert several records into partition table e INSERT INTO e VALUES
  (1669, "Jim", "Smith"),
  (337, "Mary", "Jones"),
  (16, "Frank", "White"),
  (2005, "Linda", "Black");

(2) Create a common table e2 with the same structure as partition table e

# Create a table e2
mysql> create table e2 like e;

# Delete the partition of table e2 to make it a normal table mysql> alter table e2 remove partitioning;

(3) Check how many rows there are in each partition of table e

mysql> select PARTITION_NAME, TABLE_ROWS
  -> FROM INFORMATION_SCHEMA.PARTITIONS
  -> WHERE TABLE_NAME = 'e';
+----------------+------------+
| PARTITION_NAME | TABLE_ROWS |
+----------------+------------+
| p0 | 1 |
| p1 | 0 |
| p2 | 0 |
| p3 | 3 |
+----------------+------------+
4 rows in set (0.00 sec)

(4) Exchange partition p0 of table e to ordinary table e2

mysql> alter table e exchange partition p0 with table e2;

(5) View the results

# The data in partition P0 of table e is gonemysql> SELECT PARTITION_NAME, TABLE_ROWS
  -> FROM INFORMATION_SCHEMA.PARTITIONS
  -> WHERE TABLE_NAME = 'e';
+----------------+------------+
| PARTITION_NAME | TABLE_ROWS |
+----------------+------------+
| p0 | 0 |
| p1 | 0 |
| p2 | 0 |
| p3 | 3 |
+----------------+------------+
4 rows in set (0.00 sec)

# In table e2, there is a record from partition p0 of table emysql> SELECT * FROM e2;
+----+-------+-------+
| id | fname | lname |
+----+-------+-------+
| 16 | Frank | White |
+----+-------+-------+
1 row in set (0.00 sec)

It can be found that the data of partition p0 of table e has been moved to table e2.

The above example exchanges a partition with an ordinary empty table, which is equivalent to moving the data of a partition out. It is usually used in data archiving.

Similarly, the exchange partition can also be exchanged between a partition and a non-empty ordinary table, so that the data in the ordinary table will be moved to the specified partition, and the data in the specified partition will be moved to the ordinary table. Do as follows:

(6) Add another piece of data to p0 of partition table e

# Add a record in partition p0 to table e mysql> INSERT INTO e VALUES (41, "Michael", "Green");      
Query OK, 1 row affected (0.05 sec)               

# Confirm that the inserted data is indeed stored in partition p0
mysql> SELECT PARTITION_NAME, TABLE_ROWS
  -> FROM INFORMATION_SCHEMA.PARTITIONS
  -> WHERE TABLE_NAME = 'e';      
+----------------+------------+       
| PARTITION_NAME | TABLE_ROWS |       
+----------------+------------+       
| p0 | 1 |       
| p1 | 0 |       
| p2 | 0 |       
| p3 | 3 |       
+----------------+------------+       
4 rows in set (0.00 sec)

(7) Execute swap partition

​mysql> alter table e exchange partition p0 with table e2;
Query OK, 0 rows affected (0.28 sec)

(8) View the results

# Previously, p0 of table e was exchanged to a piece of data in table e2, and now it is back to table e
mysql> SELECT * FROM e;
+------+-------+-------+
| id | fname | lname |
+------+-------+-------+
| 16 | Frank | White |   
| 1669 | Jim | Smith |
| 337 | Mary | Jones |
| 2005 | Linda | Black |
+------+-------+-------+
4 rows in set (0.00 sec)

mysql> SELECT * FROM e2;
+----+---------+-------+
| id | fname | lname |
+----+---------+-------+
| 41 | Michael | Green |
+----+---------+-------+
1 row in set (0.00 sec)

From this we can see that the data in table e's p0 and table e2 have been swapped! This scenario can be used when moving data from a common table to a partition in a partitioned table .

However, there is one thing to note:

If the data of the common table is not within the range of the partition to be exchanged, the above syntax for exchanging partitions cannot be executed successfully! Please see the operation below.

(9) Insert a piece of data that exceeds the p0 range of table e into the ordinary table e2 and execute the above exchange statement

mysql> INSERT INTO e2 VALUES (51, "Ellen", "McDonald");
Query OK, 1 row affected (0.08 sec)

mysql> alter table e exchange partition p0 with table e2;
ERROR 1707 (HY000): Found row that does not match the partition

In MySQL 5.6, this problem cannot be solved; in MySQL 5.7.5, an option WITHOUT VALIDATION is added to solve the above error. The operation is as follows:

mysql> ALTER TABLE e EXCHANGE PARTITION p0 WITH TABLE e2 WITHOUT VALIDATION;
Query OK, 0 rows affected (0.02 sec)

View the results:

# The data with id 51 is also stored in p0 of table e mysql> select * from e partition(p0);
+------+---------+----------+
| id | fname | lname |
+------+---------+----------+
| 41 | Michael | Green |
| 51 | Ellen | McDonald | 
+------+---------+----------+

mysql> SELECT * FROM e2;
+----+---------+-------+
| id | fname | lname |
+----+---------+-------+
| 16 | Frank | White |
+----+---------+-------+
1 row in set (0.00 sec)

Summarize

Through the above practical operations, we found that the MySQL exchange partition function has two applicable scenarios:

A partition of a partition table is exchanged with an empty ordinary table so that the data of the partition of the partition table is moved out.
Exchange a partition of a partition table with a non-empty ordinary table so that the data of the ordinary table is moved to the specified partition of the partition table.

Thank you for reading, I hope it can help you, thank you for your support of this site!

You may also be interested in:
  • A simple way to change the password of mysql just installed in Linux
  • CentOS 6.6 source code compilation and installation of MySQL 5.7.18 tutorial detailed explanation
  • Solution to MySql Error 1698 (28000)
  • Solution to the problem that the number of MySQL connections is limited to 214 in CentOS 7
  • Analyzing the troublesome Aborted warning in MySQL through case studies
  • Solve the problem that IN subquery in MySQL will cause the index to be unusable

<<:  Summary of common problems and solutions in Vue (recommended)

>>:  CentOS7 deployment Flask (Apache, mod_wsgi, Python36, venv)

Recommend

Detailed explanation of how to view the current number of MySQL connections

1. View the detailed information of all current c...

Detailed installation process of mysql5.7.21 under win10

This article shares the installation of MySQL 5.7...

How to use Element in React project

This is my first time using the element framework...

Implementation of docker-compose deployment of zk+kafka+storm cluster

Cluster Deployment Overview 172.22.12.20 172.22.1...

JavaScript offsetParent case study

1. Definition of offsetParent: offsetParent is th...

Detailed explanation of Nginx timed log cutting

Preface By default, Nginx logs are written to a f...

Example analysis of interval calculation of mysql date and time

This article uses an example to describe the inte...

Tutorial on how to install htop on CentOS 8

If you are looking to monitor your system interac...

How to configure Hexo and GitHub to bind a custom domain name under Windows 10

Hexo binds a custom domain name to GitHub under W...

Web Design Tutorial (1): Steps and Overall Layout

<br /> Note: All texts, except those indicat...

WeChat Mini Program User Authorization Best Practices Guide

Preface When developing WeChat applets, you often...

Introduction to MySQL method of deleting table data with foreign key constraints

When deleting a table or a piece of data in MySQL...

MySQL Series Database Design Three Paradigm Tutorial Examples

Table of contents 1. Knowledge description of the...

Detailed explanation of count without filter conditions in MySQL

count(*) accomplish 1. MyISAM: Stores the total n...