This question is placed under the Android node. Do you want to implement weight calculation under Android?
If it is on the server, I agree with @zys's plan. In addition, to reduce the number of calculations, I have the following humble opinion:
Add an updated_at and calculated_at fields to the data to be calculated, which respectively represent the latest update time of the data and the last time the weight value was calculated using the sorting algorithm. time . When a new user votes, update the updated_at field of the data. After the scheduled task calculates the weight, update the calculated_at field of the data.
In the weight calculation algorithm, updated_at and calculated_at are compared. When calculated_at > updated_at, does not need to be repeated. Calculate the weight value.
Using this simple algorithm, a large part of the data that has not been updated can be effectively removed. In addition, if the old data has been updated, it can also be included in the weight calculation range.
I have done similar things before and handled it like this. I put the time-consuming calculations in the early morning of every day and used scheduled tasks to do it. Then the results were stored in the order field or a sorting table. When reading the data, order by or related Just query the table.
You can put a separate table. When the content of the article that may affect the weight changes, you can directly update the values in the table. It should be able to withstand a considerable number. Even if it grows in the future, you can only do this for articles within a period of time. Sorting, there should be no problem.
To answer this question at a higher level, it may be helpful to your thinking.
The ways to improve performance are nothing more than a few common ideas. If you think about it, you will usually come to your own conclusion.
提升算法的效率: The weight calculation formula should not be too complex to improve the calculation speed.
用空间换时间: The weight calculation result is recorded in a temporary field, so that there is no need to repeatedly calculate the weight during sorting.
避免不必要的运算: Remove operations that have little impact on the results. For example, data from one month ago are not included in the sorting to reduce the data size of the operation.
用近似值代表准确值: Do not calculate weights in real time. Refreshing weights regularly can effectively reduce the number of operations.
This question is placed under the Android node. Do you want to implement weight calculation under Android?
If it is on the server, I agree with @zys's plan. In addition, to reduce the number of calculations, I have the following humble opinion:
Add an updated_at and calculated_at fields to the data to be calculated, which respectively represent the latest update time of the data and the last time the weight value was calculated using the sorting algorithm. time . When a new user votes, update the updated_at field of the data. After the scheduled task calculates the weight, update the calculated_at field of the data.
In the weight calculation algorithm, updated_at and calculated_at are compared. When calculated_at > updated_at, does not need to be repeated. Calculate the weight value.
Using this simple algorithm, a large part of the data that has not been updated can be effectively removed. In addition, if the old data has been updated, it can also be included in the weight calculation range.
For community and news clients, you cannot bring last month’s data when sorting, so there isn’t much.
I have done similar things before and handled it like this. I put the time-consuming calculations in the early morning of every day and used scheduled tasks to do it. Then the results were stored in the order field or a sorting table. When reading the data, order by or related Just query the table.
You can put a separate table. When the content of the article that may affect the weight changes, you can directly update the values in the table. It should be able to withstand a considerable number. Even if it grows in the future, you can only do this for articles within a period of time. Sorting, there should be no problem.
To answer this question at a higher level, it may be helpful to your thinking.
The ways to improve performance are nothing more than a few common ideas. If you think about it, you will usually come to your own conclusion.
提升算法的效率
: The weight calculation formula should not be too complex to improve the calculation speed.用空间换时间
: The weight calculation result is recorded in a temporary field, so that there is no need to repeatedly calculate the weight during sorting.避免不必要的运算
: Remove operations that have little impact on the results. For example, data from one month ago are not included in the sorting to reduce the data size of the operation.用近似值代表准确值
: Do not calculate weights in real time. Refreshing weights regularly can effectively reduce the number of operations.The summary above is very good.