MySQL 实验室 因为专注,所以专业。

  • 首页
  • 博客
  • 下载
  • 文档
  • 工具
  • 知识库
  • 培训及服务
  • MySQL 5.1 Reference Manual :: 18 Partitioning :: 18.3 Partition Management
    • MySQL 5.1 Reference Manual
    • Preface, Notes, Licenses
    • 1 General Information
    • 2 Installing and Upgrading MySQL
    • 3 Tutorial
    • 4 MySQL Programs
    • 5 MySQL Server Administration
    • 6 Backup and Recovery
    • 7 Optimization
    • 8 Language Structure
    • 9 Internationalization and Localization
    • 10 Data Types
    • 11 Functions and Operators
    • 12 SQL Statement Syntax
    • 13 Storage Engines
    • 14 High Availability and Scalability
    • 15 MySQL Enterprise Monitor
    • 16 Replication
    • 17 MySQL Cluster NDB 6.X/7.X
    • 18 Partitioning
    • 19 Stored Programs and Views
    • 20 INFORMATION_SCHEMA Tables
    • 21 Connectors and APIs
    • 22 Extending MySQL
    • A MySQL 5.1 Frequently Asked Questions
    • B Errors, Error Codes, and Common Problems
    • C MySQL Change History
    • D Restrictions and Limits
    • Index
    • Standard Index
    • C Function Index
    • Command Index
    • Function Index
    • INFORMATION_SCHEMA Index
    • Transaction Isolation Level Index
    • JOIN Types Index
    • Operator Index
    • Option Index
    • Privileges Index
    • SQL Modes Index
    • Status Variable Index
    • Statement/Syntax Index
    • System Variable Index

    18.3. Partition Management

    [+/-]

    18.3.1. Management of RANGE and LIST Partitions
    18.3.2. Management of HASH and KEY Partitions
    18.3.3. Maintenance of Partitions
    18.3.4. Obtaining Information About Partitions

    MySQL 5.1 provides a number of ways to modify partitioned tables. It is possible to add, drop, redefine, merge, or split existing partitions. All of these actions can be carried out using the partitioning extensions to the ALTER TABLE command (see Section 12.1.7, “ALTER TABLE Syntax”, for syntax definitions). There are also ways to obtain information about partitioned tables and partitions. We discuss these topics in the sections that follow.

    • For information about partition management in tables partitioned by RANGE or LIST, see Section 18.3.1, “Management of RANGE and LIST Partitions”.

    • For a discussion of managing HASH and KEY partitions, see Section 18.3.2, “Management of HASH and KEY Partitions”.

    • See Section 18.3.4, “Obtaining Information About Partitions”, for a discussion of mechanisms provided in MySQL 5.1 for obtaining information about partitioned tables and partitions.

    • For a discussion of performing maintenance operations on partitions, see Section 18.3.3, “Maintenance of Partitions”.

    Note

    In MySQL 5.1, all partitions of a partitioned table must have the same number of subpartitions, and it is not possible to change the subpartitioning once the table has been created.

    The statement ALTER TABLE ... PARTITION BY ... is available and is functional beginning with MySQL 5.1.6; previously in MySQL 5.1, this was accepted as valid syntax, but the statement did nothing.

    To change a table's partitioning scheme, it is necessary only to use the ALTER TABLE command with a partition_options clause. This clause has the same syntax as that as used with CREATE TABLE for creating a partitioned table, and always begins with the keywords PARTITION BY. Suppose that you have a table partitioned by range using the following CREATE TABLE statement:

    CREATE TABLE trb3 (id INT, name VARCHAR(50), purchased DATE)
        PARTITION BY RANGE( YEAR(purchased) ) (
            PARTITION p0 VALUES LESS THAN (1990),
            PARTITION p1 VALUES LESS THAN (1995),
            PARTITION p2 VALUES LESS THAN (2000),
            PARTITION p3 VALUES LESS THAN (2005)
        );
    

    To repartition this table so that it is partitioned by key into two partitions using the id column value as the basis for the key, you can use this statement:

    ALTER TABLE trb3 PARTITION BY KEY(id) PARTITIONS 2;
    

    This has the same effect on the structure of the table as dropping the table and re-creating it using CREATE TABLE trb3 PARTITION BY KEY(id) PARTITIONS 2;.

    In MySQL 5.1.7 and earlier MySQL 5.1 releases, ALTER TABLE ... ENGINE = ... removed all partitioning from the affected table. Beginning with MySQL 5.1.8, this statement changes only the storage engine used by the table, and leaves the table's partitioning scheme intact. As of MySQL 5.1.8, use ALTER TABLE ... REMOVE PARTITIONING to remove a table's partitioning. See Section 12.1.7, “ALTER TABLE Syntax”.

    Important

    Only a single PARTITION BY, ADD PARTITION, DROP PARTITION, REORGANIZE PARTITION, or COALESCE PARTITION clause can be used in a given ALTER TABLE statement. If you (for example) wish to drop a partition and reorganize a table's remaining partitions, you must do so in two separate ALTER TABLE statements (one using DROP PARTITION and then a second one using REORGANIZE PARITITIONS).