|NO.Z.00033|——————————|BigDataEnd|——|Hadoop&OLAP_ClickHouse.V05|——|ClickHouse.v05|ClickHouse语法|数据分区|基本操作|
一、数据分区的基本操作:
### --- 查询分区信息:
~~~ ClickHouse内置了很多system系统表,用于查询自身状态信息。
### --- 查询分区表信息
~~~ # 查看分区表信息
hadoop01 :) use system;
hadoop01 :) show tables;
┌─name───────────────────────────┐
│ aggregate_function_combinators │
│ asynchronous_metric_log │
│ asynchronous_metrics │
│ build_options │
│ clusters │
│ collations │
│ columns │
│ contributors │
│ current_roles │
│ data_type_families │
│ databases │
│ detached_parts │
│ dictionaries │
│ disks │
│ distribution_queue │
│ enabled_roles │
│ events │
│ formats │
│ functions │
│ grants │
│ graphite_retentions │
│ licenses │
│ macros │
│ merge_tree_settings │
│ merges │
│ metric_log │
│ metrics │
│ models │
│ mutations │
│ numbers │
│ numbers_mt │
│ one │
│ partition_v1 │
│ parts │
│ parts_columns │
│ privileges │
│ processes │
│ query_log │
│ query_thread_log │
│ quota_limits │
│ quota_usage │
│ quotas │
│ quotas_usage │
│ replicas │
│ replication_queue │
│ role_grants │
│ roles │
│ row_policies │
│ settings │
│ settings_profile_elements │
│ settings_profiles │
│ stack_trace │
│ storage_policies │
│ table_engines │
│ table_functions │
│ tables │
│ trace_log │
│ users │
│ zeros │
│ zeros_mt │
│ zookeeper │
└────────────────────────────────┘
### --- 其中parts是专门用来查询分区信息的表
hadoop01 :) desc parts;
┌─name──────────────────────────────────┬─type────────────┬─default_type─┬─default_expression─┬─comment─┬─codec_expression─┬─ttl_expression─┐
│ 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 │ ALIAS │ bytes_on_disk │ │ │ │
│ marks_size │ UInt64 │ ALIAS │ marks_bytes │ │ │ │
└───────────────────────────────────────┴─────────────────┴──────────────┴────────────────────┴─────────┴──────────────────┴────────────────┘
### --- 删除指定分区:
~~~ # 查看某一表现有分区
hadoop01 :) select partition_id,name,table,database from system.parts where table = 'partition_v1';
┌─partition_id─┬─name───────────┬─table────────┬─database─┐
│ 202110 │ 202110_1_1_0_4 │ partition_v1 │ system │
│ 202111 │ 202111_2_2_0_4 │ partition_v1 │ system │
└──────────────┴────────────────┴──────────────┴──────────┘
~~~ # 删除指定分区
hadoop01 :) alter table partition_v1 drop partition 202110;
~~~ # 查看表中的分区是否删除
~~~ 可以利用删除完成更新操作---先删除,再insert插入
hadoop01 :) select partition_id,name,table,database from system.parts where table = 'partition_v1';
┌─partition_id─┬─name───────────┬─table────────┬─database─┐
│ 202111 │ 202111_2_2_0_4 │ partition_v1 │ system │
└──────────────┴────────────────┴──────────────┴──────────┘
### --- 复制分区数据
~~~ 需要满足两个条件
~~~ 两张表需要有相同的分区键
~~~ 两张表需要有相同的表结构
~~~ # 复制分区数据
hadoop01 :) alter table partition_v2 replace partition 202110 from partition_v1;
hadoop01 :) select partition_id,name,table,database from system.parts where table = 'partition_v2';
┌─partition_id─┬─name─────────┬─table────────┬─database─┐
│ 202110 │ 202110_2_2_0 │ partition_v2 │ default │
└──────────────┴──────────────┴──────────────┴──────────┘
### --- 重置分区数据
hadoop01 :) alter table partition_v1 clear clumn URL in partition 202110
### --- 卸载和装载分区
~~~ 卸载:alter table ...detach..
~~~ 装载: alter table ...attach...
~~~ # 查看表分区情况
hadoop01 :) select partition_id,name,table,database from system.parts where table = 'partition_v1';
┌─partition_id─┬─name───────────┬─table────────┬─database─┐
│ 202110 │ 202110_1_1_0 │ partition_v1 │ default │
│ 202111 │ 202111_2_2_0_4 │ partition_v1 │ system │
│ 202111 │ 202111_2_2_0_4 │ partition_v1 │ system │
└──────────────┴────────────────┴──────────────┴──────────┘
~~~ # 卸载分区
hadoop01 :) alter table partition_v1 detach partition 202110
hadoop01 :) alter table partition_v1 attach partition 202111
~~~ # 查看现有分区情况
hadoop01 :) select partition_id,name,table,database from system.parts where table = 'partition_v1';
┌─partition_id─┬─name───────────┬─table────────┬─database─┐
│ 202111 │ 202111_2_2_0_4 │ partition_v1 │ system │
└──────────────┴────────────────┴──────────────┴──────────┘
Walter Savage Landor:strove with none,for none was worth my strife.Nature I loved and, next to Nature, Art:I warm'd both hands before the fire of life.It sinks, and I am ready to depart
——W.S.Landor
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· DeepSeek 开源周回顾「GitHub 热点速览」