Friends of Pythian Referral Program - Earn up to $5000!

Using Transportable Tablespaces with Partitioned Tables

Posted in: MySQL, Technical Track

Overview

In this post I will explain how to transfer a partitioned table using transportable tablespaces on MySQL 5.6. Note that the operation is not officially supported until MySQL 5.7, as the discard tablespace operation will fail if it is run against a partitioned table on MySQL 5.6.

The workaround is transferring each individual partition as if it was a standalone table. Continue reading to learn more about the detailed procedure.

Steps

1. Create the same table structure on the destination server:

[root@mysqlsandbox ~]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.35-80.0 Percona Server (GPL), Release 80.0, Revision f113994f31
 
Copyright (c) 2009-2016 Percona LLC and/or its affiliates
Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
 
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
 
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
 
mysql> use test;
Database changed
mysql> create table t ( a int, b varchar(50), c varchar(10),   `created` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
    -> PRIMARY KEY (a, created)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=COMPACT
    -> /*!50100 PARTITION BY RANGE (TO_DAYS(created))
    -> (PARTITION p267 VALUES LESS THAN (736772) ENGINE = InnoDB,
    ->  PARTITION p268 VALUES LESS THAN (736779) ENGINE = InnoDB,
    ->  PARTITION p269 VALUES LESS THAN (736786) ENGINE = InnoDB) */
    -> ;
Query OK, 0 rows affected (0.15 sec)

2. On the destination server, you will also create one non-partitioned table with the same structure as the table we want to move, for each partition to be transported e.g.:

mysql> CREATE TABLE t_p267 LIKE t;
Query OK, 0 ROWS affected (0.04 sec)
 
mysql> CREATE TABLE t_p268 LIKE t;
Query OK, 0 ROWS affected (0.04 sec)
 
mysql> CREATE TABLE t_p269 LIKE t;
Query OK, 0 ROWS affected (0.03 sec)
 
mysql> ALTER TABLE t_p267 remove partitioning;
Query OK, 0 ROWS affected (0.45 sec)
Records: 0  Duplicates: 0  Warnings: 0
 
mysql> ALTER TABLE t_p268 remove partitioning;
Query OK, 0 ROWS affected (0.18 sec)
Records: 0  Duplicates: 0  Warnings: 0
 
mysql> ALTER TABLE t_p269 remove partitioning;
Query OK, 0 ROWS affected (0.14 sec)
Records: 0  Duplicates: 0  Warnings: 0

3. Discard the tablespace for each of the tables created above on the destination server:

mysql> ALTER TABLE t_p267 discard tablespace;
Query OK, 0 ROWS affected (0.01 sec)
 
mysql> ALTER TABLE t_p268 discard tablespace;
Query OK, 0 ROWS affected (0.01 sec)
 
mysql> ALTER TABLE t_p269 discard tablespace;
Query OK, 0 ROWS affected (0.01 sec)

4. Prepare table for export on source server. Word of warning: this will lock the table for writes. If the copy process is expected to take more than your configured value of wait_timeout parameter, it is be a good idea to increase it for the session beforehand:

mysql> SET SESSION wait_timeout=28800;
mysql> FLUSH TABLES t FOR export;
Query OK, 0 ROWS affected (0.02 sec)

5. On a different terminal (to ensure the flush still going) copy each of the partitions cfg and ibd files to be moved and name them accordingly on the destination:

[root@mysqlsandbox test]# scp t#P#p267.cfg dest:/var/lib/mysql/test/t_p267.cfg
[root@mysqlsandbox test]# scp t#P#p267.ibd dest:/var/lib/mysql/test_p267.ibd
[root@mysqlsandbox test]# scp t#P#p268.cfg dest:/var/lib/mysql/test/t_p268.cfg
[root@mysqlsandbox test]# scp t#P#p268.ibd dest:/var/lib/mysql/test_p268.ibd
[root@mysqlsandbox test]# scp t#P#p269.cfg dest:/var/lib/mysql/test/t_p269.cfg
[root@mysqlsandbox test]# scp t#P#p269.ibd dest:/var/lib/mysql/test_p269.ibd

6. Unlock tables on source server:

UNLOCK TABLES;

7. Import each partition’s tablespace using the table you created to pivot. This step can take several minutes if the partitions are big.

mysql> ALTER TABLE t_p267 import tablespace;
Query OK, 0 ROWS affected (0.10 sec)
 
mysql> ALTER TABLE t_p268 import tablespace;
Query OK, 0 ROWS affected (0.04 sec)
 
mysql> ALTER TABLE t_p269 import tablespace;
Query OK, 0 ROWS affected (0.03 sec)

8. Exchange the tables with the corresponding partition of the target table:

mysql> ALTER TABLE t exchange partition p267 WITH TABLE t_p267;
Query OK, 0 ROWS affected (0.10 sec)
 
mysql> ALTER TABLE t exchange partition p268 WITH TABLE t_p268;
Query OK, 0 ROWS affected (0.03 sec)
 
mysql> ALTER TABLE t exchange partition p269 WITH TABLE t_p269;
Query OK, 0 ROWS affected (0.04 sec)

9. Check the results and the error log. You should see a similar output:

mysql> SELECT partition_name, table_rows FROM information_schema.partitions WHERE TABLE_NAME='t' AND table_schema='test';
+----------------+------------+
| partition_name | table_rows |
+----------------+------------+
| p267           |      10789 |
| p268           |          1 |
| p269           |          1 |
+----------------+------------+
3 ROWS IN SET (0.00 sec)
 
mysql> system less /var/log/mysqld.log
...
2017-04-05 16:29:01 1245 [Note] InnoDB: Importing tablespace FOR TABLE 'test/t#P#p267' that was exported FROM host 'mysqlsandbox.dev'
2017-04-05 16:29:01 1245 [Note] InnoDB: Phase I - UPDATE ALL pages
2017-04-05 16:29:01 1245 [Note] InnoDB: Sync TO disk
2017-04-05 16:29:01 1245 [Note] InnoDB: Sync TO disk - done!
2017-04-05 16:29:01 1245 [Note] InnoDB: Phase III - FLUSH changes TO disk
2017-04-05 16:29:01 1245 [Note] InnoDB: Phase IV - FLUSH complete
2017-04-05 16:29:05 1245 [Note] InnoDB: Importing tablespace FOR TABLE 'test/t#P#p268' that was exported FROM host 'mysqlsandbox.dev'
2017-04-05 16:29:05 1245 [Note] InnoDB: Phase I - UPDATE ALL pages
2017-04-05 16:29:05 1245 [Note] InnoDB: Sync TO disk
2017-04-05 16:29:05 1245 [Note] InnoDB: Sync TO disk - done!
2017-04-05 16:29:05 1245 [Note] InnoDB: Phase III - FLUSH changes TO disk
2017-04-05 16:29:05 1245 [Note] InnoDB: Phase IV - FLUSH complete
2017-04-05 16:29:08 1245 [Note] InnoDB: Importing tablespace FOR TABLE 'test/t#P#p269' that was exported FROM host 'mysqlsandbox.dev'
2017-04-05 16:29:08 1245 [Note] InnoDB: Phase I - UPDATE ALL pages
2017-04-05 16:29:08 1245 [Note] InnoDB: Sync TO disk
2017-04-05 16:29:08 1245 [Note] InnoDB: Sync TO disk - done!
2017-04-05 16:29:08 1245 [Note] InnoDB: Phase III - FLUSH changes TO disk
2017-04-05 16:29:08 1245 [Note] InnoDB: Phase IV - FLUSH complete

10. Drop tables used to pivot on the target server:

mysql> DROP TABLE t_p267;
Query OK, 0 ROWS affected (0.00 sec)
 
mysql> DROP TABLE t_p268;
Query OK, 0 ROWS affected (0.01 sec)
 
mysql> DROP TABLE t_p269;
Query OK, 0 ROWS affected (0.01 sec)
email

Interested in working with Ivan? Schedule a tech call.

Lead Database Consultant

No comments

Leave a Reply

Your email address will not be published. Required fields are marked *