[英]Is it bad to continually increment a counter column in a single row in mysql?
[英]Consistent counter column increment?
這是我擁有的update
語句,它似乎正在執行我想要的操作,即在給定表的特定記錄上增加計數器列:
update config_per_geo
inner join country on config_per_geo.country_id = country.id
set counter = counter + 1
where product_id in (?, ?, ?)
and (country.iso_code = ? or
(country.iso_code = 'ZZZ' and product_id not in
(select product_id
from (select * from config_per_geo) as cpg1
inner join country as cty1 on cpg1.country_id = cty1.id
where cty1.iso_code = ?)
)
)
(由於這個限制,似乎有必要使用cpg1
別名)
事實是,應該從Web應用程序(每個HTTP請求)執行此查詢,在該應用程序中,可能有多個同時請求,也就是說,在並發,多線程環境中。 此外,我需要每天重置一次計數器字段,並且我認為我可以通過運行CRON作業或其他方式執行此操作:
start transaction;
select counter from config_per_geo for update;
update config_per_geo set counter = 0;
commit;
問題:我想知道的是,這兩個查詢(事務)是否將counter
列始終保持一致狀態,更具體地說-第一個查詢在並發執行時(從並發請求線程)是否會一致地以原子方式遞增計數器?
我想他們應該這樣做,但是基於MySQL文檔,我無法一生確定100%的可靠性...
您不需要第二條語句中的交易代碼。 只需使用
update config_per_geo set counter = 0;
config_per_geo將保持一致。
另請參閱https://stackoverflow.com/a/1171807/4350148關於單語句交易
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.