无缝地将1添加到上一行的值? [英] Seamlessly adding 1 to the previous row value?
问题描述
我想在hit_count
的上一行的值上加1,但是如果快速运行多个查询(即每秒每秒多次加载页面),恐怕这样做可能并不安全.这是针对我正在制作的Web应用程序,因此,我想确保很好地支持任何数量的页面加载.)
I want to add 1 to the value of the previous row for hit_count
, but I'm afraid doing it may not be safe if multiple queries are being run quickly (i.e. the page is being loaded several times a second - this is for a web-app I'm making, and so I want to make sure any amount of page loads is supported well).
这就是我的想法:
$result = mysql_query("SELECT * FROM rotation");
$fetch = mysql_fetch_assoc($result);
$update_hit = $fetch['hit_counter']+1;
$query = "INSERT INTO rotation (hit_counter, rotation_name) VALUES ('$update_hit', '$rotation_name')";
$result = mysql_query($query);
我曾考虑过将hit_counter
列设置为UNIQUE KEY
,但是我不知道那之后我还会做什么.
I thought about setting the hit_counter
column to a UNIQUE KEY
, but I don't know what else I'd do after that.
我会使用AUTO_INCREMENT
,但是问题是,我需要脚本其余部分中的实际hit_counter
值.
I would use AUTO_INCREMENT
but the problem is, I need the actual hit_counter
value within the rest of the script.
任何想法,评论和建议,将不胜感激!
Any ideas, comments, advice would be greatly appreciated!
编辑:我使用的是hit_count
和hit_counter
,是一个错字.已更新,以避免任何混乱.
I used hit_count
and hit_counter
, was a typo. Updated to avoid any confusion.
推荐答案
You can use the DUPLICATE KEY functionality when you make name + counter a unique value:
INSERT INTO rotation SET hit_counter = 1, rotation_name= 'name'
ON DUPLICATE KEY UPDATE hit_counter = hit_counter + 1
明智地使用性能(如果您的要求允许的话),我建议使用f.e等缓存机制批量(每100次匹配)推送更新.内存缓存.
Performance wise (and if your requirements allow it) I advice pushing updates in bulk (per 100 hits) using a caching mechanism like f.e. memcached.
这篇关于无缝地将1添加到上一行的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!