Manz
Manz

Reputation: 988

Increment counter field in database (saving load database)

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)

Upvotes: 1

Views: 1594

Answers (2)

Manz
Manz

Reputation: 988

Auto-answer: I found fast and efficient system for temporary storage (instead of file):

Upvotes: 0

ghbarratt
ghbarratt

Reputation: 11711

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

Upvotes: 2

Related Questions