当前位置:网站首页>Take my brother to make a real-time Leaderboard
Take my brother to make a real-time Leaderboard
2022-06-24 07:42:00 【Programmer fish skin】
Design and implementation of real-time leaderboard system .
Hello everyone , I'm fish skin , Summer vacation is coming , My brother, little ABA, has heard that there are a lot of healthy people in my family , Come and play with me .
As a result, I put out a few small systems that I had developed before , I'm going to do some works with little abado during this period , Learn how to design a programming project . So when he starts school , You can do the project with the teacher more easily .
today , Let's take him to do a very common little function first : Real time ranking of users .
Real time leaderboard
demand
First, describe the requirements , In my programming navigation project (https://www.code-nav.cn) in , In order to encourage everyone to maintain the website together , Users can recommend resources 、 Positive comments 、 Get points by reporting illegal resources .
To further motivate you , The website needs to provide a user ranking , It is divided into Real time total scoreboard 、 Zhou Bang and Monthly list , all Take only before 10 name . All users can view the current leaderboard , And check your own real time Total points ranking , Subsequent administrators can award prizes to users on the list .
The effect is as follows :
Click on My ranking Button , You can see your real-time ranking :
Space is limited , Let's just talk about Real time total scoreboard Design and implementation of .
After listening to the demand , Little Abba has a big smile : It's hard ? Let me design a wave , I'll tell you more .
Design implementation
Let's look at the structure of the database first , All in all 2 Tables : User table and User score table .
The user table stores user information , And the total points of users ( Real time updates ), In other words, the data needed by the total scoreboard can be directly obtained from here , There's no need to calculate .
User table content :
user id | user name | integral (score) |
|---|---|---|
1 | Little Abba | 10 |
2 | Plum skin | 1000 |
3 | petty thief | 100 |
...... | ||
100 | Li laore | 66 |
If you want to take the front 10 name , Just take out all the users' information first , Just put it in another order , Write SQL Statement query is :
select * from `user` order by score;
Then if you want to get your own total ranking , Just traverse the ordered data found , Just find the subscript where you are , The pseudocode is as follows :
// Query the list of all users from the database
list = getAllDataList()
for(i = 0; i < total; i++) {
// Find your place
if(list[i].id == ' my id') {
return i + 1;
}
}Little Abba is proud of : That's how we're going to get to the top of the table ? Your needs are too simple , Click on the tongue .
I laugh so much : Not bad , The idea of the championship is right , At least know to sort all the data . But if there are too many users ? Like hundreds of thousands of them , You just need to check your overall ranking , Do you still need to sort all the data ?
Little Abba was lost in thought , I thought about it for a long time. , I didn't think of it .
So I suggested : If you want to know your ranking in an exam , Do you just need to know how many people have higher scores than yourself , You don't have to worry about who's in the line, right ?
Little Abba patted his head : Yeah , I just need to find out my score first , Then count the number of users whose scores are greater than mine , I don't know my ranking ?
First use SQL Statement to find out the user's score :
/* Take only the columns you need */ select score as myScore from `user` where id = " user id";
And then use SQL The number of statements whose statistics score is greater than that of the user :
select count(*) from `user` where score > myScore;
Finally, we just need to add 1, It's my ranking ~
Little ABA sighed : It turns out that a little bit of thinking , It saves the performance overhead of extra sorting , take off ~
Think more
Fish skin : Don't take off yet , In fact, for a system with a large number of users , The above plan is enough . Now let's make it more difficult , If the number of users is a little more , For example, 100 million , How to real-time access before 10 What about the name ?
Little Abba : That's true “ Billion points ”, As far as your programming navigation is concerned, there are still 100 million users ?
Fish skin : cut the crap , Dreams are necessary , What if there are 100 million users ? Think about the system !
Little Abba : Not to mention how slow it is to sort 100 million data , It's a question whether we can save it or not ... ah , wait , That's what interviews are all about Top N problem !
Fish skin : Pretty good , I was asked several times during the interview Top N problem , How to find out the front N How about the number ?
Little Abba : I don't understand that at all , Algorithm does not , It's killing .
Fish skin : Actually Top N The core of the problem is to ensure the complexity of space and time , The first thing to consider is that data can be stored in memory , How to calculate faster .
Usually Top N There are several solutions to the problem .
Top N Solution
Sort all
Sort all data directly ( Quick row, etc ), The disadvantage is that you need to load the data into memory at one time .
Partial elimination
In memory to maintain a size of N The container of , Let the rest of the numbers go into the container one by one , And eliminate the minimum value in the container . Finally, the number left in the container is the first N name . The advantage is that it saves memory , The disadvantage is that it's too slow .
Divide and conquer
Divide the data into groups , In the group, select the first one respectively N A group leader , Finally, let these team leaders compete together , Choose the final front N name .
Hash preprocessing
If the data is highly repetitive , Can pass hash The way , Remove a lot of duplicate data . such as 1 In 100 million data , Half of it is 0, Half of it is 1, So take the front 10 Name time , You can eliminate the other half as 0 The data of .
But preprocessing itself also requires time and space , This requires us to have a clear judgment on the repeatability of the data , Or you'll be smart 、 Run counter to one's desire .
Heap
High frequency test points in interview algorithm —— Heap sort , You can take the front N The number makes up the pile of small roots , The top of the pile is always the minimum . Then go through the following numbers , If it is larger than the top, replace the top and adjust the minimum structure . The time complexity and space complexity of the algorithm ( by N, constant ) Is pretty good , So we have to master .
But which one to choose ? We still need to combine our actual projects and business scenarios to analyze .
Practical solution
Because our database records points , So when the user scale is large , First of all Sub database and sub table , It's usually a horizontal scale , According to certain rules ( such as id) Storing user data rows in multiple tables in batches .
And then with big data Map / Reduce The processing mechanism is the same , May adopt Divide and conquer The way Parallel computing The top of each table 10 name (map), After all the calculations , Put it all together to calculate the final front 10 name (reduce).
In this way , Don't say 1 The hundred million ,2 Billion 、3 The computing model of billion is the same , Just add the horizontal expansion of the machine ~
So I met Top N When it comes to problems , You can answer the above several schemes first , Combined with the specific scene analysis , Divide and conquer and minimum pile are relative to each other The core The point of .
Redis
Last , For the design of real-time leaderboard , I'm sure many friends who have recited the eight part essay will think of using it at the first time Redis Ordered set of zset, It's really a solution , But we should also analyze the pros and cons in combination with the scene , Don't answer in seconds .
Using memory based Redis zset It's faster , And it naturally supports sorting 、 Easy to use . But when the amount of data is large, it also faces data update 、 maintain 、 Sync 、 Persistent storage and so on , And for our low real-time demand , Some of them are overqualified. Ha ha .
I'm fish skin , It's not easy to write , give the thumbs-up It's still a request , I wish you all the best 、 Make a fortune 、 Universiade .
Finally, I'll send you some more Help me get to the big factory offer Learning resources , Video tutorial + exercises + answer + Source code 、 Programming books 、 Big factory surface 、 Practical projects, etc .
The way : ran , leave 6T Resources for !
How I started from scratch through self-study , Get Tencent 、 Byte and other big factories offer Of , You can read this article , No more confusion !
The way : I studied computer for four years , Mutual encouragement !
边栏推荐
- How to turn on win11 notebook power saving mode? How to open win11 computer power saving mode
- 《canvas》之第3章 曲线图形
- 日期、时间库使用备注
- Black box and white box models for interpretable AI
- More than 60 million shovel excrement officials, can they hold a spring of domestic staple food?
- 《canvas》之第1章 canvas概述
- MSSQL high permission injection write horse to Chinese path
- Several misunderstandings of VPN
- Combine with (& &) logic or (||), dynamic binding and ternary operation
- Deploy L2TP in VPN (Part 2)
猜你喜欢

get_ started_ 3dsctf_ two thousand and sixteen

【008】表格数据逐行筛选,跳出for循环及跳过本次循环思路_#VBA

图形技术之坐标转换
![[equalizer] bit error rate performance comparison simulation of LS equalizer, def equalizer and LMMSE equalizer](/img/45/61258aa20cd287047c028f220b7f7a.png)
[equalizer] bit error rate performance comparison simulation of LS equalizer, def equalizer and LMMSE equalizer

(CVE-2020-11978)Airflow dag中的命令注入漏洞复现【vulhub靶场】
╯︵ ┻━┻](/img/26/6986a8ae6c00eb2431a082dc0ff978.png)
[DDCTF2018](╯°□°)╯︵ ┻━┻

