简体   繁体   English

SQL定期在一个表中计数所有新投票,在另一个表中递增总计数值

[英]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. 我现在有近1000万张选票,并且在必要时对一个级别的所有选票进行计数变得非常缓慢,因此我计划在级别表上添加两列,其中包含向上和向下投票以及服务器的计数可能每24小时更新一次。

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. 遗憾的是,投票表没有自动递增的主键列,而是主键是mapid和userid的总和,因此一个用户无法为同一张地图投票两次。

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. 因此,我要在票数表中添加一列“已计数”,如果尚未将其添加到汇总数据中,则为0,如果已添加,则为1。 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 -选择所有计为0的选票

-Take the mapid from that row, and increment the thumbsup count for that mapid in the levels table -从该行中获取mapid,并在级别表中增加该mapid的大拇指数

-Set counted to 1 for that vote -该票数设为1

I'm using MySQL. 我正在使用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 因此,每当有人投票时,只要在mysql中使用set命令“ set thumbsup = thumbsup + 1”,如果它没有那2列,然后添加它,就可以给我发送接口类型mysql / mysql / DBO,我可以更新我的答案发布代码可以对其进行计数并将其插入到新创建的列中,而不必每次都从DB获得单个值时就对所有选票进行计数,那么您可以使用这种方式来获得选票

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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