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()─┐ │ 0 │ 17824710 │ │ 22 │ 1701 │ └──────────────────┴──────────┘
假設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()─┐ │ 0 │ 17826411 │ └──────────────────┴──────────┘
返回結果正確,這個更新操作會被記錄到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 │ 0 │ 3841126 │ 637611245 │ 2018-10-12 12:16:24 │ │ all_37_75_2 │ 0 │ 4358144 │ 598548358 │ 2018-10-12 12:16:47 │ │ all_112_117_1 │ 0 │ 638976 │ 167899233 │ 2018-10-12 12:17:00 │ │ all_151_155_1 │ 0 │ 778240 │ 27388052 │ 2018-10-12 12:17:29 │ │ all_76_111_2 │ 0 │ 3833856 │ 989762502 │ 2018-10-12 12:17:30 │ │ all_156_161_1 │ 0 │ 837460 │ 27490891 │ 2018-10-12 12:17:43 │ │ all_118_150_2 │ 0 │ 3637248 │ 859673147 │ 2018-10-12 12:17:52 │ │ all_1_36_2_162 │ 1 │ 3841126 │ 637611232 │ 2018-10-12 12:39:32 │ │ all_37_75_2_162 │ 1 │ 4358144 │ 598548352 │ 2018-10-12 12:39:32 │ │ all_76_111_2_162 │ 1 │ 3833856 │ 989762502 │ 2018-10-12 12:39:32 │ │ all_112_117_1_162 │ 1 │ 638976 │ 167899233 │ 2018-10-12 12:39:32 │ │ all_118_150_2_162 │ 1 │ 3637248 │ 859673147 │ 2018-10-12 12:39:32 │ │ all_151_155_1_162 │ 1 │ 778240 │ 27388052 │ 2018-10-12 12:39:32 │ │ all_156_161_1_162 │ 1 │ 837460 │ 27490891 │ 2018-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更新操作有一些限制:
- 索引列不能進行更新
- 分布式表不能進行更新
- ALTER TABLE UPDATE/DELETE不支持分布式DDL,因此需要在分布式環境中手動在每個節點上local的進行更新/刪除數據。
- 不適合頻繁更新或point更新由於Clickhouse更新操作非常耗資源,如果頻繁的進行更新操作,可能會弄崩集群,請謹慎操作。
