开发者

Increment counter field in database (saving load database)

开发者 https://www.devze.com 2023-04-13 05:06 出处:网络
Scenario: A database keeps several counters that are updated frequently. The update statement is simple:

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 c开发者_高级运维onstantly), 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)


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):

  • Redis: http://redis.io/ (ideally, persistent)
  • Memcached: http://memcached.org/
0

精彩评论

暂无评论...
验证码 换一张
取 消