amwdnuff 发表于 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,
   endtimedatetime,
   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 ;




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




页: [1]
查看完整版本: 使用位运算 数据分析 - 用户历史