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] │
└──────────────────────────────────────┘
注意事项: