Clickhouse數(shù)據(jù)表、數(shù)據(jù)分區(qū)partition的基本操作代碼
1. 數(shù)據(jù)表的基本操作
只有MergeTree系列、Merge、Distributed表引擎支持alter操作
1.1 添加字段
clickhouse1 :) clickhouse1 :) create table alter_table_test( :-] id UInt32, :-] name String, :-] city String :-] ) engine = MergeTree() :-] order by id; clickhouse1 :) clickhouse1 :) alter table alter_table_test add column if not exists score Float32 default 8.8 after city; clickhouse1 :)
1.2 修改字段數(shù)據(jù)類型、添加或修改字段默認值
clickhouse1 :) clickhouse1 :) alter table alter_table_test modify column if exists score Float64 default 0.0; clickhouse1 :)
修改前后的字段數(shù)據(jù)類型需要兼容
1.3 添加或修改字段備注
clickhouse1 :) clickhouse1 :) alter table alter_table_test comment column if exists score '分數(shù)'; clickhouse1 :)
1.4 刪除字段
clickhouse1 :) clickhouse1 :) alter table alter_table_test drop column if exists score; clickhouse1 :)
1.5 重命名或移動數(shù)據(jù)表
clickhouse1 :) clickhouse1 :) rename table default.alter_table_test to default.alter_table_rename_test; clickhouse1 :)
- 多個db.tb to db.tb用逗號分隔
- 如果源表與目標表數(shù)據(jù)庫不一樣,則表示移動數(shù)據(jù)表, 但數(shù)據(jù)表的移動只能在同一服務器
- 支持on cluster cluster_name操作
1.6 清空數(shù)據(jù)表
clickhouse1 :) clickhouse1 :) truncate table if exists default.alter_table_rename_test; clickhouse1 :)
支持on cluster cluster_name操作
1.7 insert數(shù)據(jù)
2. 數(shù)據(jù)分區(qū)partition的基本操作
測試表和測試數(shù)據(jù)的準備
clickhouse1 :) clickhouse1 :) create table partition_table_test( :-] id UInt32, :-] name String, :-] city String :-] ) engine = MergeTree() :-] order by id :-] partition by city; clickhouse1 :) clickhouse1 :) insert into partition_table_test(id, name, city) values(1, 'name1', 'Beijing'); clickhouse1 :) insert into partition_table_test(id, name, city) values(2, 'name2', 'Shanghai'); clickhouse1 :) clickhouse1 :) create table partition_table_test2( :-] id UInt32, :-] name String, :-] city String :-] ) engine = ReplacingMergeTree() :-] order by id :-] partition by city; clickhouse1 :)
2.1 查詢數(shù)據(jù)表partition相關信息
clickhouse1 :) clickhouse1 :) select database, table, partition, partition_id, name, path from system.parts where database = 'default' and table = 'partition_table_test'; ┌─database─┬─table────────────────┬─partition─┬─partition_id─────────────────────┬─name───────────────────────────────────┬─path────────────────────────────────────────────────────────────────────────────────────────────────────┐ │ default │ partition_table_test │ Shanghai │ 6a9748c898bf80cb661db240706867aa │ 6a9748c898bf80cb661db240706867aa_2_2_0 │ /root/clickhouse/store/9eb/9ebd4336-b065-48ac-9ebd-4336b06588ac/6a9748c898bf80cb661db240706867aa_2_2_0/ │ │ default │ partition_table_test │ Beijing │ 8d2db6c332407299b732139fd8a261c0 │ 8d2db6c332407299b732139fd8a261c0_1_1_0 │ /root/clickhouse/store/9eb/9ebd4336-b065-48ac-9ebd-4336b06588ac/8d2db6c332407299b732139fd8a261c0_1_1_0/ │ └──────────┴──────────────────────┴───────────┴──────────────────────────────────┴────────────────────────────────────────┴─────────────────────────────────────────────────────────────────────────────────────────────────────────┘ clickhouse1 :)
一個partition_id下面存在多個name
2.2 刪除partition
clickhouse1 :) clickhouse1 :) alter table partition_table_test drop partition 'Beijing' :-] ; clickhouse1 :) clickhouse1 :) select * from partition_table_test; ┌─id─┬─name──┬─city─────┐ │ 2 │ name2 │ Shanghai │ └────┴───────┴──────────┘ clickhouse1 :)
上面我們刪除了城市為Beijing的partition,然后再通過insert插入新的數(shù)據(jù),就間接實現(xiàn)了數(shù)據(jù)更新
2.3 復制partition
clickhouse1 :) clickhouse1 :) alter table partition_table_test2 replace partition 'Shanghai' from partition_table_test; clickhouse1 :) clickhouse1 :) select * from partition_table_test2; ┌─id─┬─name──┬─city─────┐ │ 2 │ name2 │ Shanghai │ └────┴───────┴──────────┘ clickhouse1 :)
- 將A表的數(shù)據(jù)partition,復制到B表的條件:
兩張表字段結構完全相同
兩張表partition by、order by一樣
- 會刪除目標表partition_table_test2原來的城市Shanghai partition
2.4 將partition中某一列的數(shù)據(jù)變?yōu)槟J值
clickhouse1 :) clickhouse1 :) alter table partition_table_test clear column name in partition 'Shanghai'; clickhouse1 :) clickhouse1 :) select * from partition_table_test; ┌─id─┬─name─┬─city─────┐ │ 2 │ │ Shanghai │ └────┴──────┴──────────┘ clickhouse1 :)
- 變更字段不能為primary key、order by、partition by定義的字段
- 如果該字段未聲明默認值,則以字段數(shù)據(jù)類型的默認值為準
2.5 partition的卸載和裝載
clickhouse1 :) clickhouse1 :) alter table partition_table_test detach partition 'Shanghai'; clickhouse1 :) clickhouse1 :) select * from partition_table_test; SELECT * FROM partition_table_test Query id: 45460933-7b2e-4389-a056-85d3d75184a8 Ok. 0 rows in set. Elapsed: 0.005 sec. clickhouse1 :) clickhouse1 :) alter table partition_table_test attach partition 'Shanghai'; clickhouse1 :) clickhouse1 :) select * from partition_table_test; ┌─id─┬─name─┬─city─────┐ │ 2 │ │ Shanghai │ └────┴──────┴──────────┘ clickhouse1 :)
- detach后,該分區(qū)目錄被移動到數(shù)據(jù)表目錄的detached目錄下
- clickhouse除了能對detached目錄下的分區(qū)目錄執(zhí)行attach命令, 不能執(zhí)行其它操作
- attach則將detached目錄下的分區(qū)目錄重新移回去
到此這篇關于Clickhouse數(shù)據(jù)表、數(shù)據(jù)分區(qū)partition的基本操作的文章就介紹到這了,更多相關Clickhouse 數(shù)據(jù)分區(qū)partition內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
SQL利用Function創(chuàng)建長整形的唯一ID示例代碼
這篇文章主要給大家介紹了關于SQL利用Function創(chuàng)建長整形的唯一ID的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2018-07-07dapper使用Insert或update時部分字段不映射到數(shù)據(jù)庫
我們在使用dapper的insert或update方法時可能會遇見一些實體中存在的字段但是,數(shù)據(jù)庫中不存在的字段,這樣在使用insert時就是拋出異常提示字段不存在,這個時候該怎么解決呢,下面給大家分享示例實體代碼,感興趣的朋友一起看看吧2023-12-12快速解決openGauss數(shù)據(jù)庫pg_xlog爆滿問題
這篇文章主要介紹了openGauss數(shù)據(jù)庫pg_xlog爆滿問題解決,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-04-04SQL數(shù)據(jù)庫的所有命令(函數(shù)、運算符)匯總大全
結構化查詢語言(Structured?Query?Language)簡稱SQL,結構化查詢語言是一種數(shù)據(jù)庫查詢和程序設計語言,用于存取數(shù)據(jù)以及查詢、更新和管理關系數(shù)據(jù)庫系統(tǒng)。sql語句就是對數(shù)據(jù)庫進行操作的一種語言。2023-01-01時序數(shù)據(jù)庫VictoriaMetrics源碼解析之寫入與索引
這篇文章主要為大家介紹了VictoriaMetrics時序數(shù)據(jù)庫的寫入與索引源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-05-05DBeaver連接GBase數(shù)據(jù)庫的簡單步驟記錄
DBeaver數(shù)據(jù)庫連接工具,是我用了這么久最好用的一個數(shù)據(jù)庫連接工具,擁有的優(yōu)點,支持的數(shù)據(jù)庫多、快捷鍵很贊、導入導出數(shù)據(jù)非常方便,下面這篇文章主要給大家介紹了關于DBeaver連接GBase數(shù)據(jù)庫的簡單步驟,需要的朋友可以參考下2024-03-03