Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Statistics of active users by bitmap method

2025-04-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

Practical applications of Setbit

Scenario: 100 million users, each user logging in/doing any operation, recorded as active today, otherwise recorded as inactive

Weekly ratings: Prize-winning Active users: 7 consecutive days of activity

Monthly reviews, etc...

Idea:

Userid dt active

1 2013-07-27 1

1 2013-0726 1

If it is placed in a table, 1: the table increases sharply, 2: the group sum operation is used, and the calculation is slow.

Bitmap: Bitmap

Log0721: '011001............... 0'

......

log0726 : '011001............... 0'

Log0727 : '0110000............. 1'

1: Record user login:

A bitmap is generated by date every day. After the user logs in, the bit value on the user_id bit is set to 1.

2: Take the bitmap of 1 week and calculate,

A 1 in the upper bit is a continuous login user.

redis 127.0.0.1: 6379> setbit mon 1000000 0 (100 million users on Monday initial state, all 0)

(integer) 0

redis 127.0.0.1: 6379> setbit mon 3 1

(integer) 0

redis 127.0.0.1: 6379> setbit mon 5 1

(integer) 0

redis 127.0.0.1: 6379> setbit mon 7 1 (User 7 logs in once on Monday, counted as 1)

(integer) 0

redis 127.0.0.1:6379> setbit thur 100000000 0

(integer) 0

redis 127.0.0.1:6379> setbit thur 3 1

(integer) 0

redis 127.0.0.1:6379> setbit thur 5 1

(integer) 0

redis 127.0.0.1:6379> setbit thur 8 1

(integer) 0

redis 127.0.0.1:6379> setbit wen 100000000 0

(integer) 0

redis 127.0.0.1:6379> setbit wen 3 1

(integer) 0

redis 127.0.0.1:6379> setbit wen 4 1

(integer) 0

redis 127.0.0.1:6379> setbit wen 6 1

(integer) 0

redis 127.0.0.1:6379> bitop and res mon feb wen

(integer) 12500001

As in the previous example, advantages:

1: Save space, 100 million people log in every day, with 100 million bits, about 1200WByte, about 10M characters can be expressed

2: Easy to calculate

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report