13.7.3.2 CHECK TABLE Statement语句

CHECK TABLE tbl_name [, tbl_name] ... [option] ...
option: {
    FOR UPGRADE
  | QUICK
  | FAST
  | MEDIUM
  | EXTENDED
  | CHANGED
}

CHECK TABLE checks a table or tables for errors. CHECK TABLE can also check views for problems, such as tables that are referenced in the view definition that no longer exist.CHECK TABLE检查一个或多个表是否有错误。CHECK TABLE还可以检查视图中是否存在问题,例如视图定义中引用的不再存在的表。

To check a table, you must have some privilege for it.若要检查表,您必须拥有该表的某些特权。

CHECK TABLE works for InnoDB, MyISAM, ARCHIVE, and CSV tables.CHECK TABLE适用于InnoDBMyISAMARCHIVECSV表。

Before running CHECK TABLE on InnoDB tables, see CHECK TABLE Usage Notes for InnoDB Tables.InnoDB表上运行CHECK TABLE之前,请参阅InnoDB表的CHECK TABLE 使用说明

CHECK TABLE is supported for partitioned tables, and you can use ALTER TABLE ... CHECK PARTITION to check one or more partitions; for more information, see Section 13.1.9, “ALTER TABLE Statement”, and Section 24.3.4, “Maintenance of Partitions”.分区表支持CHECK TABLE,并且可以使用ALTER TABLE ... CHECK PARTITION可检查一个或多个分区;有关更多信息,请参阅第13.1.9节,“ALTER TABLE语句”第24.3.4节,“分区的维护”

CHECK TABLE ignores virtual generated columns that are not indexed.忽略未编入索引的虚拟生成列。

CHECK TABLE Output输出

CHECK TABLE returns a result set with the columns shown in the following table.返回一个结果集,其中的列如下表所示。

ColumnValue
TableThe table name表名称
OpAlways check始终是check
Msg_typestatus, error, info, note, or warning
Msg_textAn informational message一条信息性消息

The statement might produce many rows of information for each checked table. 该语句可能会为每个选中的表生成许多行信息。The last row has a Msg_type value of status and the Msg_text normally should be OK. 最后一行的Msg_type值为statusMsg_text通常应为OKTable is already up to date means that the storage engine for the table indicated that there was no need to check the table.Table is already up to date意味着该表的存储引擎指示无需检查该表。

Checking Version Compatibility检查版本兼容性

The FOR UPGRADE option checks whether the named tables are compatible with the current version of MySQL. FOR UPGRADE选项检查命名表是否与当前版本的MySQL兼容。With FOR UPGRADE, the server checks each table to determine whether there have been any incompatible changes in any of the table's data types or indexes since the table was created. If not, the check succeeds. 使用FOR UPGRADE,服务器会检查每个表,以确定自创建表以来,表的任何数据类型或索引中是否存在任何不兼容的更改。否则,检查成功。Otherwise, if there is a possible incompatibility, the server runs a full check on the table (which might take some time).否则,如果可能存在不兼容,服务器会对表进行全面检查(这可能需要一些时间)。

Incompatibilities might occur because the storage format for a data type has changed or because its sort order has changed. 由于数据类型的存储格式已更改或其排序顺序已更改,可能会发生不兼容。Our aim is to avoid these changes, but occasionally they are necessary to correct problems that would be worse than an incompatibility between releases.目标是避免这些更改,但偶尔它们对于纠正比版本之间的不兼容更糟糕的问题是必要的。

FOR UPGRADE discovers these incompatibilities:发现这些不兼容性:

  • The indexing order for end-space in TEXT columns for InnoDB and MyISAM tables changed between MySQL 4.1 and 5.0.InnoDBMyISAM表的TEXT列中结束空间的索引顺序在MySQL 4.1和5.0之间发生了变化。

  • The storage method of the new DECIMAL data type changed between MySQL 5.0.3 and 5.0.5.DECIMAL数据类型的存储方法在MySQL 5.0.3和5.0.5之间发生了变化。

  • Changes are sometimes made to character sets or collations that require table indexes to be rebuilt. 有时会对需要重新生成表索引的字符集或排序规则进行更改。For details about such changes, see Section 2.11.4, “Changes in MySQL 8.0”. 有关这些更改的详细信息,请参阅第2.11.4节,“MySQL 8.0中的更改”For information about rebuilding tables, see Section 2.11.13, “Rebuilding or Repairing Tables or Indexes”.有关重建表的信息,请参阅第2.11.13节,“重建或修复表或索引”

  • MySQL 8.0 does not support the 2-digit YEAR(2) data type permitted in older versions of MySQL. MySQL 8.0不支持旧版本MySQL中允许的2位数YEAR(2)数据类型。For tables containing YEAR(2) columns, CHECK TABLE recommends REPAIR TABLE, which converts 2-digit YEAR(2) columns to 4-digit YEAR columns.

  • Trigger creation time is maintained.触发器创建时间保持不变。

  • A table is reported as needing a rebuild if it contains old temporal columns in pre-5.6.4 format (TIME, DATETIME, and TIMESTAMP columns without support for fractional seconds precision) and the avoid_temporal_upgrade system variable is disabled. This helps the MySQL upgrade procedure detect and upgrade tables containing old temporal columns. If avoid_temporal_upgrade is enabled, FOR UPGRADE ignores the old temporal columns present in the table; consequently, the upgrade procedure does not upgrade them.

    To check for tables that contain such temporal columns and need a rebuild, disable avoid_temporal_upgrade before executing CHECK TABLE ... FOR UPGRADE.若要检查包含此类临时列并需要重新生成的表,请在执行CHECK TABLE ... FOR UPGRADE之前禁用avoid_temporal_upgrade

  • Warnings are issued for tables that use nonnative partitioning because nonnative partitioning is removed in MySQL 8.0. See Chapter 24, Partitioning.对使用非本机分区的表发出警告,因为MySQL 8.0中删除了非本机划分。参见第24章,“分区”

Checking Data Consistency检查数据一致性

The following table shows the other check options that can be given. These options are passed to the storage engine, which may use or ignore them.下表显示了可以提供的其他检查选项。这些选项将传递给存储引擎,存储引擎可能会使用或忽略它们。

TypeMeaning
QUICKDo not scan the rows to check for incorrect links. 不要扫描行以检查不正确的链接。Applies to InnoDB and MyISAM tables and views.适用于InnoDBMyISAM表和视图。
FASTCheck only tables that have not been closed properly. Ignored for InnoDB; applies only to MyISAM tables and views.只检查未正确关闭的表。InnoDB忽略;仅适用于MyISAM表和视图。
CHANGEDCheck only tables that have been changed since the last check or that have not been closed properly. 只检查自上次检查以来已更改或未正确关闭的表。Ignored for InnoDB; applies only to MyISAM tables and views.InnoDB忽略;仅适用于MyISAM表和视图。
MEDIUMScan rows to verify that deleted links are valid. This also calculates a key checksum for the rows and verifies this with a calculated checksum for the keys. 扫描行以验证已删除的链接是否有效。这还会计算行的密钥校验和,并使用计算的密钥校验值进行验证。Ignored for InnoDB; applies only to MyISAM tables and views.InnoDB忽略;仅适用于MyISAM表和视图。
EXTENDEDDo a full key lookup for all keys for each row. 对每一行的所有关键点进行完整的关键点查找。This ensures that the table is 100% consistent, but takes a long time. 这样可以确保表是100%一致的,但需要很长时间。Ignored for InnoDB; applies only to MyISAM tables and views.InnoDB忽略;仅适用于MyISAM表和视图。

You can combine check options, as in the following example that does a quick check on the table to determine whether it was closed properly:您可以组合检查选项,如以下示例所示,该示例对表进行快速检查以确定表是否正确关闭:

CHECK TABLE test_table FAST QUICK;
Note注意

If CHECK TABLE finds no problems with a table that is marked as corrupted or not closed properly, CHECK TABLE may remove the mark.如果CHECK TABLE没有发现标记为“损坏”或“未正确关闭”的表有问题,CHECK TABLE可能会删除该标记。

If a table is corrupted, the problem is most likely in the indexes and not in the data part. All of the preceding check types check the indexes thoroughly and should thus find most errors.如果表损坏,问题很可能出现在索引中,而不是数据部分。前面所有的检查类型都会彻底检查索引,因此应该会发现大多数错误。

To check a table that you assume is okay, use no check options or the QUICK option. 要检查您认为可以检查的表,请不使用检查选项或QUICK选项。The latter should be used when you are in a hurry and can take the very small risk that QUICK does not find an error in the data file. (In most cases, under normal usage, MySQL should find any error in the data file. 后者应该在您匆忙的时候使用,并且可以承担QUICK在数据文件中没有发现错误的非常小的风险。(在大多数情况下,在正常使用情况下,MySQL应该会在数据文件中发现任何错误。If this happens, the table is marked as corrupted and cannot be used until it is repaired.)如果发生这种情况,表将被标记为“已损坏”,在修复之前无法使用。)

FAST and CHANGED are mostly intended to be used from a script (for example, to be executed from cron) to check tables periodically. In most cases, FAST is to be preferred over CHANGED. (The only case when it is not preferred is when you suspect that you have found a bug in the MyISAM code.)

EXTENDED is to be used only after you have run a normal check but still get errors from a table when MySQL tries to update a row or find a row by key. This is very unlikely if a normal check has succeeded.

Use of CHECK TABLE ... EXTENDED might influence execution plans generated by the query optimizer.

Some problems reported by CHECK TABLE cannot be corrected automatically:

  • Found row where the auto_increment column has the value 0.

    This means that you have a row in the table where the AUTO_INCREMENT index column contains the value 0. (It is possible to create a row where the AUTO_INCREMENT column is 0 by explicitly setting the column to 0 with an UPDATE statement.)

    This is not an error in itself, but could cause trouble if you decide to dump the table and restore it or do an ALTER TABLE on the table. In this case, the AUTO_INCREMENT column changes value according to the rules of AUTO_INCREMENT columns, which could cause problems such as a duplicate-key error.

    To get rid of the warning, execute an UPDATE statement to set the column to some value other than 0.

CHECK TABLE Usage Notes for InnoDB Tables

The following notes apply to InnoDB tables:

  • If CHECK TABLE encounters a corrupt page, the server exits to prevent error propagation (Bug #10132). If the corruption occurs in a secondary index but table data is readable, running CHECK TABLE can still cause a server exit.

  • If CHECK TABLE encounters a corrupted DB_TRX_ID or DB_ROLL_PTR field in a clustered index, CHECK TABLE can cause InnoDB to access an invalid undo log record, resulting in an MVCC-related server exit.

  • If CHECK TABLE encounters errors in InnoDB tables or indexes, it reports an error, and usually marks the index and sometimes marks the table as corrupted, preventing further use of the index or table. Such errors include an incorrect number of entries in a secondary index or incorrect links.

  • If CHECK TABLE finds an incorrect number of entries in a secondary index, it reports an error but does not cause a server exit or prevent access to the file.

  • CHECK TABLE surveys the index page structure, then surveys each key entry. It does not validate the key pointer to a clustered record or follow the path for BLOB pointers.

  • When an InnoDB table is stored in its own .ibd file, the first 3 pages of the .ibd file contain header information rather than table or index data. The CHECK TABLE statement does not detect inconsistencies that affect only the header data. To verify the entire contents of an InnoDB .ibd file, use the innochecksum command.

  • When running CHECK TABLE on large InnoDB tables, other threads may be blocked during CHECK TABLE execution. To avoid timeouts, the semaphore wait threshold (600 seconds) is extended by 2 hours (7200 seconds) for CHECK TABLE operations. If InnoDB detects semaphore waits of 240 seconds or more, it starts printing InnoDB monitor output to the error log. If a lock request extends beyond the semaphore wait threshold, InnoDB aborts the process. To avoid the possibility of a semaphore wait timeout entirely, run CHECK TABLE QUICK instead of CHECK TABLE.

  • CHECK TABLE functionality for InnoDB SPATIAL indexes includes an R-tree validity check and a check to ensure that the R-tree row count matches the clustered index.

  • CHECK TABLE supports secondary indexes on virtual generated columns, which are supported by InnoDB.

  • As of MySQL 8.0.14, InnoDB supports parallel clustered index reads, which can improve CHECK TABLE performance. InnoDB reads the clustered index twice during a CHECK TABLE operation. The second read can be performed in parallel. The innodb_parallel_read_threads session variable must be set to a value greater than 1 for parallel clustered index reads to occur. The default value is 4. The actual number of threads used to perform a parallel clustered index read is determined by the innodb_parallel_read_threads setting or the number of index subtrees to scan, whichever is smaller.

CHECK TABLE Usage Notes for MyISAM Tables

The following notes apply to MyISAM tables:

  • CHECK TABLE updates key statistics for MyISAM tables.

  • If CHECK TABLE output does not return OK or Table is already up to date, you should normally run a repair of the table. See Section 7.6, “MyISAM Table Maintenance and Crash Recovery”.

  • If none of the CHECK TABLE options QUICK, MEDIUM, or EXTENDED are specified, the default check type for dynamic-format MyISAM tables is MEDIUM. This has the same result as running myisamchk --medium-check tbl_name on the table. The default check type also is MEDIUM for static-format MyISAM tables, unless CHANGED or FAST is specified. In that case, the default is QUICK. The row scan is skipped for CHANGED and FAST because the rows are very seldom corrupted.