相機標定(標定目的、原理)

How to delete / select an input method on your computer
![[image feature extraction] image feature extraction based on pulse coupled neural network (PCNN) including Matlab source code](/img/b3/26cfa385aa357c3a7a77e9db47e94c.png)
[image feature extraction] image feature extraction based on pulse coupled neural network (PCNN) including Matlab source code
![[mrctf2020] thousand layer routine](/img/8e/d7b6e7025b87ea0f43a6123760a113.png)
[mrctf2020] thousand layer routine
随机推荐
buuctf misc [UTCTF2020]docx
[image feature extraction] image feature extraction based on pulse coupled neural network (PCNN) including Matlab source code
Win11 points how to divide disks? How to divide disks in win11 system?
[GUET-CTF2019]zips
[机缘参悟-29]:鬼谷子-内揵篇-与上司交往的五种层次
The seminar on "global IPv6 development and outlook 2020-2021" was held in Beijing
什么是CC攻击?如何判断网站是否被CC攻击? CC攻击怎么防御?
Reppoints: Microsoft skillfully uses deformation convolution to generate point sets for target detection, full of creativity | iccv 2019
Wechat cloud hosting hot issues Q & A
Mysql database recovery case sharing
向量操作与坐标转换相关方法
光照使用的简单总结
相機標定(標定目的、原理)
L2TP connection failure guide in VPN
Tidb operator source code reading (IV) control cycle of components
Tencent cloud security and privacy computing has passed the evaluation of the ICT Institute and obtained national recognition
Knowledge points of 2022 system integration project management engineer examination: ITSS information technology service
Shell script for MySQL real-time synchronization of binlog
How to turn on win11 notebook power saving mode? How to open win11 computer power saving mode
Super fast reading in OI