查看: 409|回复: 0

使用位运算 数据分析 - 用户历史

 关闭 [复制链接]

签到天数: 2596 天

连续签到: 10 天

[LV.Master]至尊海神IIIIII

发表于 2010-10-23 19:03 | 显示全部楼层 |阅读模式
使用位运算 数据分析 - 用户历史





为解决用户的同属性 大量操作存储问题 :


数据库表结果:
  用户 |  属性域+  |  当前时间 | 最小时间力度 | int  | int .....


比如 :
  用户1 | 频道1 | ....   | 2010-10-01 | 1天   | 0
  .....


表结构为 ( 预订表存储 一个月 2010-10 ) :
create table test (
   uid int primary key ,
   domain varchar(20),
   starttime datetime,
   endtime  datetime,
   touch int ,
   unique(uid,domain,starttime ) );




用户分别在 10-05 ,10-13  ,10-20 天来过
#10-05 减去 10-01  为 5 天
insert into test values
  (1,'yy-xx','2010-10-01','2010-10-05',1<<DATEDIFF(endtime,starttime )  )
on duplicate key
   update endtime = '2010-10-05'  ,  
          touch = ( 1<<DATEDIFF(endtime,starttime)|touch) ;


# 10-13 减去 10-01  为 13天
insert into test values
  (1,'yy-xx','2010-10-01','2010-10-13',1<<DATEDIFF(endtime,starttime )  )
on duplicate key
   update endtime = '2010-10-13'  ,  
          touch = ( 1<<DATEDIFF(endtime,starttime)|touch) ;


# 10-20 减去 10-01  为 20 天
insert into test values
  (1,'yy-xx','2010-10-01','2010-10-20',1<<DATEDIFF(endtime,starttime )  )
on duplicate key
   update endtime = '2010-10-20'  ,  
          touch = ( 1<<DATEDIFF(endtime,starttime)|touch) ;


当前月 来的 总天数  :
>  select BIT_COUNT(touch) from test ;




上周来天数 (2010-10-03 到 2010-10-09) :
>  select
  bit_count(
   ( touch >> datediff('2010-10-03',starttime) ) &
   (1<<datediff('2010-10-09','2010-10-03'))-1
  )
from test ;




还有 许多这样的想法 , 后面再说吧 下班 ~




<
PCOS系统下载站:http://zhuangji.wang

本版积分规则