在clickhouse中更新和刪除


ck 目前支持了更新和刪除,但是與傳統sql語法 略有不同,我也記錄下來,防止后面忘記。

測試數據

:) select count(*) from system.columns where table='test_update';

┌─count()─┐
│     332 │
└─────────┘

:) select count(*) from test_update;

┌──count()─┐
│ 17925050 │
└──────────┘

 

具體刪除&更新實現

語法 如下:

ALTER TABLE <table_name> DELETE WHERE <filter>;

and

ALTER TABLE <table_name> UPDATE col1 = expr1, ... WHERE <filter>;

示例

:) select event_status_key, count(*) from test_update where event_status_key in (0, 22) group by event_status_key;

┌─event_status_key─┬──count()─┐
│                017824710 │
│               221701 │
└──────────────────┴──────────┘

 

假設event_status_key= 22的數據都是錯誤數據,我們需要修復這個問題
:) ALTER TABLE test_update UPDATE event_status_key=0 where event_status_key=22;

0 rows in set. Elapsed: 0.067 sec.

如上,反饋很及時,但是更新是異步的,可能需要等一會,看下結果:

:) select event_status_key, count(*) from test_update where event_status_key in (0, 22) group by event_status_key;

 ┌─event_status_key─┬──count()─┐
 │                017826411 │
 └──────────────────┴──────────┘

返回結果正確,這個更新操作會被記錄到system.mutations 表里面:

:) select * from system.mutations where table='test_update';

Row 1:
──────
database:                   test
table:                      test_update
mutation_id:                mutation_162.txt
command:                    UPDATE event_status_key = 0 WHERE event_status_key = 22
create_time:                2018-10-12 12:39:32
block_numbers.partition_id: ['']
block_numbers.number:       [162]
parts_to_do:                0
is_done:                    1

注意:
1. 該命令必須在版本號大於1.1.54388才可以使用,適用於 mergeTree 引擎

2. 該命令是異步執行的,可以通過查看表 system.mutations 來查看命令的是否執行完畢

 

 

可以使用system.parts 表查詢一些意思的洞察數據:

:) select name, active, rows, bytes_on_disk, modification_time from system.parts where table='test_update' order by modification_time;

┌─name──────────────┬─active─┬────rows─┬─bytes_on_disk─┬───modification_time─┐
│ all_1_36_2        │      038411266376112452018-10-12 12:16:24 │
│ all_37_75_2       │      043581445985483582018-10-12 12:16:47 │
│ all_112_117_1     │      06389761678992332018-10-12 12:17:00 │
│ all_151_155_1     │      0778240273880522018-10-12 12:17:29 │
│ all_76_111_2      │      038338569897625022018-10-12 12:17:30 │
│ all_156_161_1     │      0837460274908912018-10-12 12:17:43 │
│ all_118_150_2     │      036372488596731472018-10-12 12:17:52 │
│ all_1_36_2_162    │      138411266376112322018-10-12 12:39:32 │
│ all_37_75_2_162   │      143581445985483522018-10-12 12:39:32 │
│ all_76_111_2_162  │      138338569897625022018-10-12 12:39:32 │
│ all_112_117_1_162 │      16389761678992332018-10-12 12:39:32 │
│ all_118_150_2_162 │      136372488596731472018-10-12 12:39:32 │
│ all_151_155_1_162 │      1778240273880522018-10-12 12:39:32 │
│ all_156_161_1_162 │      1837460274908912018-10-12 12:39:32 │
└───────────────────┴────────┴─────────┴───────────────┴─────────────────────┘

 

數據展示每個分區被更新的操作的時間,而且它的更新速度非常快

 

 

如果有數組列在我們表中如何處理。如何給所有用戶增加 這個數組的value 的值

:) select count(*) from test_update where has(dmp_audience_ids, 31694239);

┌─count()─┐
│  228706 │
└─────────┘
使用arrayPushBack 函數給dmp_audience_ids 列加入值1234567:
:) alter table test_update update dmp_audience_ids = arrayPushBack(dmp_audience_ids, 1234567) where has(dmp_audience_ids, 31694239);

 

立即查詢反饋結果
:) select count(*) from test_update where has(dmp_audience_ids, 1234567)

┌─count()─┐
│  228706 │
└─────────┘

:) select dmp_audience_ids from test_update where has(dmp_audience_ids, 1234567) and length(dmp_audience_ids)<5 limit 1;

┌─dmp_audience_ids─────────────────────┐
│ [31694239,31694422,31694635,1234567] │
└──────────────────────────────────────┘

注意事項:

Clickhouse更新操作有一些限制:

  1. 索引列不能進行更新
  2. 分布式表不能進行更新
  3. ALTER TABLE UPDATE/DELETE不支持分布式DDL,因此需要在分布式環境中手動在每個節點上local的進行更新/刪除數據。
  4. 不適合頻繁更新或point更新由於Clickhouse更新操作非常耗資源,如果頻繁的進行更新操作,可能會弄崩集群,請謹慎操作。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM