Rechercher dans le manuel MySQL

23.3.3 Exchanging Partitions and Subpartitions with Tables

In MySQL 8.0, it is possible to exchange a table partition or subpartition with a table using ALTER TABLE pt EXCHANGE PARTITION p WITH TABLE nt, where pt is the partitioned table and p is the partition or subpartition of pt to be exchanged with unpartitioned table nt, provided that the following statements are true:

  1. Table nt is not itself partitioned.

  2. Table nt is not a temporary table.

  3. The structures of tables pt and nt are otherwise identical.

  4. Table nt contains no foreign key references, and no other table has any foreign keys that refer to nt.

  5. There are no rows in nt that lie outside the boundaries of the partition definition for p. This condition does not apply if WITHOUT VALIDATION is used.

  6. For InnoDB tables, both tables use the same row format. To determine the row format of an InnoDB table, query INFORMATION_SCHEMA.INNODB_TABLES.

  7. nt does not have any partitions that use the DATA DIRECTORY option. This restriction is lifted for InnoDB tables in MySQL 8.0.14 and later.

In addition to the ALTER, INSERT, and CREATE privileges usually required for ALTER TABLE statements, you must have the DROP privilege to perform ALTER TABLE ... EXCHANGE PARTITION.

You should also be aware of the following effects of ALTER TABLE ... EXCHANGE PARTITION:

  • Executing ALTER TABLE ... EXCHANGE PARTITION does not invoke any triggers on either the partitioned table or the table to be exchanged.

  • Any AUTO_INCREMENT columns in the exchanged table are reset.

  • The IGNORE keyword has no effect when used with ALTER TABLE ... EXCHANGE PARTITION.

The syntax for ALTER TABLE ... EXCHANGE PARTITION is shown here, where pt is the partitioned table, p is the partition (or subpartition) to be exchanged, and nt is the nonpartitioned table to be exchanged with p:

  1.     EXCHANGE PARTITION p
  2.     WITH TABLE nt;

Optionally, you can append WITH VALIDATION or WITHOUT VALIDATION. When WITHOUT VALIDATION is specified, the ALTER TABLE ... EXCHANGE PARTITION operation does not perform any row-by-row validation when exchanging a partition a nonpartitioned table, allowing database administrators to assume responsibility for ensuring that rows are within the boundaries of the partition definition. WITH VALIDATION is the default.

One and only one partition or subpartition may be exchanged with one and only one nonpartitioned table in a single ALTER TABLE EXCHANGE PARTITION statement. To exchange multiple partitions or subpartitions, use multiple ALTER TABLE EXCHANGE PARTITION statements. EXCHANGE PARTITION may not be combined with other ALTER TABLE options. The partitioning and (if applicable) subpartitioning used by the partitioned table may be of any type or types supported in MySQL 8.0.

Exchanging a Partition with a Nonpartitioned Table

Suppose that a partitioned table e has been created and populated using the following SQL statements:

  1.     id INT NOT NULL,
  2.     fname VARCHAR(30),
  3.     lname VARCHAR(30)
  4. )
  5.     PARTITION BY RANGE (id) (
  6.         PARTITION p0 VALUES LESS THAN (50),
  7.         PARTITION p1 VALUES LESS THAN (100),
  8.         PARTITION p2 VALUES LESS THAN (150),
  9.         PARTITION p3 VALUES LESS THAN (MAXVALUE)
  10. );
  11.  
  12.     (1669, "Jim", "Smith"),
  13.     (337, "Mary", "Jones"),
  14.     (16, "Frank", "White"),
  15.     (2005, "Linda", "Black");

Now we create a nonpartitioned copy of e named e2. This can be done using the mysql client as shown here:

  1. mysql> CREATE TABLE e2 LIKE e;
  2. Query OK, 0 rows affected (0.04 sec)
  3.  
  4. mysql> ALTER TABLE e2 REMOVE PARTITIONING;
  5. Query OK, 0 rows affected (0.07 sec)
  6. Records: 0  Duplicates: 0  Warnings: 0

You can see which partitions in table e contain rows by querying the INFORMATION_SCHEMA.PARTITIONS table, like this:

  1. mysql> SELECT PARTITION_NAME, TABLE_ROWS
  2.            FROM INFORMATION_SCHEMA.PARTITIONS
  3.            WHERE TABLE_NAME = 'e';
  4. +----------------+------------+
  5. | PARTITION_NAME | TABLE_ROWS |
  6. +----------------+------------+
  7. | p0             |          1 |
  8. | p1             |          0 |
  9. | p2             |          0 |
  10. | p3             |          3 |
  11. +----------------+------------+
  12. 2 rows in set (0.00 sec)
Note

For partitioned InnoDB tables, the row count given in the TABLE_ROWS column of the INFORMATION_SCHEMA.PARTITIONS table is only an estimated value used in SQL optimization, and is not always exact.

To exchange partition p0 in table e with table e2, you can use ALTER TABLE, as shown here:

  1. mysql> ALTER TABLE e EXCHANGE PARTITION p0 WITH TABLE e2;
  2. Query OK, 0 rows affected (0.04 sec)

More precisely, the statement just issued causes any rows found in the partition to be swapped with those found in the table. You can observe how this has happened by querying the INFORMATION_SCHEMA.PARTITIONS table, as before. The table row that was previously found in partition p0 is no longer present:

  1. mysql> SELECT PARTITION_NAME, TABLE_ROWS
  2.            FROM INFORMATION_SCHEMA.PARTITIONS
  3.            WHERE TABLE_NAME = 'e';
  4. +----------------+------------+
  5. | PARTITION_NAME | TABLE_ROWS |
  6. +----------------+------------+
  7. | p0             |          0 |
  8. | p1             |          0 |
  9. | p2             |          0 |
  10. | p3             |          3 |
  11. +----------------+------------+
  12. 4 rows in set (0.00 sec)

If you query table e2, you can see that the missing row can now be found there:

  1. mysql> SELECT * FROM e2;
  2. +----+-------+-------+
  3. | id | fname | lname |
  4. +----+-------+-------+
  5. | 16 | Frank | White |
  6. +----+-------+-------+
  7. 1 row in set (0.00 sec)

The table to be exchanged with the partition does not necessarily have to be empty. To demonstrate this, we first insert a new row into table e, making sure that this row is stored in partition p0 by choosing an id column value that is less than 50, and verifying this afterward by querying the PARTITIONS table:

  1. mysql> INSERT INTO e VALUES (41, "Michael", "Green");            
  2. Query OK, 1 row affected (0.05 sec)                              
  3.  
  4. mysql> SELECT PARTITION_NAME, TABLE_ROWS
  5.            FROM INFORMATION_SCHEMA.PARTITIONS
  6.            WHERE TABLE_NAME = 'e';            
  7. +----------------+------------+            
  8. | PARTITION_NAME | TABLE_ROWS |            
  9. +----------------+------------+            
  10. | p0             |          1 |            
  11. | p1             |          0 |            
  12. | p2             |          0 |            
  13. | p3             |          3 |            
  14. +----------------+------------+            
  15. 4 rows in set (0.00 sec)

Now we once again exchange partition p0 with table e2 using the same ALTER TABLE statement as previously:

  1. mysql> ALTER TABLE e EXCHANGE PARTITION p0 WITH TABLE e2;
  2. Query OK, 0 rows affected (0.28 sec)

The output of the following queries shows that the table row that was stored in partition p0 and the table row that was stored in table e2, prior to issuing the ALTER TABLE statement, have now switched places:

  1. mysql> SELECT * FROM e;
  2. +------+-------+-------+
  3. | id   | fname | lname |
  4. +------+-------+-------+
  5. |   16 | Frank | White |
  6. | 1669 | Jim   | Smith |
  7. |  337 | Mary  | Jones |
  8. | 2005 | Linda | Black |
  9. +------+-------+-------+
  10. 4 rows in set (0.00 sec)
  11.  
  12. mysql> SELECT PARTITION_NAME, TABLE_ROWS
  13.            FROM INFORMATION_SCHEMA.PARTITIONS
  14.            WHERE TABLE_NAME = 'e';
  15. +----------------+------------+
  16. | PARTITION_NAME | TABLE_ROWS |
  17. +----------------+------------+
  18. | p0             |          1 |
  19. | p1             |          0 |
  20. | p2             |          0 |
  21. | p3             |          3 |
  22. +----------------+------------+
  23. 4 rows in set (0.00 sec)
  24.  
  25. mysql> SELECT * FROM e2;
  26. +----+---------+-------+
  27. | id | fname   | lname |
  28. +----+---------+-------+
  29. | 41 | Michael | Green |
  30. +----+---------+-------+
  31. 1 row in set (0.00 sec)

Inhoudsopgave Haut

Nonmatching Rows

You should keep in mind that any rows found in the nonpartitioned table prior to issuing the ALTER TABLE ... EXCHANGE PARTITION statement must meet the conditions required for them to be stored in the target partition; otherwise, the statement fails. To see how this occurs, first insert a row into e2 that is outside the boundaries of the partition definition for partition p0 of table e. For example, insert a row with an id column value that is too large; then, try to exchange the table with the partition again:

  1. mysql> INSERT INTO e2 VALUES (51, "Ellen", "McDonald");
  2. Query OK, 1 row affected (0.08 sec)
  3.  
  4. mysql> ALTER TABLE e EXCHANGE PARTITION p0 WITH TABLE e2;
  5. ERROR 1707 (HY000): Found row that does not match the partition

Only the WITHOUT VALIDATION option would permit this operation to succeed:

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

When a partition is exchanged with a table that contains rows that do not match the partition definition, it is the responsibility of the database administrator to fix the non-matching rows, which can be performed using REPAIR TABLE or ALTER TABLE ... REPAIR PARTITION.

Inhoudsopgave Haut

Exchanging Partitions Without Row-By-Row Validation

To avoid time consuming validation when exchanging a partition with a table that has many rows, it is possible to skip the row-by-row validation step by appending WITHOUT VALIDATION to the ALTER TABLE ... EXCHANGE PARTITION statement.

