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ù)類型、添加或修改字段默認(rèn)值
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 '分?jǐn)?shù)'; clickhouse1 :)
1.4 刪除字段
clickhouse1 :) clickhouse1 :) alter table alter_table_test drop column if exists score; clickhouse1 :)
1.5 重命名或移動(dòng)數(shù)據(jù)表
clickhouse1 :) clickhouse1 :) rename table default.alter_table_test to default.alter_table_rename_test; clickhouse1 :)
- 多個(gè)db.tb to db.tb用逗號(hào)分隔
- 如果源表與目標(biāo)表數(shù)據(jù)庫(kù)不一樣,則表示移動(dòng)數(shù)據(jù)表, 但數(shù)據(jù)表的移動(dòng)只能在同一服務(wù)器
- 支持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的基本操作
測(cè)試表和測(cè)試數(shù)據(jù)的準(zhǔn)備
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相關(guān)信息
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 :)
一個(gè)partition_id下面存在多個(gè)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,然后再通過(guò)insert插入新的數(shù)據(jù),就間接實(shí)現(xiàn)了數(shù)據(jù)更新
2.3 復(fù)制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,復(fù)制到B表的條件:
兩張表字段結(jié)構(gòu)完全相同
兩張表partition by、order by一樣
- 會(huì)刪除目標(biāo)表partition_table_test2原來(lái)的城市Shanghai partition
2.4 將partition中某一列的數(shù)據(jù)變?yōu)槟J(rèn)值
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定義的字段
- 如果該字段未聲明默認(rèn)值,則以字段數(shù)據(jù)類型的默認(rèn)值為準(zhǔn)
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ū)目錄被移動(dòng)到數(shù)據(jù)表目錄的detached目錄下
- clickhouse除了能對(duì)detached目錄下的分區(qū)目錄執(zhí)行attach命令, 不能執(zhí)行其它操作
- attach則將detached目錄下的分區(qū)目錄重新移回去
到此這篇關(guān)于Clickhouse數(shù)據(jù)表、數(shù)據(jù)分區(qū)partition的基本操作的文章就介紹到這了,更多相關(guān)Clickhouse 數(shù)據(jù)分區(qū)partition內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SQL中過(guò)濾條件放on和where中的區(qū)別詳解
這篇文章主要給大家介紹了關(guān)于SQL中過(guò)濾條件放on和where中的區(qū)別,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起看看吧2019-01-01
Dbeaver做數(shù)據(jù)遷移的詳細(xì)過(guò)程記錄
DBeaver是一款跨平臺(tái)的通用數(shù)據(jù)庫(kù)開(kāi)源管理工具,支持 MySQL,PostgreSQL,Oracle,DB2,MSSQL,Sybase,Mimer,HSQLDB,Derby以及其他兼容JDBC的數(shù)據(jù)庫(kù),下面這篇文章主要給大家介紹了關(guān)于Dbeaver做數(shù)據(jù)遷移的詳細(xì)過(guò)程,需要的朋友可以參考下2023-05-05
淺談數(shù)據(jù)庫(kù)緩存最終一致性的四種方案
緩存是軟件開(kāi)發(fā)中一個(gè)非常有用的概念,數(shù)據(jù)庫(kù)緩存更是在項(xiàng)目中必然會(huì)遇到的場(chǎng)景,緩存一致性的保證,更是在面試中被反復(fù)問(wèn)到。下面我們就一起來(lái)了解一下2021-04-04
數(shù)據(jù)庫(kù)測(cè)試 實(shí)用技巧及測(cè)試方法
軟件應(yīng)用程序已經(jīng)離不開(kāi)數(shù)據(jù)庫(kù)。無(wú)論是在Web、桌面應(yīng)用、客戶端服務(wù)器、企業(yè)和個(gè)人業(yè)務(wù),都需要數(shù)據(jù)庫(kù)在后端操作。2011-07-07
SQLite不支持Right Join的解決辦法GROUP BY
sqlite真的不錯(cuò),就是不支持right join,所以我們用下面的方法解決2008-06-06
postgres 數(shù)據(jù)庫(kù)中的數(shù)據(jù)轉(zhuǎn)換
postgres8.3以后,字段數(shù)據(jù)之間的默認(rèn)轉(zhuǎn)換取消了。如果需要進(jìn)行數(shù)據(jù)變換的話,在postgres數(shù)據(jù)庫(kù)中,我們可以用"::"來(lái)進(jìn)行字段數(shù)據(jù)的類型轉(zhuǎn)換。2009-07-07
銀河麒麟V10安裝達(dá)夢(mèng)8數(shù)據(jù)庫(kù)詳細(xì)操作過(guò)程及避坑
在數(shù)字化轉(zhuǎn)型的浪潮下,數(shù)據(jù)庫(kù)作為企業(yè)核心數(shù)據(jù)存儲(chǔ)與管理的基石,其安全性、穩(wěn)定性和自主可控性顯得尤為重要,這篇文章主要介紹了銀河麒麟V10安裝達(dá)夢(mèng)8數(shù)據(jù)庫(kù)詳細(xì)操作過(guò)程及避坑的相關(guān)資料,需要的朋友可以參考下2026-04-04

