首页 > 数据库 > 数据库开发技术 > clickhouse日常操作问题分享1031

1. 分布式表创建
1.1 创建本地表
需在每个节点创建表
CREATE TABLE testdb.t_tb1(`uuid` String,`name` String,`timestamp` DateTime64(3),`insert_time` DateTime64(3),`note` String)ENGINE = ReplicatedMergeTree('/clickhouse/tables/{shard}/testdb/t_tb1', '{replica}')PARTITION BY toYYYYMMDD(timestamp)ORDER BY timestampSETTINGS index_granularity = 8192
1.2 创建分布式表
CREATE TABLE testdb.t_tb1_all(`uuid` String,`name` String,`timestamp` DateTime64(3),`insert_time` DateTime64(3),`note` String)ENGINE = Distributed('ck', 'testdb', 't_tb1', rand());
1.3 常见问题
Map类型问题
如使用到map类型,则创建本地表时会出现如下错误
Code: 44. DB::Exception: Received from localhost:9000. DB::Exception: Cannot create table with column 'xxx' which type is 'Map(String,Int64)' because experimental Map type is not allowed. Set 'allow_experimental_map_type = 1' setting to enable.此时按照提示,创建表前进行allow_experimental_map_type =1设置即可
set allow_experimental_map_type =1;分区数上限问题
2022-10-29T23:24:37.968170Z WARN vector::sinks::util::retries: Retrying after response. reason=Code: 252, e.displayText() = DB::Exception: Too many parts (120163) in all partitions in total. This indicates wrong choice of partition key. The threshold can be modified with 'max_parts_in_total' setting in <merge_tree> element in config.xml or with per-table setting. (version 21.3.12.2 (official build))默认每张表的分区数大小是10W,超过后将只能等待分区数合并后才能继续写入。解决方式有两种:
修改表分区数上限:如建表或修改表语句中加max_parts_in_total = 110000
修改分区方式:修改分区键
推荐使用第二种,因为分区数上限调大后对整体的稳定性有影响
其他表级别的报错信息,都可以修改表属性方式实现。但是修改前建议先考虑使用方面是否可以优化。
2. 重建分布式表
在clickhouse的使用过程中,难免会要删除掉已经创建好的表或进行重建。正常步骤为先删除本地表,再删除分布式表,删除命令如下
--删除本地表drop table testdb.t_tb1on cluster ck;--删除分布式drop table testdb.t_tb1_all on cluster ck;
Code: 253. DB::Exception: Received from localhost:9000. DB::Exception: Replica /clickhouse/tables/03/testdb/t_tb1/replicas/192.168.56.129 already exists.原因是clickhouse默认的库引擎是原子数据库引擎,删除Atomic数据库中的表后,它不会立即删除,而是会在480秒后删除。
处理方式,有3种:
修改database_atomic_delay_before_drop_table_sec参数,设置为需要的大小(默认480s)
等待480后在进行重建表的操作
修改建表语句中名称,如t_tb1 改为t_tb1_new等
推荐不修改,等待一段时间后再重建。

4. 监控利器出鞘:Prometheus+Grafana监控MySQL、Redis数据库
5. PostgreSQL主从复制--物理复制
6. MySQL传统点位复制在线转为GTID模式复制




原文作者:数据库干货铺;如有侵权,请联系管理员删除。