The following example compares the difference between execution times when exchanging a partition with a nonpartitioned table, with and without validation. The partitioned table (table e) contains two partitions of 1 million rows each. The rows in p0 of table e are removed and p0 is exchanged with a nonpartitioned table of 1 million rows. The WITH VALIDATION operation takes 0.74 seconds. By comparison, the WITHOUT VALIDATION operation takes 0.01 seconds.

  1. # Create a partitioned table with 1 million rows in each partition
  2.  
  3.     id INT NOT NULL,
  4.     fname VARCHAR(30),
  5.     lname VARCHAR(30)
  6. )
  7.     PARTITION BY RANGE (id) (
  8.         PARTITION p0 VALUES LESS THAN (1000001),
  9.         PARTITION p1 VALUES LESS THAN (2000001),
  10. );
  11.  
  12. mysql> SELECT COUNT(*) FROM e;                                            
  13. | COUNT(*) |
  14. +----------+
  15. |  2000000 |
  16. +----------+
  17. 1 row in set (0.27 sec)
  18.  
  19. # View the rows in each partition
  20.  
  21. SELECT PARTITION_NAME, TABLE_ROWS FROM INFORMATION_SCHEMA.PARTITIONS WHERE TABLE_NAME = 'e';
  22. +----------------+-------------+
  23. | PARTITION_NAME | TABLE_ROWS  |
  24. +----------------+-------------+
  25. | p0             |     1000000 |
  26. | p1             |     1000000 |
  27. +----------------+-------------+
  28. 2 rows in set (0.00 sec)
  29.  
  30. # Create a nonpartitioned table of the same structure and populate it with 1 million rows
  31.  
  32.     id INT NOT NULL,
  33.     fname VARCHAR(30),
  34.     lname VARCHAR(30)
  35. );
  36.  
  37. mysql> SELECT COUNT(*) FROM e2;
  38. +----------+
  39. | COUNT(*) |
  40. +----------+
  41. |  1000000 |
  42. +----------+
  43. 1 row in set (0.24 sec)
  44.  
  45. # Create another nonpartitioned table of the same structure and populate it with 1 million rows
  46.  
  47.     id INT NOT NULL,
  48.     fname VARCHAR(30),
  49.     lname VARCHAR(30)
  50. );
  51.    
  52. mysql> SELECT COUNT(*) FROM e3;
  53. +----------+
  54. | COUNT(*) |
  55. +----------+
  56. |  1000000 |
  57. +----------+
  58. 1 row in set (0.25 sec)
  59.  
  60. # Drop the rows from p0 of table e
  61.  
  62. mysql> DELETE FROM e WHERE id < 1000001;
  63. Query OK, 1000000 rows affected (5.55 sec)
  64.  
  65. # Confirm that there are no rows in partition p0
  66.  
  67. mysql> SELECT PARTITION_NAME, TABLE_ROWS FROM INFORMATION_SCHEMA.PARTITIONS WHERE TABLE_NAME = 'e';
  68. +----------------+------------+
  69. | PARTITION_NAME | TABLE_ROWS |
  70. +----------------+------------+
  71. | p0             |          0 |
  72. | p1             |    1000000 |
  73. +----------------+------------+
  74. 2 rows in set (0.00 sec)
  75.    
  76. # Exchange partition p0 of table e with the table e2 'WITH VALIDATION'
  77.  
  78. mysql> ALTER TABLE e EXCHANGE PARTITION p0 WITH TABLE e2 WITH VALIDATION;
  79. Query OK, 0 rows affected (0.74 sec)
  80.  
  81. # Confirm that the partition was exchanged with table e2
  82.  
  83. mysql> SELECT PARTITION_NAME, TABLE_ROWS FROM INFORMATION_SCHEMA.PARTITIONS WHERE TABLE_NAME = 'e';
  84. +----------------+------------+
  85. | PARTITION_NAME | TABLE_ROWS |
  86. +----------------+------------+
  87. | p0             |    1000000 |
  88. | p1             |    1000000 |
  89. +----------------+------------+
  90. 2 rows in set (0.00 sec)
  91.  
  92. # Once again, drop the rows from p0 of table e
  93.  
  94. mysql> DELETE FROM e WHERE id < 1000001;
  95. Query OK, 1000000 rows affected (5.55 sec)
  96.  
  97. # Confirm that there are no rows in partition p0
  98.  
  99. mysql> SELECT PARTITION_NAME, TABLE_ROWS FROM INFORMATION_SCHEMA.PARTITIONS WHERE TABLE_NAME = 'e';
  100. +----------------+------------+
  101. | PARTITION_NAME | TABLE_ROWS |
  102. +----------------+------------+
  103. | p0             |          0 |
  104. | p1             |    1000000 |
  105. +----------------+------------+
  106. 2 rows in set (0.00 sec)
  107.  
  108. # Exchange partition p0 of table e with the table e3 'WITHOUT VALIDATION'
  109.  
  110. mysql> ALTER TABLE e EXCHANGE PARTITION p0 WITH TABLE e3 WITHOUT VALIDATION;
  111. Query OK, 0 rows affected (0.01 sec)
  112.  
  113. # Confirm that the partition was exchanged with table e3
  114.  
  115. mysql> SELECT PARTITION_NAME, TABLE_ROWS FROM INFORMATION_SCHEMA.PARTITIONS WHERE TABLE_NAME = 'e';
  116. +----------------+------------+
  117. | PARTITION_NAME | TABLE_ROWS |
  118. +----------------+------------+
  119. | p0             |    1000000 |
  120. | p1             |    1000000 |
  121. +----------------+------------+
  122. 2 rows in set (0.00 sec)

If a partition is exchanged with a table that contains rows that do not match the partition definition, it is the responsibility of the database administrator to fix the non-matching rows, which can be performed using REPAIR TABLE or ALTER TABLE ... REPAIR PARTITION.

Inhoudsopgave Haut

Exchanging a Subpartition with a Nonpartitioned Table

