日常工作中需要观察Clickhouse的磁盘使用情况,查看文档后发现基于system.parts
表可以查询相关信息。
需要注意的是,systems.parts
只可以查询MergeTree
系列的表的情况。
systems.parts
字段说明见文末。
相关查询
查看数据库的磁盘使用情况及数据行数
查询本机整个数据库所有表总共的情况
select \
sum(rows) as row, \
formatReadableSize(sum(bytes_on_disk)) as used_disk, \
formatReadableSize(sum(data_uncompressed_bytes)) as brfore_compress, \
formatReadableSize(sum(data_compressed_bytes)) as after_compress, \
round(sum(data_compressed_bytes) / sum(data_uncompressed_bytes) * 100, 0) compress_rate \
from system.parts
查询所有表某一天的磁盘使用情况
查询本机所有表在指定日期的磁盘使用、数据量情况。
select \
table, \
sum(rows) as row, \
formatReadableSize(sum(bytes_on_disk)) as used_disk, \
formatReadableSize(sum(data_uncompressed_bytes)) as before_compress, \
formatReadableSize(sum(data_compressed_bytes)) as after_compress, \
round(sum(data_compressed_bytes) / sum(data_uncompressed_bytes) * 100, 0) compress_rate \
from system.parts \
where partition = '20210719000000' \
group by table \
order by row desc \
;
基于system.parts
创建分布式表
因为clickhouse是集群部署的的,所以需要查看每台机器上的相关信息。
所以就得去每台机器上执行查询,这样太麻烦了。
我们可以基于system.parts
表创建分布式表,就可以查询分布式表获取全部机器的信息。
创建分布式表的语句
基于system.parts
创建名为system.dist_parts
的分布式表。
CREATE TABLE system.dist_parts ( \
partition String, \
name String, \
part_type String, \
active UInt8, \
marks UInt64, \
rows UInt64, \
bytes_on_disk UInt64, \
data_compressed_bytes UInt64, \
data_uncompressed_bytes UInt64, \
marks_bytes UInt64, \
modification_time DateTime, \
remove_time DateTime, \
refcount UInt32, \
min_date Date, \
max_date Date, \
min_time DateTime, \
max_time DateTime, \
partition_id String, \
min_block_number Int64, \
max_block_number Int64, \
level UInt32, \
data_version UInt64, \
primary_key_bytes_in_memory UInt64, \
primary_key_bytes_in_memory_allocated UInt64, \
is_frozen UInt8, \
database String, \
table String, \
engine String, \
disk_name String, \
path String, \
hash_of_all_files String, \
hash_of_uncompressed_files String, \
uncompressed_hash_of_compressed_files String, \
delete_ttl_info_min DateTime, \
delete_ttl_info_max DateTime, \
`move_ttl_info.expression` Array(String), \
`move_ttl_info.min` Array(DateTime), \
`move_ttl_info.max` Array(DateTime), \
bytes UInt64, \
marks_size UInt64 \
) \
ENGINE = Distributed('shard_base', 'system', 'parts', rand());
查询分布式表获取所有机器某天的数据量
select \
'all' as table, \
hostname() as hostname, \
partition as partition, \
sum(rows) as row, \
formatReadableSize(sum(bytes_on_disk)) as used_disk, \
formatReadableSize(sum(data_uncompressed_bytes)) as before_compress, \
formatReadableSize(sum(data_compressed_bytes)) as after_compress, \
round(sum(data_compressed_bytes) / sum(data_uncompressed_bytes) * 100, 0) compress_rate \
from system.dist_parts \
where toInt64OrZero(partition) >= 20210718000000 and toInt64OrZero(partition) < 20210719000000 \
group by partition, hostname() \
order by partition, hostname asc \
;
system.parts
字段信息
partition (String) – 分区的名称。要了解分区是什么,参考ALTER查询的描述。格式:
YYYYMM 按月自动分区。
any_string 当手动分区。
name (String) – 数据片段的名称。
active (UInt8) – 指示数据片段是否处于激活状态的标志。如果数据片段是激活的,则在表中可以使用它。否则它是被删除状态。未激活的数据片段在合并后仍然存在。
marks (UInt64) – 标记的数量。要获得数据片段的大概行数,可以用索引粒度乘以标记(通常是8192)(这个提示不适用于自适应粒度)。
rows (UInt64) – 行数。
bytes_on_disk (UInt64) – 所有数据片段文件的总大小,单位字节。
data_compressed_bytes (UInt64) – 数据片段占用压缩数据的总大小。所有的附属文件不包括在内(例如,标记文件)。
data_uncompressed_bytes (UInt64) – 数据片段中未压缩数据的总大小。所有的附属文件不包括在内(例如,标记文件)。
marks_bytes (UInt64) – 标记的文件的大小。
modification_time (DateTime) – 修改包含数据片段的目录的时间。这通常对应于创建数据部分的时间。
remove_time (DateTime) – 数据片段变为非激活状态的时间。
refcount (UInt32) – 使用数据片段的位置数量。大于2的值表示数据部分用于查询或合并。
min_date (Date) – 数据片段中日期键的最小值。
max_date (Date) – 数据片段中日期键的最大值。
min_time (DateTime) – 数据片段中日期和时间键的最小值。
max_time(DateTime) – 数据片段中日期和时间键的最小值。
partition_id (String) – 分区的ID。
min_block_number (UInt64) – 合并后组成当前片段的最小数据块编号。
max_block_number (UInt64) – 合并后组成当前片段的最大数据块编号。
level (UInt32) – 合并树的深度。零意味着当前片段是通过插入创建的,而不是通过合并其他片段创建的。
data_version (UInt64) – 用于确定应用于数据片段变化的数字(变化版本高于data_version)。
primary_key_bytes_in_memory (UInt64) – 主键值使用的内存量(以字节为单位)。
primary_key_bytes_in_memory_allocated (UInt64) – 为主键值保留的内存量(以字节为单位)。
is_frozen (UInt8) – 表明存在分区数据备份的标志。1、备份存在。0,备份不存在。更多详细信息,请参考FREEZE PARTITION
database (String) – 数据库名称
table (String) – 表名
engine (String) – 表引擎,没有参数。
path (String) – 数据片段文件的文件夹绝对路径。
disk (String) – 存储数据部分的磁盘的名称。
hash_of_all_files (String) – sipHash128压缩文件。
hash_of_uncompressed_files (String) – sipHash128的未压缩文件(带有标记文件、索引文件等)。
uncompressed_hash_of_compressed_files (String) – sipHash128压缩文件中的数据,就好像它们是未压缩的一样。
bytes (UInt64) – bytes_on_disk别名。
marks_size (UInt64) – marks_bytes别名。
参考
官方文档说明 https://clickhouse.tech/docs/en/operations/system-tables/parts/
clickhouse系统表 https://blog.csdn.net/anyitian/article/details/115933733