TheDocumentation Index
Fetch the complete documentation index at: https://private-7c7dfe99-page-updates.mintlify.app/llms.txt
Use this file to discover all available pages before exploring further.
CHECK TABLE query in ClickHouse is used to perform a validation check on a specific table or its partitions. It ensures the integrity of the data by verifying the checksums and other internal data structures.
Particularly it compares actual file sizes with the expected values which are stored on the server. If the file sizes do not match the stored values, it means the data is corrupted. This can be caused, for example, by a system crash during query execution.
Syntax
The basic syntax of the query is as follows:table_name: Specifies the name of the table that you want to check.partition_expression: (Optional) If you want to check a specific partition of the table, you can use this expression to specify the partition.part_name: (Optional) If you want to check a specific part in the table, you can add string literal to specify a part name.FORMAT format: (Optional) Allows you to specify the output format of the result.SETTINGS: (Optional) Allows additional settings.- (Optional): check_query_single_value_result: This setting controls if the output is detailed (
0) or summarized (1). - Other settings can be applied as well. If you don’t require a deterministic order for the results, you can set max_threads to a value greater than one to speed up the query.
- (Optional): check_query_single_value_result: This setting controls if the output is detailed (
check_query_single_value_result setting.
In case of check_query_single_value_result = 1 only result column with a single row is returned. Value inside this row is 1 if the integrity check is passed and 0 if data is corrupted.
With check_query_single_value_result = 0 the query returns the following columns:
part_path: Indicates the path to the data part or file name.is_passed: Returns 1 if the check for this part is successful, 0 otherwise.message: Any additional messages related to the check, such as errors or success messages.
CHECK TABLE query supports the following table engines:
Performed over the tables with another table engines causes an NOT_IMPLEMENTED exception.
Engines from the *Log family do not provide automatic data recovery on failure. Use the CHECK TABLE query to track data loss in a timely manner.
Examples
By defaultCHECK TABLE query shows the general table check status:
check_query_single_value_result setting.
Also, to check a specific partition of the table, you can use the PARTITION keyword.
PART keyword.
Receiving a ‘Corrupted’ Result
Remove the existing checksum file:(Replicated)MergeTree tables at once by using the CHECK ALL TABLES query.
If the Data Is Corrupted
If the table is corrupted, you can copy the non-corrupted data to another table. To do this:- Create a new table with the same structure as damaged table. To do this execute the query
CREATE TABLE <new_table_name> AS <damaged_table_name>. - Set the
max_threadsvalue to 1 to process the next query in a single thread. To do this run the querySET max_threads = 1. - Execute the query
INSERT INTO <new_table_name> SELECT * FROM <damaged_table_name>. This request copies the non-corrupted data from the damaged table to another table. Only the data before the corrupted part will be copied. - Restart the
clickhouse-clientto reset themax_threadsvalue.