您好,登錄后才能下訂單哦!
這篇文章給大家分享的是有關如何使用MySQL位圖索引解決用戶畫像問題的內容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。
每個bigint類型包括60個記錄的位信息.
但是第0位表示第六十個記錄的位
第1位至第59位表示第一至五十九的記錄的位信息.
這樣記錄的位信息保存并不連續,
使用的時候還得把最右邊的一位挪到最左邊,不好理解還非常麻煩,性能也有損耗.
經過王工的改良,
使用如下sql替換,則保存的位信息就連續了
SELECT CEIL(id / 60) g60, CEIL(id / 1200) g1200, age grouped, COUNT(*) total, BIT_OR(1 << (if(MOD(id, 60)=0,60,MOD(id, 60)))) bitmap FROM o_huaxiang_big_0 o GROUP BY g1200 , g60 , age
創建位圖索引的整體SQL如下
truncate table bitmap20_0; insert into bitmap20_0 select 'o_huaxiang_big' table_name, 'umc_sex' column_name, ((g1200-1)*60)*20 min_id, ((g1200-1)*60)*20+1200 max_id, v2.* from ( select g1200, grouped, sum(total) total, ifnull(max(case when abs((g1200-1)*20-g60)=20 then bitmap else null end),0) c20, ifnull(max(case when abs((g1200-1)*20-g60)=19 then bitmap else null end),0) c19, ifnull(max(case when abs((g1200-1)*20-g60)=18 then bitmap else null end),0) c18, ifnull(max(case when abs((g1200-1)*20-g60)=17 then bitmap else null end),0) c17, ifnull(max(case when abs((g1200-1)*20-g60)=16 then bitmap else null end),0) c16, ifnull(max(case when abs((g1200-1)*20-g60)=15 then bitmap else null end),0) c15, ifnull(max(case when abs((g1200-1)*20-g60)=14 then bitmap else null end),0) c14, ifnull(max(case when abs((g1200-1)*20-g60)=13 then bitmap else null end),0) c13, ifnull(max(case when abs((g1200-1)*20-g60)=12 then bitmap else null end),0) c12, ifnull(max(case when abs((g1200-1)*20-g60)=11 then bitmap else null end),0) c11, ifnull(max(case when abs((g1200-1)*20-g60)=10 then bitmap else null end),0) c10, ifnull(max(case when abs((g1200-1)*20-g60)=9 then bitmap else null end),0) c9, ifnull(max(case when abs((g1200-1)*20-g60)=8 then bitmap else null end),0) c8, ifnull(max(case when abs((g1200-1)*20-g60)=7 then bitmap else null end),0) c7, ifnull(max(case when abs((g1200-1)*20-g60)=6 then bitmap else null end),0) c6, ifnull(max(case when abs((g1200-1)*20-g60)=5 then bitmap else null end),0) c5, ifnull(max(case when abs((g1200-1)*20-g60)=4 then bitmap else null end),0) c4, ifnull(max(case when abs((g1200-1)*20-g60)=3 then bitmap else null end),0) c3, ifnull(max(case when abs((g1200-1)*20-g60)=2 then bitmap else null end),0) c2, ifnull(max(case when abs((g1200-1)*20-g60)=1 then bitmap else null end),0) c1 from ( SELECT CEIL(id / 60) g60, CEIL(id / 1200) g1200, umc_sex grouped, COUNT(*) total, BIT_OR(1 << (if(MOD(id, 60)=0,60,MOD(id, 60)))) bitmap FROM o_huaxiang_big_0 o GROUP BY g1200 , g60 , umc_sex ) v1 group by g1200,grouped ) v2; insert into bitmap20_0 select 'o_huaxiang_big' table_name, 'age' column_name, ((g1200-1)*60)*20 min_id, ((g1200-1)*60)*20+1200 max_id, v2.* from ( select g1200, grouped, sum(total) total, ifnull(max(case when abs((g1200-1)*20-g60)=20 then bitmap else null end),0) c20, ifnull(max(case when abs((g1200-1)*20-g60)=19 then bitmap else null end),0) c19, ifnull(max(case when abs((g1200-1)*20-g60)=18 then bitmap else null end),0) c18, ifnull(max(case when abs((g1200-1)*20-g60)=17 then bitmap else null end),0) c17, ifnull(max(case when abs((g1200-1)*20-g60)=16 then bitmap else null end),0) c16, ifnull(max(case when abs((g1200-1)*20-g60)=15 then bitmap else null end),0) c15, ifnull(max(case when abs((g1200-1)*20-g60)=14 then bitmap else null end),0) c14, ifnull(max(case when abs((g1200-1)*20-g60)=13 then bitmap else null end),0) c13, ifnull(max(case when abs((g1200-1)*20-g60)=12 then bitmap else null end),0) c12, ifnull(max(case when abs((g1200-1)*20-g60)=11 then bitmap else null end),0) c11, ifnull(max(case when abs((g1200-1)*20-g60)=10 then bitmap else null end),0) c10, ifnull(max(case when abs((g1200-1)*20-g60)=9 then bitmap else null end),0) c9, ifnull(max(case when abs((g1200-1)*20-g60)=8 then bitmap else null end),0) c8, ifnull(max(case when abs((g1200-1)*20-g60)=7 then bitmap else null end),0) c7, ifnull(max(case when abs((g1200-1)*20-g60)=6 then bitmap else null end),0) c6, ifnull(max(case when abs((g1200-1)*20-g60)=5 then bitmap else null end),0) c5, ifnull(max(case when abs((g1200-1)*20-g60)=4 then bitmap else null end),0) c4, ifnull(max(case when abs((g1200-1)*20-g60)=3 then bitmap else null end),0) c3, ifnull(max(case when abs((g1200-1)*20-g60)=2 then bitmap else null end),0) c2, ifnull(max(case when abs((g1200-1)*20-g60)=1 then bitmap else null end),0) c1 from ( SELECT CEIL(id / 60) g60, CEIL(id / 1200) g1200, age grouped, COUNT(*) total, BIT_OR(1 << (if(MOD(id, 60)=0,60,MOD(id, 60)))) bitmap FROM o_huaxiang_big_0 o GROUP BY g1200 , g60 , age ) v1 group by g1200,grouped ) v2;
感謝各位的閱讀!關于“如何使用MySQL位圖索引解決用戶畫像問題”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。