Rechercher dans le manuel MySQL

23.2.3.1 RANGE COLUMNS partitioning

Range columns partitioning is similar to range partitioning, but enables you to define partitions using ranges based on multiple column values. In addition, you can define the ranges using columns of types other than integer types.

RANGE COLUMNS partitioning differs significantly from RANGE partitioning in the following ways:

  • RANGE COLUMNS does not accept expressions, only names of columns.

  • RANGE COLUMNS accepts a list of one or more columns.

    RANGE COLUMNS partitions are based on comparisons between tuples (lists of column values) rather than comparisons between scalar values. Placement of rows in RANGE COLUMNS partitions is also based on comparisons between tuples; this is discussed further later in this section.

  • RANGE COLUMNS partitioning columns are not restricted to integer columns; string, DATE and DATETIME columns can also be used as partitioning columns. (See Section 23.2.3, “COLUMNS Partitioning”, for details.)

The basic syntax for creating a table partitioned by RANGE COLUMNS is shown here:

  1. CREATE TABLE table_name
  2. PARTITIONED BY RANGE COLUMNS(column_list) (
  3.     PARTITION partition_name VALUES LESS THAN (value_list)[,
  4.     PARTITION partition_name VALUES LESS THAN (value_list)][,
  5.     ...]
  6. )
  7.  
  8. column_list:
  9.     column_name[, column_name][, ...]
  10.  
  11. value_list:
  12.     value[, value][, ...]
Note

Not all CREATE TABLE options that can be used when creating partitioned tables are shown here. For complete information, see Section 13.1.20, “CREATE TABLE Syntax”.

In the syntax just shown, column_list is a list of one or more columns (sometimes called a partitioning column list), and value_list is a list of values (that is, it is a partition definition value list). A value_list must be supplied for each partition definition, and each value_list must have the same number of values as the column_list has columns. Generally speaking, if you use N columns in the COLUMNS clause, then each VALUES LESS THAN clause must also be supplied with a list of N values.

The elements in the partitioning column list and in the value list defining each partition must occur in the same order. In addition, each element in the value list must be of the same data type as the corresponding element in the column list. However, the order of the column names in the partitioning column list and the value lists does not have to be the same as the order of the table column definitions in the main part of the CREATE TABLE statement. As with table partitioned by RANGE, you can use MAXVALUE to represent a value such that any legal value inserted into a given column is always less than this value. Here is an example of a CREATE TABLE statement that helps to illustrate all of these points:

  1. mysql> CREATE TABLE rcx (
  2.     ->     a INT,
  3.     ->     b INT,
  4.     ->     c CHAR(3),
  5.     ->     d INT
  6.     -> )
  7.     -> PARTITION BY RANGE COLUMNS(a,d,c) (
  8.     ->     PARTITION p0 VALUES LESS THAN (5,10,'ggg'),
  9.     ->     PARTITION p1 VALUES LESS THAN (10,20,'mmm'),
  10.     ->     PARTITION p2 VALUES LESS THAN (15,30,'sss'),
  11.     ->     PARTITION p3 VALUES LESS THAN (MAXVALUE,MAXVALUE,MAXVALUE)
  12.     -> );
  13. Query OK, 0 rows affected (0.15 sec)

Table rcx contains the columns a, b, c, d. The partitioning column list supplied to the COLUMNS clause uses 3 of these columns, in the order a, d, c. Each value list used to define a partition contains 3 values in the same order; that is, each value list tuple has the form (INT, INT, CHAR(3)), which corresponds to the data types used by columns a, d, and c (in that order).

Placement of rows into partitions is determined by comparing the tuple from a row to be inserted that matches the column list in the COLUMNS clause with the tuples used in the VALUES LESS THAN clauses to define partitions of the table. Because we are comparing tuples (that is, lists or sets of values) rather than scalar values, the semantics of VALUES LESS THAN as used with RANGE COLUMNS partitions differs somewhat from the case with simple RANGE partitions. In RANGE partitioning, a row generating an expression value that is equal to a limiting value in a VALUES LESS THAN is never placed in the corresponding partition; however, when using RANGE COLUMNS partitioning, it is sometimes possible for a row whose partitioning column list's first element is equal in value to the that of the first element in a VALUES LESS THAN value list to be placed in the corresponding partition.

