构建clickhouse复杂数据模型

智能路径

  • 输入,在数据范围内指定结束事件与窗口大小
  • 返回,按用户访问时间由小到大排序后的路径字符串
select
  d_i,
  arrayStringConcat(
    arrayMap(
      b - > tupleElement(b, 1),
      arraySort(
        y - > tupleElement(y, 2),
        arrayFilter(
          (x, y, z) - > toDateTimeOrZero(z) - toDateTimeOrZero(y) < 1000,
          arrayMap(
            (x, y) - > (x, y),
            groupArray(e_t),
            groupArray(time)
          ),
          groupArray(time),
          arrayWithConstant(
            length(groupArray(time)),
            maxIf(time, e_t = 'launch')
          )
        )
      )
    ),
    '->'
  ) path
from
  bw.scene_tracker
where
  d_i <> ''
group by
  d_i


例子

上述例子窗口大小为1000s,结束事件 “launch”; 亿级数据妙出

  • 简版

select path,count(1)cn from(
select uid, maxIf( ts, url = 'https://ark.analysys.cn/browseGoods' ) as maxTime,
arrayFilter(x->maxTime-x.1<60*30*1000 and maxTime>=x.1 , groupArray( (ts,url) )) as window_data,
arraySort(x->x.1,window_data) as sort_data,
arrayStringConcat(sort_data.2,'->') as path
from bw.session group by uid ) where path<>'' group by path order by cn desc limit 11

  • 对路径中相邻页面重复的数据进行去重
select path,count(1)cn from(
select uid, maxIf( ts, url = 'https://ark.analysys.cn/browseGoods' ) as maxTime,
arrayFilter(x->maxTime-x.1<60*30*1000 and maxTime>=x.1 , groupArray( (ts,url) )) as window_data,
arraySort(x->x.1,window_data) as sort_data,
arrayFilter((x,y)->x<>sort_data[y-1].2 ,sort_data.2,arrayEnumerate( sort_data )) as sort_data_url, --相邻去重
arrayStringConcat(sort_data_url,'->') as path
from bw.session group by uid ) where path<>'' group by path order by cn desc limit 11

  • 线上环境测试版
select   data, count(1) cn from (
 with  maxIf( c_t , cat='page_view'and act='页面_浏览') as max_time, -- 目标事件时间
         arraySort(
           e -> e.1,
           arrayFilter(x->x.1<=toUInt64OrZero(max_time),groupArray((toUInt64OrZero(c_t), (cat,act) )))
         ) as sorted_array,
          -- 按时间排序后的数据
         arrayPushFront( sorted_array, sorted_array[1] ) as e_arr,
         arrayFilter(
           (i, e,z) -> z.1  < toUInt64OrZero(max_time)  
                   and   (e > 1800000 or (z.2.1='page_view' and z.2.2='页面_浏览')),  
            arrayEnumerate(e_arr), arrayDifference( e_arr.1 ),e_arr
         ) as arr_indx, -- 过滤目标事件、时间差后的数据
         arrayReduce('max',arr_indx) as smIndx,
         arrayFilter(
           (e,i) ->  i>=smIndx and e.1<=toUInt64OrZero(max_time)   ,  
           sorted_array, arrayEnumerate(sorted_array) 
         ) as data_ 
 
 select u_i,
         arrayFilter((x,y)-> y<>0 ,data_.2,arrayDifference(arrayEnumerateDense(data_.2))) as data__,  
         arraySlice(data__,length(data__)-8,8 ) as data,
        --  arrayStringConcat(data,'->') as path,
         hasAll(data, [ ('page_view','页面_浏览') ]) as has_way_point 
    from app.scene_tracker where c_p='PC'   and length(u_i)>20  
    group by u_i  having length(data)>1 
  ) tab
where has_way_point=1 group by data order by cn desc limit 1000


易观 OLAP Session分析

http://ds.analysys.cn/2019/session.html

1、计算默认session每天的会话次数、人均访问时长、退出率

