Clickhouse实战笔记

一 、DDL

1.1 建表

关键点:分区、默认排序、使用引擎


CREATE TABLE user_model_gpu_info (
`regdate` Date,
 `user_id` String,
 `puid` String,
 `reg_version` String,
 `system` String,
 `model` String,
 `memory` String COMMENT '内存',
 `cpu` String,
 `cpucore` String,
 `cpufeq` String,
 `screen_w` String,
 `screen_h` String,
 `screen_resolution` String,
 `screen_density` String,
 `shaderlvl` String,
 `graphicdevice_id` String,
 `graphicdevice` String,
 `graphicmemory` String,
 `gpu_frame_count` String COMMENT 'gpu跑分',
 `model_level` Int32,
 `opengl` String,
 `graphics_multi_threaded` String,
 `supports_instancing` String,
 `max_texture_size` String,
 `npot_support` String,
 `perform_test` String
) ENGINE = MergeTree()  PARTITION BY regdate ORDER BY (user_id, puid) SETTINGS index_granularity = 8192

1.2 修改表字段

1.2.1新增

ALTER  TABLE use_skill ADD Column  integration_name String 

1.2.2 删除 

ALTER  TABLE lost_dwd_unit_use_skill DROP Column  integration_name  

1.3 TRUNCATE 清空表数据

 TRUNCATE TABLE tablename

二、DML

2.1 数据删除

ALTER TABLE  table_name  DELETE where date = '2020-01-08'

2.2 REPLACE() 对字段内容进行替换

select  REPLACE('7654,7698,7782,7788', ',' ,'_')  from table limit 5
 

2.3 数据类型转换

CAST(colmun AS String)   或者 toString()

2.4 Explode字段数据分割

[mysql]SUBSTRING_INDEX()
 SELECT SUBSTRING_INDEX('apple,pear,melon', ',', 1) from DUAL
 [clickhouse]splitByString( '.',action)[1]    -- 从1开始取值提取分割后的元素

三 业务逻辑SQL

3.1 行转列

(1)
	SELECT date, hour,
		if(`Column`='hour_pay_gross',`Value`,0) AS `hour_pay_gross`, 
		if(`Column`='hour_pay_user',`Value`,0) AS `hour_pay_user``
	FROM  paid_daily_hour_stats

		
(2)CASE-WHEN 
	SELECT 
		CASE  WHEN f5='1' THEN '金币' 
			  WHEN f5='2' THEN '钻石'	   
		END AS currency
	FROM log 
	LIMIT 10

Clickhouse实战笔记_第1张图片

 

 

你可能感兴趣的:(ClickHouse)