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

Realize the ranking function.

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

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

The ranking function is realized by using the ordered collection of MongoDB and Redis respectively. Compared with the sorting function of the traditional database, look for the advantages of the ordered set to realize the sorting function.

Specific to a practical example, such as the ranking of gifts sent by viewers of live streaming sites to VJs, if you sort them directly in the database, there are the following disadvantages:

The ranking will be updated in real time, and the data will be sorted every time it changes, which will affect the performance of the database. . Frequently update data, resulting in database performance degradation.

The sorting time is slow when the amount of data is too large.

Adding an index to the sorted field takes up more space.

Sort using redis ordered collections:

(1)。 Import the data from MongoDB directly into an ordered collection called rank in Redis:

(2)。 Displays the ranking of a specific user, with the following code:

(3)。 Displays the ranking of all users, the specific code is as follows:

(4)。 Running effect:

The ordered set is unique (assuming that rk is the name of the ranking, and user1, user2 and so on are the unique identifiers of players). Score can be the same

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