直接使用Clickhouse官方支持的Mysql引擎表的方式!
一、首先創建Mysql引擎表:
CREATE?TABLE?saas_analysis.t_page_view_new_for_write
(`id`?Int64,`shop_id`?Nullable(Int64),`session_id`?Nullable(String),`client_id`?Nullable(String),`one_id`?Nullable(String),`browser_ip`?Nullable(String),`ip_country`?Nullable(String),`ua`?Nullable(String),`referer`?Nullable(String),`current_url`?Nullable(String),`page_type`?Nullable(String),`extra_params`?Nullable(String),`cloak_strategy`?Nullable(String),`cloak_risk_type`?Nullable(Int32),`flow_channel`?Nullable(String),`first_paint_time`?Nullable(Int32),`ready_time`?Nullable(Int32),`create_time`?DateTime
)ENGINE?=?MySQL('127.0.0.1:3306',?'saas_analysis',?'t_page_view_new',?'mysql_rw',?'password', 0, '1')SETTINGSconnect_timeout?=?30,???????--?連接超時時間(秒)read_write_timeout?=?1800,??--?讀寫超時時間(秒)connection_pool_size?=?16,??--?連接池大小connection_max_tries?=?3;
ENGINE配置密碼后兩位代表:
replace_query
?— 將?INSERT INTO
?查詢轉換為?REPLACE INTO
?的標志。如果?replace_query=1
,則查詢會被替換。(參數值int類型)on_duplicate_clause
?— 添加到?INSERT
?查詢中的?ON DUPLICATE KEY on_duplicate_clause
?表達式。(參數值String類型)'1' 代表開啟,不能和replace_query
?同時開啟。
二、執行Clickhouse查詢寫入SQL(insert into select):
-- 會話級別設置(僅當前會話有效)
SET max_execution_time = 1200; -- 設置為1200秒(20分鐘)INSERT INTO saas_analysis.t_page_view_new_for_write
(id,shop_id,session_id,client_id,one_id,browser_ip,ip_country,ua,referer,current_url,page_type,extra_params,flow_channel,first_paint_time,ready_time,create_time
)
SELECTid,shop_id,session_id,client_id,one_id,browser_ip,ip_country,ua,referer,current_url,page_type,extra_params,flow_channel,first_paint_time,ready_time,create_time
FROM saas_analysis.t_page_view
WHERE create_time >='2025-06-02 00:00:00' and create_time < '2025-06-03 00:00:00'
注意:這里由于數據量問題,需要加大CK的最大查詢超時時間。
否則會報錯 Estimated?query?execution?time?(633.9467681108954?seconds)?is?too?long.?Maximum:?600.
三、在執行過程中遇到的其他報錯:
MySQL server has gone away
①加大客戶端的socket_timeout時間為3000000
②加大Mysql允許最大接收數據包
SET GLOBAL max_allowed_packet=10737418240; // 10G
四、執行結果:
數據量大的需要寫腳本分批去執行,我這邊一天作為一批,一天數據量在2000萬,耗時10分鐘左右。