简体   繁体   中英

User actions that affect all the items in a database, how to handle it efficiently?

I am working on a voting system for my company, in this system, each user can vote in one or more topics. Voting in a topic will cause this topic to get +1 in reputation while all other topics will lose -1 in reputation. (These numbers are rather arbitrary, the real values are a complex formula).

I was thinking to structure my database creating a view where I will store the ID for each topics, the total of votes for this topic, the total of votes for other topics, and the calculated reputation.

My problem is regarding performance/scalability. I think this solution is not very effective, because if I have 100 topics and 2 votes per second, I will have to refresh all the values for all the topics two times a second, that would be a total of 200 records per second.

Is this analysis correct? Is mssql engine going to handle it easily? Or should I look for some other approach? What do you suggest?

thanks, Oscar

This is going to be very hard on the database.

What variables does your formula depend on? If the amounts you're adding and subtracting are somewhat global, you can keep track of them and only calculate the score of each topic when you need to display it.

For example, you could increment the score of one topic only, but also increment a global variable that keeps track of how many "upvotes" there are in total. that way when you want to display the score of any topic, you would be able to calculate it on the fly without bombarding your database.

More information about the formula can get you better advice.

EDIT:

You have this formula:

origninalRep + (upvotes*10 - otherVotes) 

You can get originalRep straightforward with one query.

Same for upvotes .

The "tricky" part would be otherVotes . But that's simply globalVotesCount - upvotes , and you have those if you keep track of the global upvotes count.

I hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM