简体   繁体   中英

SQL regularly counting all new votes in one table and incrementing a total count value in another

I have three tables, one with user made levels, and two more tables for upvotes and downvotes on said levels.

I now have nearly 10 million votes cast, and it's becoming very slow to count all the votes for a level whenever is necessary, so I'm planning to add two columns on the levels table, with counts for upvotes and downvotes, and the server will update these maybe once every 24 hours.

The votes tables unfortunately don't have an auto incremented primary key column, instead the primary key is the mapid and userid combined, so one user cannot vote for the same map twice.

So I'm adding a column to the votes tables, "counted", which is 0 if it hasn't been added to the aggregate data, 1 if it has. This way I don't need to recount all the votes that have already been added to the aggregate data, only the new ones.

My query needs to:

-Select all the votes that have their counted set as 0

-Take the mapid from that row, and increment the thumbsup count for that mapid in the levels table

-Set counted to 1 for that vote

I'm using MySQL. All help greatly appreciated!

Table maps_thumbsup
userid | mapid | counted

Table maps_thumbsdown
userid | mapid | counted

Table maps
id | authorname | leveldata | ... | thumbsupcount | thumbsdowncount

doesn't your main table already have the number of votes? T

able maps_thumbsup
userid | mapid | counted

Table maps_thumbsdown
userid | mapid | counted

Table maps
id | authorname | leveldata | ... | thumbsupcount | thumbsdowncount

so for every time someone votes up just use the set command in mysql "set thumbsup = thumbsup + 1" if it doesn't have that 2 columns then add it and can you send me you interface type mysql / mysql / DBO i can update my answer post code for it to count and insert the count in to newly create columns no need to count all the votes every time just get single value from DB then you can use this way to get the votes

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