SELECT
    day,
    countDistinct(sid) AS scn,
    countDistinct(uid) AS ucn,
    sum(t2 - t1) / ucn AS dur,
    countIf(t1 = t2) / scn AS t_rate
FROM
(
    SELECT
        day,
        uid,
        sid,
        min(ts2) AS t1,
        max(ts2) AS t2
    FROM bw.session2
    GROUP BY
        day,
        uid,
        sid
)
GROUP BY day
ORDER BY day ASC

2、根据动态session计算每日会话次数

  • 第一版
#  4s
select
  day,
  sum(length(sessions))
from
  (
    select
      day,
      arrayFilter(
        (y, z) - > dateDiff(
          'minute',
          toDateTimeOrZero(y),
          toDateTimeOrZero(z)
        ) > 30,
        arraySort(x - > x, groupArray(time)),
        arrayPushBack(
          arrayPopFront(arraySort(x - > x, groupArray(time))),
          '2029-09-08 23:21:30'
        )
      ) sessions
    from
      bw.scene_tracker
    where
      d_i <> ''
    group by
      day,
      d_i
  )
where
  length(sessions) > 0
group by
  day


  • 第二版
# 4s
select day,sumArray(arrayFilter((z,x,y)->dateDiff( 'minute', toDateTimeOrZero(x) ,toDateTimeOrZero(y)) >30,tupleElement(sessions,3),tupleElement(sessions,1), tupleElement(sessions,2)) ) from (
select day, arrayMap((x,y) -> ( x,y,1),
arraySort(groupArray(time)),
arrayPushBack(arrayPopFront(arraySort(x->x, groupArray(time))),'2029-09-08 23:21:30')) sessions
from bw.scene_tracker where d_i<>'' group by day, d_i )    group by day

  • 第三版
# 4s

select day,sum(length(arrayFilter((x,y)->dateDiff( 'minute', toDateTimeOrZero(x) ,toDateTimeOrZero(y)) >30, t1, t2)) ) from (
select day,  
arraySort(x->x, groupArray(time )) t1 ,
arrayPushBack(arrayPopFront( t1 ),'2029-09-08 23:21:30') t2
from bw.scene_tracker where d_i<>'' group by day, d_i )    group by day limit 1111
 
  • 终结版
# 使用超时时间30分钟+跨天的session切割规则,计算出20190501-20190510,每天的会话次数 
select day ,sumArray( sessions ) from (
select day,  arrayMap(( y,z) -> if(dateDiff( 'minute', y ,z)>30,1,0)  , 
arraySort(groupArray( ts2)) as t1,
arrayPushBack(arrayPopFront(t1) ,addYears(now(),1000))) sessions 
from bw.session2    group by day, uid ) where length(sessions)>0 group by day  

  • 简化版
select day ,sum(length( sessions )) from (
select day, arrayFilter(x ->  x>30*60*1000,
arrayDifference(arraySort( groupArray( ts ))))   sessions
from bw.session2    group by day, uid )   group by day  

  • 整合版
select day , sum(arrayUniq(psarray)) pscn, sum(arrayUniq(psarray)-length(intersect)) /sum(length(idxx)) as t_rate from (   
select day,       
arraySort(x->x.2, groupArray( (url,ts,event_code )))as cur,   
arrayFilter( (x,y,z)->  y>30*60*1000 or z='$start_event'   , range( length(cur)) , arrayDifference(cur.2) ,cur.3)   as idx ,
arrayPushBack(idx ,length(cur)) as idx2, 
arrayEnumerate(idx2) as idxx, 
flatten(arrayMap((x,y)->arrayResize([concat(toString(x),toString(uid)) ], x-idx2[y-1], concat(toString(x),toString(uid)) ), idx2,  idxx ) ) as tkarray ,
arrayFilter((x,y)-> y='https://ark.analysys.cn/browseGoods' , tkarray, cur.1) as psarray  , 
arrayFilter((x,y)-> y<>'https://ark.analysys.cn/browseGoods' , tkarray, cur.1) as psother,
arrayIntersect(psarray,psother) intersect 
from bw.session2    group by day, uid )  group by day 

  • 另一种实现 性能一般般
