mysql partition related operations

RANGE Partitioning: Assigns multiple rows to a partition based on column values ​​that belong to a given contiguous range.

LIST partitioning: Similar to partitioning by RANGE, the difference is that LIST partitioning is based on the column value matching a value in a discrete value set to select.

HASH Partitioning: Partitioning that selects based on the return value of a user-defined expression computed using the column values ​​of the rows to be inserted into the table. This function can contain any expression valid in MySQL that yields a non-negative integer value.

KEY partitioning: Similar to partitioning by HASH, the difference is that KEY partitioning only supports the calculation of one or more columns, and the mysql server provides its own hash function. One or more columns must contain integer values.

LINER HASH

MySQL also supports linear hashing, which differs from regular hashing in that linear hashing uses a linear powers-of-two algorithm, while regular hashing uses hashing The modulus of the hash function value. The only syntax difference between linear hash partitioning and regular hash partitioning is the addition of the "LINEAR" keyword to the "PARTITION BY" clause.

Partition table management operations

Query the data of a partition

select * from emp partition(p2, p3)

to delete the partition:

alter table empdrop partition p1;

cannot delete the hash or key partition.

Delete multiple partitions at once, alter table empdrop partition p1,p2;

add partitions:

alter table empadd partition (partition p3 values ​​less than (4000));

alter table empladd partition (partition p3 values ​​in (40));

decompose partition:

The Reorganizepartition keyword can modify some or all of the partitions of the table without losing data. The overall scope of the partitions before and after decomposition should be the same.

alter table te

reorganize partition p1 into

(

partition p1 values ​​less than (100),

partition p3 values ​​less than (1000)

); ---- will not lose data

Merge partition:

Merge partition: merge 2 partitions into one.
alter table te

reorganize partition p1,p3 into

(partition p1 values ​​less than (1000));

---- will not lose

data Redefine the hash partition table:

Alter table emp partitionby hash(salary)partitions 7;

----No Data will be

lost Redefine the range partition table:

Alter table emp partitionbyrange(salary)

(

partition p1 values ​​less than (2000),

partition p2 values ​​less than (4000)

); ---- will not lose data

Delete all partitions of the table:

Alter table emp removepartitioning;-- will not lose data

Rebuild partitions:

this and first Deleting all records saved in the partition and then reinserting them has the same effect. It can be used to defragment partitions.

ALTER TABLE emp rebuild partitionp1,p2;

Optimizing partitions:

If a large number of rows are deleted from the partition, or a large number of rows with variable length (that is, columns of type VARCHAR, BLOB, or TEXT) have been made Modified, you can use "ALTER TABLE ... OPTIMIZE PARTITION" to reclaim unused space and defragment partition data files.

ALTER TABLE empoptimize partition p1,p2;

analyze partition:

read and save the key distribution of the partition.

ALTER TABLE empanalyze partition p1,p2;

patch partition:

patch damaged partitions.

ALTER TABLE emp repairpartition p1,p2;

CHECK PARTITIONS: Partitions

can be checked in almost the same way as CHECK TABLE for non-partitioned tables.

ALTER TABLE empCHECK partition p1, p2;

This command can tell you whether the data or indexes in the partition p1, p2 of the table emp have been destroyed. If this happens, use "ALTER TABLE ... REPAIR PARTITION" to patch the partition.

The limitations of mysql partition table
1. In version 5.1, the partition table has clear provisions for unique constraints, and each unique constraint must be included in the partition key (including the primary key constraint) of the partition table.

2. How MySQL partitions handle NULL values

​​If the column where the partition key is located does not have a notnull constraint.

If it is a range partitioned table, then null rows will be stored in the partition with the smallest range.

If it is a list partitioned table, then null rows will be saved to the partition where the list is 0.

In the case of partitioning by HASH and KEY, any expression mysql that produces a NULL value is regarded as its return value of 0.

In order to avoid this situation, it is recommended to set the partition key to NOT NULL.

3. The partition key must be of type INT, or returned by an expression of type INT, which can be NULL. The only exception is when the partition type is KEY partition, other types of columns can be used as partition keys (except BLOB or TEXT columns).

4. Create an index on the partition key of the partitioned table, then the index will also be partitioned. There is no global index for the partition key.

5. Only RANG and LIST partitions can be sub-partitioned, and HASH and KEY partitions cannot be sub-partitioned.

6. Temporary tables cannot be partitioned.



Several methods to obtain mysql partition table information
1. show create table table name
You can view the create statement for creating a partitioned table.

2. show table status
to see if the table is a partitioned table

. 3. View the information_schema.partitions table
select
partition_name part,
partition_expression expr,
partition_description descr,
table_rows
from information_schema.partitions where
table_schema = schema()
and table_name ='test';
You can view which partitions the table has, the method of partitioning, the number of records of data in the partition, etc.

4. explain partitions select
statement This statement displays which partitions are scanned and how they are used.

See Information address: http://x125858805.iteye.com/blog/2068120

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326174748&siteId=291194637