简体   繁体   English

MySQL更新或插入带有多个查询的整数值以同时更改它

[英]MySQL Update or Insert an integer value with multiple queries to change it at same time

I have an integer column in a MySQL table that I want to add one to or subtract one from (call it a point system). 我在MySQL表中有一个整数列,想要向其中添加一个或从中减去一个(称为点系统)。

AFAIK the two options to this are updating the int column of a single row in a table or inserting a new row every time there is a change as shown below. AFAIK的两个选项是更新表中单行的int列,或每当发生如下所示的更改时插入新行。

id | int
1  | 5
2  | 4
3  | 5
4  | 6

I'm not sure which to choose, since it is possible that many users may be sending queries to change this number, so I don't want the queries to be adding or subtracting a historical number. 我不确定该选择哪个,因为可能有许多用户正在发送查询来更改此数字,所以我不希望查询添加或减去一个历史数字。

Which is the better way or is there a way that is even better that I have not yet considered? 哪一种是更好的方法,或者还有我还没有考虑过的更好的方法? (By 'better' I mean less likely to fail under pressure from lots of queries trying to add/subtract the value). (通过“更好”,我的意思是在尝试添加/减去该值的大量查询的压力下失败的可能性较小)。 I've heard about locking and transactions, but not sure how to implement them in this simple system that I'm sure lots of applications with this problem have solved. 我听说过有关锁定和事务的信息,但是不确定如何在这个简单的系统中实现它们,我相信很多解决此问题的应用程序都已解决。

Thanks. 谢谢。

This is more of a comment than an answer. 这更多是评论而不是答案。

You don't provide enough information to really answer the question. 您没有提供足够的信息来真正回答问题。 The key is how the results are going to be used. 关键是如何使用结果。

Inserting a record into a table is usually faster than updating one, because the SQL engine doesn't need to search for the record in the first place. 将记录插入表中通常比更新记录更快,因为SQL引擎不需要首先搜索记录。 Of course, this gets more complicated if you have indexes and unique constraints on the table. 当然,如果您在表上有索引和唯一约束,则情况会变得更加复杂。

On the other hand, if you need to know the total value, then adding up the values in the table takes time. 另一方面,如果您需要知道总值,则将表中的值相加会花费时间。

So, the balance is really between how often you have increments versus how often you look at the sums. 因此,平衡实际上是您增加的频率与查看总和的频率之间。 If you never look at the sums, the insert is probably faster. 如果您从不查看总和,则insert操作可能会更快。 In general, though, I would lean toward updating the values. 不过,总的来说,我倾向于更新值。

I would figure that updating the integer value for the id would be the correct way to handle this. 我认为更新id的整数值将是处理此问题的正确方法。 As there is obviously the potential for multiple requests to update the integer value at the same time you don't want to use a set in stone query like this: 由于显然您不希望在石头查询中使用这样的集合,因此有多个请求可能同时更新整数值,如下所示:

UPDATE pointstable SET int = 7 WHERE id = '4';

Instead you want the query to perform the math increment like this: 相反,您希望查询执行如下数学增量:

UPDATE pointstable SET int = (int + 1) WHERE id = '4';

for a simple case you can make use of MySQL MyISAM engine and use table locking for concurrency - the locking is very quick. 对于简单的情况,您可以使用MySQL MyISAM引擎并使用表锁定进行并发-锁定非常快。 Unless you are talking like a million people trying to update the counter, scalability should not be an issue 除非您像一百万人一样尝试更新计数器,否则可伸缩性就不会成为问题

so the algorithm is 所以算法是

when a user wishes to modify the number you do this 当用户希望修改号码时,您可以这样做

LOCK TABLES pointstable WRITE; 
UPDATE pointstable SET int = (int + 4) WHERE id = 3;
UNLOCK TABLES; 

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

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