You can also exchange a subpartition of a subpartitioned table (see Section 23.2.6, “Subpartitioning”) with a nonpartitioned table using an ALTER TABLE ... EXCHANGE PARTITION statement. In the following example, we first create a table es that is partitioned by RANGE and subpartitioned by KEY, populate this table as we did table e, and then create an empty, nonpartitioned copy es2 of the table, as shown here:

  1. mysql> CREATE TABLE es (
  2.     ->     id INT NOT NULL,
  3.     ->     fname VARCHAR(30),
  4.     ->     lname VARCHAR(30)
  5.     -> )
  6.     ->     PARTITION BY RANGE (id)
  7.     ->     SUBPARTITION BY KEY (lname)
  8.     ->     SUBPARTITIONS 2 (
  9.     ->         PARTITION p0 VALUES LESS THAN (50),
  10.     ->         PARTITION p1 VALUES LESS THAN (100),
  11.     ->         PARTITION p2 VALUES LESS THAN (150),
  12.     ->         PARTITION p3 VALUES LESS THAN (MAXVALUE)
  13.     ->     );
  14. Query OK, 0 rows affected (2.76 sec)
  15.  
  16. mysql> INSERT INTO es VALUES
  17.     ->     (1669, "Jim", "Smith"),
  18.     ->     (337, "Mary", "Jones"),
  19.     ->     (16, "Frank", "White"),
  20.     ->     (2005, "Linda", "Black");
  21. Query OK, 4 rows affected (0.04 sec)
  22. Records: 4  Duplicates: 0  Warnings: 0
  23.  
  24. mysql> CREATE TABLE es2 LIKE es;
  25. Query OK, 0 rows affected (1.27 sec)
  26.  
  27. mysql> ALTER TABLE es2 REMOVE PARTITIONING;
  28. Query OK, 0 rows affected (0.70 sec)
  29. Records: 0  Duplicates: 0  Warnings: 0

Although we did not explicitly name any of the subpartitions when creating table es, we can obtain generated names for these by including the SUBPARTITION_NAME column of the PARTITIONS table from INFORMATION_SCHEMA when selecting from that table, as shown here:

  1. mysql> SELECT PARTITION_NAME, SUBPARTITION_NAME, TABLE_ROWS
  2.     ->     FROM INFORMATION_SCHEMA.PARTITIONS
  3.     ->     WHERE TABLE_NAME = 'es';
  4. +----------------+-------------------+------------+
  5. | PARTITION_NAME | SUBPARTITION_NAME | TABLE_ROWS |
  6. +----------------+-------------------+------------+
  7. | p0             | p0sp0             |          1 |
  8. | p0             | p0sp1             |          0 |
  9. | p1             | p1sp0             |          0 |
  10. | p1             | p1sp1             |          0 |
  11. | p2             | p2sp0             |          0 |
  12. | p2             | p2sp1             |          0 |
  13. | p3             | p3sp0             |          3 |
  14. | p3             | p3sp1             |          0 |
  15. +----------------+-------------------+------------+
  16. 8 rows in set (0.00 sec)

The following ALTER TABLE statement exchanges subpartition p3sp0 in table es with the nonpartitioned table es2:

  1. mysql> ALTER TABLE es EXCHANGE PARTITION p3sp0 WITH TABLE es2;
  2. Query OK, 0 rows affected (0.29 sec)

You can verify that the rows were exchanged by issuing the following queries:

  1. mysql> SELECT PARTITION_NAME, SUBPARTITION_NAME, TABLE_ROWS
  2.     ->     FROM INFORMATION_SCHEMA.PARTITIONS
  3.     ->     WHERE TABLE_NAME = 'es';
  4. +----------------+-------------------+------------+
  5. | PARTITION_NAME | SUBPARTITION_NAME | TABLE_ROWS |
  6. +----------------+-------------------+------------+
  7. | p0             | p0sp0             |          1 |
  8. | p0             | p0sp1             |          0 |
  9. | p1             | p1sp0             |          0 |
  10. | p1             | p1sp1             |          0 |
  11. | p2             | p2sp0             |          0 |
  12. | p2             | p2sp1             |          0 |
  13. | p3             | p3sp0             |          0 |
  14. | p3             | p3sp1             |          0 |
  15. +----------------+-------------------+------------+
  16. 8 rows in set (0.00 sec)
  17.  
  18. mysql> SELECT * FROM es2;
  19. +------+-------+-------+
  20. | id   | fname | lname |
  21. +------+-------+-------+
  22. | 1669 | Jim   | Smith |
  23. |  337 | Mary  | Jones |
  24. | 2005 | Linda | Black |
  25. +------+-------+-------+
  26. 3 rows in set (0.00 sec)

If a table is subpartitioned, you can exchange only a subpartition of the table—not an entire partition—with an unpartitioned table, as shown here:

  1. mysql> ALTER TABLE es EXCHANGE PARTITION p3 WITH TABLE es2;
  2. ERROR 1704 (HY000): Subpartitioned table, use subpartition instead of partition

Table structures are compared in a strict fashion; the number, order, names, and types of columns and indexes of the partitioned table and the nonpartitioned table must match exactly. In addition, both tables must use the same storage engine:

  1. mysql> CREATE TABLE es3 LIKE e;
  2. Query OK, 0 rows affected (1.31 sec)
  3.  
  4. mysql> ALTER TABLE es3 REMOVE PARTITIONING;
  5. Query OK, 0 rows affected (0.53 sec)
  6. Records: 0  Duplicates: 0  Warnings: 0
  7.  
  8. mysql> SHOW CREATE TABLE es3\G
  9. *************************** 1. row ***************************
  10.        Table: es3
  11.   `id` int(11) NOT NULL,
  12.   `fname` varchar(30) DEFAULT NULL,
  13.   `lname` varchar(30) DEFAULT NULL
  14. 1 row in set (0.00 sec)
  15.  
  16. mysql> ALTER TABLE es3 ENGINE = MyISAM;
  17. Query OK, 0 rows affected (0.15 sec)
  18. Records: 0  Duplicates: 0  Warnings: 0
  19.  
  20. mysql> ALTER TABLE es EXCHANGE PARTITION p3sp0 WITH TABLE es3;
  21. ERROR 1497 (HY000): The mix of handlers in the partitions is not allowed in this version of MySQL

Zoek in de MySQL-handleiding

Nederlandse vertaling

U hebt gevraagd om deze site in het Nederlands te bezoeken. Voor nu wordt alleen de interface vertaald, maar nog niet alle inhoud.

Als je me wilt helpen met vertalingen, is je bijdrage welkom. Het enige dat u hoeft te doen, is u op de site registreren en mij een bericht sturen waarin u wordt gevraagd om u toe te voegen aan de groep vertalers, zodat u de gewenste pagina's kunt vertalen. Een link onderaan elke vertaalde pagina geeft aan dat u de vertaler bent en heeft een link naar uw profiel.

Bij voorbaat dank.

Document heeft de 26/06/2006 gemaakt, de laatste keer de 26/10/2018 gewijzigd
Bron van het afgedrukte document:https://www.gaudry.be/nl/mysql-rf-partitioning-management-exchange.html

De infobrol is een persoonlijke site waarvan de inhoud uitsluitend mijn verantwoordelijkheid is. De tekst is beschikbaar onder CreativeCommons-licentie (BY-NC-SA). Meer info op de gebruiksvoorwaarden en de auteur.

Referenties

  1. Bekijk - html-document Taal van het document:en Manuel MySQL : https://dev.mysql.com/

Deze verwijzingen en links verwijzen naar documenten die geraadpleegd zijn tijdens het schrijven van deze pagina, of die aanvullende informatie kunnen geven, maar de auteurs van deze bronnen kunnen niet verantwoordelijk worden gehouden voor de inhoud van deze pagina.
De auteur Deze site is als enige verantwoordelijk voor de manier waarop de verschillende concepten, en de vrijheden die met de referentiewerken worden genomen, hier worden gepresenteerd. Vergeet niet dat u meerdere broninformatie moet doorgeven om het risico op fouten te verkleinen.

Inhoudsopgave Haut