Consider the RANGE partitioned table created by this statement:

  1.     a INT,
  2.     b INT
  3. )
  4. PARTITION BY RANGE (a)  (
  5.     PARTITION p0 VALUES LESS THAN (5),
  6.     PARTITION p1 VALUES LESS THAN (MAXVALUE)
  7. );

If we insert 3 rows into this table such that the column value for a is 5 for each row, all 3 rows are stored in partition p1 because the a column value is in each case not less than 5, as we can see by executing the proper query against the INFORMATION_SCHEMA.PARTITIONS table:

  1. mysql> INSERT INTO r1 VALUES (5,10), (5,11), (5,12);
  2. Query OK, 3 rows affected (0.00 sec)
  3. Records: 3  Duplicates: 0  Warnings: 0
  4.  
  5. mysql> SELECT PARTITION_NAME,TABLE_ROWS
  6.     ->     FROM INFORMATION_SCHEMA.PARTITIONS
  7.     ->     WHERE TABLE_NAME = 'r1';
  8. +----------------+------------+
  9. | PARTITION_NAME | TABLE_ROWS |
  10. +----------------+------------+
  11. | p0             |          0 |
  12. | p1             |          3 |
  13. +----------------+------------+
  14. 2 rows in set (0.00 sec)

Now consider a similar table rc1 that uses RANGE COLUMNS partitioning with both columns a and b referenced in the COLUMNS clause, created as shown here:

  1.     a INT,
  2.     b INT
  3. )
  4. PARTITION BY RANGE COLUMNS(a, b) (
  5.     PARTITION p0 VALUES LESS THAN (5, 12),
  6.     PARTITION p3 VALUES LESS THAN (MAXVALUE, MAXVALUE)
  7. );

If we insert exactly the same rows into rc1 as we just inserted into r1, the distribution of the rows is quite different:

  1. mysql> INSERT INTO rc1 VALUES (5,10), (5,11), (5,12);
  2. Query OK, 3 rows affected (0.00 sec)
  3. Records: 3  Duplicates: 0  Warnings: 0
  4.  
  5. mysql> SELECT PARTITION_NAME,TABLE_ROWS
  6.     ->     FROM INFORMATION_SCHEMA.PARTITIONS
  7.     ->     WHERE TABLE_NAME = 'rc1';
  8. +--------------+----------------+------------+
  9. | TABLE_SCHEMA | PARTITION_NAME | TABLE_ROWS |
  10. +--------------+----------------+------------+
  11. | p            | p0             |          2 |
  12. | p            | p1             |          1 |
  13. +--------------+----------------+------------+
  14. 2 rows in set (0.00 sec)

This is because we are comparing rows rather than scalar values. We can compare the row values inserted with the limiting row value from the VALUES THAN LESS THAN clause used to define partition p0 in table rc1, like this:

  1. mysql> SELECT (5,10) < (5,12), (5,11) < (5,12), (5,12) < (5,12);
  2. +-----------------+-----------------+-----------------+
  3. | (5,10) < (5,12) | (5,11) < (5,12) | (5,12) < (5,12) |
  4. +-----------------+-----------------+-----------------+
  5. |               1 |               1 |               0 |
  6. +-----------------+-----------------+-----------------+
  7. 1 row in set (0.00 sec)

The 2 tuples (5,10) and (5,11) evaluate as less than (5,12), so they are stored in partition p0. Since 5 is not less than 5 and 12 is not less than 12, (5,12) is considered not less than (5,12), and is stored in partition p1.

The SELECT statement in the preceding example could also have been written using explicit row constructors, like this:

  1. SELECT ROW(5,10) < ROW(5,12), ROW(5,11) < ROW(5,12), ROW(5,12) < ROW(5,12);

For more information about the use of row constructors in MySQL, see Section 13.2.11.5, “Row Subqueries”.

For a table partitioned by RANGE COLUMNS using only a single partitioning column, the storing of rows in partitions is the same as that of an equivalent table that is partitioned by RANGE. The following CREATE TABLE statement creates a table partitioned by RANGE COLUMNS using 1 partitioning column:

  1.     a INT,
  2.     b INT
  3. )
  4. PARTITION BY RANGE COLUMNS (a)  (
  5.     PARTITION p0 VALUES LESS THAN (5),
  6.     PARTITION p1 VALUES LESS THAN (MAXVALUE)
  7. );

If we insert the rows (5,10), (5,11), and (5,12) into this table, we can see that their placement is the same as it is for the table r we created and populated earlier:

  1. mysql> INSERT INTO rx VALUES (5,10), (5,11), (5,12);
  2. Query OK, 3 rows affected (0.00 sec)
  3. Records: 3  Duplicates: 0  Warnings: 0
  4.  
  5. mysql> SELECT PARTITION_NAME,TABLE_ROWS
  6.     ->     FROM INFORMATION_SCHEMA.PARTITIONS
  7.     ->     WHERE TABLE_NAME = 'rx';
  8. +--------------+----------------+------------+
  9. | TABLE_SCHEMA | PARTITION_NAME | TABLE_ROWS |
  10. +--------------+----------------+------------+
  11. | p            | p0             |          0 |
  12. | p            | p1             |          3 |
  13. +--------------+----------------+------------+
  14. 2 rows in set (0.00 sec)

It is also possible to create tables partitioned by RANGE COLUMNS where limiting values for one or more columns are repeated in successive partition definitions. You can do this as long as the tuples of column values used to define the partitions are strictly increasing. For example, each of the following CREATE TABLE statements is valid:

  1.     a INT,
  2.     b INT
  3. )
  4. PARTITION BY RANGE COLUMNS(a,b) (
  5.     PARTITION p0 VALUES LESS THAN (0,10),
  6.     PARTITION p1 VALUES LESS THAN (10,20),
  7.     PARTITION p2 VALUES LESS THAN (10,30),
  8.     PARTITION p3 VALUES LESS THAN (MAXVALUE,MAXVALUE)
  9.  );
  10.  
  11.     a INT,
  12.     b INT
  13. )
  14. PARTITION BY RANGE COLUMNS(a,b) (
  15.     PARTITION p0 VALUES LESS THAN (0,10),
  16.     PARTITION p1 VALUES LESS THAN (10,20),
  17.     PARTITION p2 VALUES LESS THAN (10,30),
  18.     PARTITION p3 VALUES LESS THAN (10,35),
  19.     PARTITION p4 VALUES LESS THAN (20,40),
  20.     PARTITION p5 VALUES LESS THAN (MAXVALUE,MAXVALUE)
  21.  );

The following statement also succeeds, even though it might appear at first glance that it would not, since the limiting value of column b is 25 for partition p0 and 20 for partition p1, and the limiting value of column c is 100 for partition p1 and 50 for partition p2:

  1.     a INT,
  2.     b INT,
  3.     c INT
  4. )
  5. PARTITION BY RANGE COLUMNS(a,b,c) (
  6.     PARTITION p0 VALUES LESS THAN (0,25,50),
  7.     PARTITION p1 VALUES LESS THAN (10,20,100),
  8.     PARTITION p2 VALUES LESS THAN (10,30,50)
  9.     PARTITION p3 VALUES LESS THAN (MAXVALUE,MAXVALUE,MAXVALUE)
  10.  );

When designing tables partitioned by RANGE COLUMNS, you can always test successive partition definitions by comparing the desired tuples using the mysql client, like this:

  1. mysql> SELECT (0,25,50) < (10,20,100), (10,20,100) < (10,30,50);
  2. +-------------------------+--------------------------+
  3. | (0,25,50) < (10,20,100) | (10,20,100) < (10,30,50) |
  4. +-------------------------+--------------------------+
  5. |                       1 |                        1 |
  6. +-------------------------+--------------------------+
  7. 1 row in set (0.00 sec)

If a CREATE TABLE statement contains partition definitions that are not in strictly increasing order, it fails with an error, as shown in this example:

  1. mysql> CREATE TABLE rcf (
  2.     ->     a INT,
  3.     ->     b INT,
  4.     ->     c INT
  5.     -> )
  6.     -> PARTITION BY RANGE COLUMNS(a,b,c) (
  7.     ->     PARTITION p0 VALUES LESS THAN (0,25,50),
  8.     ->     PARTITION p1 VALUES LESS THAN (20,20,100),
  9.     ->     PARTITION p2 VALUES LESS THAN (10,30,50),
  10.     ->     PARTITION p3 VALUES LESS THAN (MAXVALUE,MAXVALUE,MAXVALUE)
  11.     ->  );
  12. ERROR 1493 (HY000): VALUES LESS THAN value must be strictly increasing for each partition

When you get such an error, you can deduce which partition definitions are invalid by making less than comparisons between their column lists. In this case, the problem is with the definition of partition p2 because the tuple used to define it is not less than the tuple used to define partition p3, as shown here:

  1. mysql> SELECT (0,25,50) < (20,20,100), (20,20,100) < (10,30,50);
  2. +-------------------------+--------------------------+
  3. | (0,25,50) < (20,20,100) | (20,20,100) < (10,30,50) |
  4. +-------------------------+--------------------------+
  5. |                       1 |                        0 |
  6. +-------------------------+--------------------------+
  7. 1 row in set (0.00 sec)

It is also possible for MAXVALUE to appear for the same column in more than one VALUES LESS THAN clause when using RANGE COLUMNS. However, the limiting values for individual columns in successive partition definitions should otherwise be increasing, there should be no more than one partition defined where MAXVALUE is used as the upper limit for all column values, and this partition definition should appear last in the list of PARTITION ... VALUES LESS THAN clauses. In addition, you cannot use MAXVALUE as the limiting value for the first column in more than one partition definition.

As stated previously, it is also possible with RANGE COLUMNS partitioning to use non-integer columns as partitioning columns. (See Section 23.2.3, “COLUMNS Partitioning”, for a complete listing of these.) Consider a table named employees (which is not partitioned), created using the following statement:

  1. CREATE TABLE employees (
  2.     id INT NOT NULL,
  3.     fname VARCHAR(30),
  4.     lname VARCHAR(30),
  5.     hired DATE NOT NULL DEFAULT '1970-01-01',
  6.     separated DATE NOT NULL DEFAULT '9999-12-31',
  7.     job_code INT NOT NULL,
  8.     store_id INT NOT NULL
  9. );

Using RANGE COLUMNS partitioning, you can create a version of this table that stores each row in one of four partitions based on the employee's last name, like this:

  1. CREATE TABLE employees_by_lname (
  2.     id INT NOT NULL,
  3.     fname VARCHAR(30),
  4.     lname VARCHAR(30),
  5.     hired DATE NOT NULL DEFAULT '1970-01-01',
  6.     separated DATE NOT NULL DEFAULT '9999-12-31',
  7.     job_code INT NOT NULL,
  8.     store_id INT NOT NULL
  9. )
  10. PARTITION BY RANGE COLUMNS (lname)  (
  11.     PARTITION p0 VALUES LESS THAN ('g'),
  12.     PARTITION p1 VALUES LESS THAN ('m'),
  13.     PARTITION p2 VALUES LESS THAN ('t'),
  14.     PARTITION p3 VALUES LESS THAN (MAXVALUE)
  15. );

Alternatively, you could cause the employees table as created previously to be partitioned using this scheme by executing the following ALTER TABLE statement:

  1. ALTER TABLE employees PARTITION BY RANGE COLUMNS (lname)  (
  2.     PARTITION p0 VALUES LESS THAN ('g'),
  3.     PARTITION p1 VALUES LESS THAN ('m'),
  4.     PARTITION p2 VALUES LESS THAN ('t'),
  5.     PARTITION p3 VALUES LESS THAN (MAXVALUE)
  6. );
Note

Because different character sets and collations have different sort orders, the character sets and collations in use may effect which partition of a table partitioned by RANGE COLUMNS a given row is stored in when using string columns as partitioning columns. In addition, changing the character set or collation for a given database, table, or column after such a table is created may cause changes in how rows are distributed. For example, when using a case-sensitive collation, 'and' sorts before 'Andersen', but when using a collation that is case insensitive, the reverse is true.

For information about how MySQL handles character sets and collations, see Chapter 10, Character Sets, Collations, Unicode.

Similarly, you can cause the employees table to be partitioned in such a way that each row is stored in one of several partitions based on the decade in which the corresponding employee was hired using the ALTER TABLE statement shown here:

  1. ALTER TABLE employees PARTITION BY RANGE COLUMNS (hired)  (
  2.     PARTITION p0 VALUES LESS THAN ('1970-01-01'),
  3.     PARTITION p1 VALUES LESS THAN ('1980-01-01'),
  4.     PARTITION p2 VALUES LESS THAN ('1990-01-01'),
  5.     PARTITION p3 VALUES LESS THAN ('2000-01-01'),
  6.     PARTITION p4 VALUES LESS THAN ('2010-01-01'),
  7.     PARTITION p5 VALUES LESS THAN (MAXVALUE)
  8. );

See Section 13.1.20, “CREATE TABLE Syntax”, for additional information about PARTITION BY RANGE COLUMNS syntax.


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-columns-range.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