数据库中增量计数器字段(保存加载数据库)
场景:数据库保存了多个经常更新的计数器。
更新语句很简单:
UPDATE table SET count = count + 1
但是更新非常频繁(不需要不断地显示更新值),因此可能需要找到一种分块升级的方法(用于保存连接):
而不是+1,+1,在短时间内(每隔一秒或几秒)+1、+1、+1、+1、+1、+1、+1、+1,在长时间段内(每 5 分钟)+80。
如何更新计数器值以保存频繁连接?
(注意:我在MySQL数据库中看到UPDATE LOW_PRIORITY,我发现类似的东西)
Scenario: A database keeps several counters that are updated frequently.
The update statement is simple:
UPDATE table SET count = count + 1
However, the updates are very frequent (not required show updated value constantly), so that it might be appropriate to find a way to upgrade in blocks (for save connections):
Instead of +1, +1, +1, +1, +1, +1, +1, +1, +1, +1 in short time periods (every one or few seconds), make a +80 in long time periods blocks (every 5 minutes).
How I can update counter value for save frequent connections?
(Note: I see UPDATE LOW_PRIORITY in MySQL databases, I find things similar)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
计数器是一个非常简单的东西。如果需要,您可以将号码保存在文本文件中。在服务器上保存到本地文件通常比在数据库上运行查询更容易,而且听起来您已经对减少数据库的负载感兴趣。如果您担心竞争条件,请使用文件块(例如 flock) ;然后让脚本以您认为合适的频率将数据库与文本文件同步(可能使用 计划任务)。
A counter is a very simple thing. You could save the number in a text file if you want. Saving to a local file is usually easier on the server than running a query on the database and it sounds like you are already interested in reducing the load on the database. Use a file block (such as flock) if you are worried about the race condition; then have a script synchronize the database with the text file(s) with the frequency you think is appropriate (perhaps using a cron job).
自动回答:我找到了快速高效的临时存储系统(而不是文件):
Auto-answer: I found fast and efficient system for temporary storage (instead of file):