# 结果与上面一致
select day , sumArray(tp.3) pscn, sumArray(tp.2) /sum(length(tp)) as trate from (   
select day,       
arraySort(x->x.2, groupArray( (url,ts )))as cur,   
arrayFilter( (x,y )->  y>30*60*1000 , range( length(cur)) , arrayDifference(cur.2) )   as idx ,
arrayPushBack(idx ,length(cur)) as idx2, 
arrayEnumerate(idx2) as idxx, 
arrayMap((x,y)-> (arraySlice(cur.1, idx2[y-1]+1, x-idx2[y-1]) as session, if(hasAny(session,['https://ark.analysys.cn/browseGoods']) and  arrayUniq(session)=1,1,0) as depth, hasAny(session,['https://ark.analysys.cn/browseGoods']) as pscn),  idx2, idxx) tp 
from bw.session2    group by day, uid )  group by day 


2、根据动态session计算每日着陆页的跳出率

跳出率=访问了一个页面的Session数/总的Session数

  • 第一版
select day ,sumArray( sessions.1 )/sumArray( sessions.2 ) from (  
select day,  arrayMap(( x,y,z) -> (if(dateDiff( 'minute', y.2 ,z.2)>30 and endsWith(y.1,'index'),1,0) ,if(dateDiff(   'minute', y.2 ,z.2)>30,1,0))  ,  
arrayMap((x,y)->(x,y),groupArray( url ), groupArray( ts2))as data,  
arraySort(x->x.2, data) as t1,  
arrayPushBack(arrayPopFront(t1) ,('',addYears(now(),1000)))  
) sessions   
from bw.session2    group by day, uid )  group by day 

3、 使用超时时间30分钟+跨天+指定开始事件,的session切割规则计算出20190501-20190510,每天包含某个页面行为的会话总数,人均访问深度。

  • 第一版
--每天包含某个页面行为的会话总数

select day , sum(arrayUniq(cn)) from (  
select day,     
arraySort(x->x.2,arrayMap((x,y,z)->(x,y,z),groupArray( url ), groupArray( ts2 ), groupArray( event_code )))as cur,  
arrayPushBack(arrayPopFront(cur) ,('', addYears(now(),1000) ,'')) as next,
arrayEnumerate(cur) as inx,
arrayFilter( (x,y,z)-> dateDiff( 'minute', y.2 ,z.2)>30 or z.3='$start_event' , inx, cur, next)   as idx,
arrayEnumerate(idx) as idxx,
flatten(arrayMap((x,y)->arrayWithConstant(x-idx[y-1],x), idx,  idxx) ) as tkarray ,
arrayResize( tkarray, length( cur ), length( cur )) as narray,
arrayFilter((x,y)-> y.1='https://ark.analysys.cn/browseGoods' , narray, cur)  cn
from bw.session2    group by day, uid )  group by day 

--人均访问深度

select day , sum(arrayUniq(cn)) pcn,sum(length(idx)) scn from (  
select day,     
arraySort(x->x.2,arrayMap((x,y,z)->(x,y,z),groupArray( url ), groupArray( ts2 ), groupArray( event_code )))as cur,  
arrayPushBack(arrayPopFront(cur) ,('', addYears(now(),1000) ,'')) as next,
arrayEnumerate(cur) as inx,
arrayFilter( (x,y,z)-> dateDiff( 'minute', y.2 ,z.2)>30 or z.3='$start_event' , inx, cur, next)   as idx,
arrayEnumerate(idx) as idxx,
flatten(arrayMap((x,y)->arrayWithConstant(x-idx[y-1],x), idx,  idxx) ) as tkarray ,
arrayResize( tkarray, length( cur ), length( cur )) as narray,
arrayMap((x,y)-> concat(y.1,'$$',toString( x))  , narray, cur)  cn
from bw.session2    group by day, uid )  group by day 

  • 第二版(整合版)
