zoukankan      html  css  js  c++  java
  • 在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更新操作非常耗资源,如果频繁的进行更新操作,可能会弄崩集群,请谨慎操作。
  • 相关阅读:
    STM32 GPIO 配置之ODR, BSRR, BRR 详解
    Understanding the STM32F0's GPIO
    STM32F4 External interrupts
    Calculate CAN bit timing parameters -- STM32
    禁用SQL Server Management Studio的IntelliSense
    SQL Server 2016中In-Memory OLTP继CTP3之后的新改进
    一张图解释SQL Server集群、镜像、复制、日志传送
    SQL Server出现错误: 4014
    SQL Server 2016五大优势挖掘企业用户数据价值
    SQL Server 2008, 2008 R2, 2012 and 2014 完全支持TLS1.2加密传输
  • 原文地址:https://www.cnblogs.com/wenBlog/p/15745238.html
Copyright © 2011-2022 走看看