可以使用用于此类目的 SQL 语句对分区表执行许多表和分区维护任务。A number of table and partition maintenance tasks can be carried out on partitioned tables using SQL statements intended for such purposes.
分区表的表维护可以使用以下语句完成:CHECK TABLE、OPTIMIZE TABLE、ANALYZE TABLE 和 REPAIR TABLE,这些语句支持分区表。Table maintenance of partitioned tables can be accomplished
using the statements CHECK TABLE,
OPTIMIZE TABLE,
ANALYZE TABLE, and
REPAIR TABLE, which are supported
for partitioned tables.
您可以使用对 ALTER
TABLE 的多种扩展,直接对一个或多个分区执行此类操作,如下列表所述:You can use a number of extensions to
ALTER
TABLE for performing operations of this type on one or
more partitions directly, as described in the following list:
重建分区。 重建分区;这与删除分区中存储的所有记录然后重新插入它们具有相同的效果。这对于碎片整理是有用的。Rebuilding partitions. Rebuilds the partition; this has the same effect as dropping all records stored in the partition, then reinserting them. This can be useful for purposes of defragmentation.
示例:Example:
ALTER TABLE t1 REBUILD PARTITION p0, p1;
优化分区。 如果您从分区中删除了大量行,或者对具有可变长度行的分区表(即具有
VARCHAR、BLOB或TEXT列)进行了多次更改,您可以使用ALTER TABLE ... OPTIMIZE PARTITION来回收未使用的空间并对分区数据文件进行碎片整理。Optimizing partitions. If you have deleted a large number of rows from a partition or if you have made many changes to a partitioned table with variable-length rows (that is, havingVARCHAR,BLOB, orTEXTcolumns), you can useALTER TABLE ... OPTIMIZE PARTITIONto reclaim any unused space and to defragment the partition data file.示例:Example:
ALTER TABLE t1 OPTIMIZE PARTITION p0, p1;
在给定分区上使用
OPTIMIZE PARTITION相当于对该分区运行CHECK PARTITION、ANALYZE PARTITION和REPAIR PARTITION。UsingOPTIMIZE PARTITIONon a given partition is equivalent to runningCHECK PARTITION,ANALYZE PARTITION, andREPAIR PARTITIONon that partition.一些 MySQL 存储引擎,包括
InnoDB,不支持按分区优化;在这些情况下,ALTER TABLE ... OPTIMIZE PARTITION会分析并重建整个表,并发出相应警告。(Bug #11751825, Bug #42822)请改用ALTER TABLE ... REBUILD PARTITION和ALTER TABLE ... ANALYZE PARTITION来避免此问题。Some MySQL storage engines, includingInnoDB, do not support per-partition optimization; in these cases,ALTER TABLE ... OPTIMIZE PARTITIONanalyzes and rebuilds the entire table, and causes an appropriate warning to be issued. (Bug #11751825, Bug #42822) UseALTER TABLE ... REBUILD PARTITIONandALTER TABLE ... ANALYZE PARTITIONinstead, to avoid this issue.分析分区。 这会读取并存储分区的键分布。Analyzing partitions. This reads and stores the key distributions for partitions.
示例:Example:
ALTER TABLE t1 ANALYZE PARTITION p3;
修复分区。 这会修复损坏的分区。Repairing partitions. This repairs corrupted partitions.
示例:Example:
ALTER TABLE t1 REPAIR PARTITION p0,p1;
通常,当分区包含重复键错误时,
REPAIR PARTITION会失败。你可以在此选项下使用ALTER IGNORE TABLE,在这种情况下,所有因存在重复键而无法移动的行都会从分区中移除(Bug #16900947)。Normally,REPAIR PARTITIONfails when the partition contains duplicate key errors. You can useALTER IGNORE TABLEwith this option, in which case all rows that cannot be moved due to the presence of duplicate keys are removed from the partition (Bug #16900947).检查分区。 你可以以类似于对非分区表使用
CHECK TABLE的方式检查分区中的错误。Checking partitions. You can check partitions for errors in much the same way that you can useCHECK TABLEwith nonpartitioned tables.示例:Example:
ALTER TABLE trb3 CHECK PARTITION p1;
此语句可以告诉您表
t1的分区p1中的数据或索引是否已损坏。如果是这种情况,请使用ALTER TABLE ... REPAIR PARTITION来修复分区。This statement tells you whether the data or indexes in partitionp1of tablet1are corrupted. If this is the case, useALTER TABLE ... REPAIR PARTITIONto repair the partition.通常,当分区中存在重复键错误时,
CHECK PARTITION会失败。您可以使用带有此选项的ALTER IGNORE TABLE,在这种情况下,该语句会返回在分区中发现重复键违规的每一行的内容。仅报告表分区表达式中列的值。(Bug #16900947)Normally,CHECK PARTITIONfails when the partition contains duplicate key errors. You can useALTER IGNORE TABLEwith this option, in which case the statement returns the contents of each row in the partition where a duplicate key violation is found. Only the values for the columns in the partitioning expression for the table are reported. (Bug #16900947)
上面列表中的每个语句也支持使用关键字 ALL 来替代分区名称列表。使用 ALL 会使该语句作用于表中的所有分区。Each of the statements in the list just shown also supports the
keyword ALL in place of the list of partition
names. Using ALL causes the statement to act
on all partitions in the table.
您也可以使用 ALTER TABLE ...
TRUNCATE PARTITION 来截断分区。该语句可以用来删除一个或多个分区中的所有行,其方式与 TRUNCATE TABLE 删除表中所有行类似。You can also truncate partitions using
ALTER TABLE ...
TRUNCATE PARTITION. This statement can be used to
delete all rows from one or more partitions in much the same way
that TRUNCATE TABLE deletes all
rows from a table.
ALTER TABLE ...
TRUNCATE PARTITION ALL 会截断表中的所有分区。ALTER TABLE ...
TRUNCATE PARTITION ALL truncates all partitions in the
table.