-- 第三题 简版
select day , sum(arrayUniq(psarray)) pscn, sum(arrayUniq(deptharray)) /sum(length(idxx)) as avgdepth from (   
select day,       
arraySort(x->x.2, groupArray( (url,ts,event_code )))as cur,   
arrayFilter( (x,y,z)->  y>30*60*1000 or z='$start_event'   , range( length(cur)) , arrayDifference(cur.2) ,cur.3)   as idx ,
arrayPushBack(idx ,length(cur)) as idx2, 
arrayEnumerate(idx2) as idxx, 
flatten(arrayMap((x,y)->arrayWithConstant(x-arrayElement(idx2, y-1), concat(toString(x),toString(uid)) ), idx2,  idxx ) ) as tkarray ,
arrayFilter((x,y)-> y='https://ark.analysys.cn/browseGoods' , tkarray, cur.1) as psarray, 
arrayMap((x,y)-> concat(y,'$$',   x )  , tkarray, cur.1) as deptharray  
from bw.session2    group by day, uid )  group by day  


  • 第三版
# 用 arrayResize 函数替代 arrayWithConstant 性能提升2s
select day , sum(arrayUniq(psarray)) pscn, sum(arrayUniq(deptharray)) /sum(length(idxx)) as avgdepth from (   
select day,       
arraySort(x->x.2, groupArray( (url,ts,event_code )))as cur,   
arrayFilter( (x,y,z)->  y>30*60*1000 or z='$start_event'   , range( length(cur)) , arrayDifference(cur.2) ,cur.3)   as idx ,
arrayPushBack(idx ,length(cur)) as idx2, 
arrayEnumerate(idx2) as idxx, 
flatten(arrayMap((x,y)->arrayResize([concat(toString(x),toString(uid)) ], x-idx2[y-1], concat(toString(x),toString(uid)) ), idx2,  idxx ) ) as tkarray ,
arrayFilter((x,y)-> y='https://ark.analysys.cn/browseGoods' , tkarray, cur.1) as psarray, 
arrayMap((x,y)-> concat(y,   x )  , tkarray, cur.1) as deptharray  
from bw.session2    group by day, uid )  group by day 

  • 另一种解题思路
select day , sum(pacn) pscn, sum(cl) /sum(asm) as avgdepth from (   
select day,       
arraySort(x->x.2, groupArray( (url,ts )))as cur,   
-- arrayMap( (y,z)->  if(y>30*60*1000 or z='https://ark.analysys.cn/startUp' ,1,0)  , arrayDifference(cur.2) ,cur.1)   as idx ,
arrayMap((y,z)->  if(y>30*60*1000 or z='https://ark.analysys.cn/startUp' ,1,0), arrayDifference(cur.2) ,cur.1) amap,
arrayCumSum(amap) as rsid,
arraySum(amap )+1 asm,
arrayFilter((x,y)-> y='https://ark.analysys.cn/browseGoods' , rsid, cur.1) as psarray, 
arrayUniq(  psarray )  as pacn,length(cur) as cl
from bw.session3    group by day, uid )  group by day  


建表语句

CREATE TABLE bw.session2 (`uid` Int64, `ts` UInt64, `event_code` String, `sid` String, `url` String, `platform` String, `source` String, `city` String, `brand` String, `buy_count` Int32, `price` Float64, `day` LowCardinality(String), `ts2` DateTime, `uid2` LowCardinality(String)) ENGINE = MergeTree PARTITION BY tuple() ORDER BY (day, uid2, ts2) SETTINGS index_granularity = 8192

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 202,607评论 5 476
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,047评论 2 379
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 149,496评论 0 335
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,405评论 1 273
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,400评论 5 364
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,479评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,883评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,535评论 0 256
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,743评论 1 295
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,544评论 2 319
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,612评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,309评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,881评论 3 306
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,891评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,136评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,783评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,316评论 2 342

推荐阅读更多精彩内容