G
G
gikm2017-04-16 09:02:39
MySQL
gikm, 2017-04-16 09:02:39

How to speed up MySQL UPDATE?

Greetings!
In order for each news to have a counter of the number of views on the blog, when you open an article, the following request occurs:

UPDATE `table` SET `views` = `views` + 1 WHERE id = '123'

Those. the view counter field of each news, when it is opened, is incremented by 1.
But, as it turned out, such requests slow down the server very, very much (there are a lot of them).
How can this task be better optimized?
PS - InnoDB, the index hangs on id and views, SELECT selections are constantly made from the same table.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
Dimonchik, 2017-04-16
@dimonchik2013

you can trigger on each select,
but because correctly, you don’t need to go after a cached article every time - make a separate accounting service: insert logs for each view and post-process data daily / hourly / weekly / second

F
Fortop, 2017-04-16
@Fortop

The problem is optimized in a trivial way.
Realtime counters are transferred to Redis (another key-value storage), and the update to the database should be done once a day, for example, by cron.
Naturally, if you need up-to-date data at the current moment, then they must be taken from Redis

N
Nikolai Chuprik, 2017-04-17
@choupa

I'm not special at all, but if you make a table in the form of a "stack" with auto-increment or even without it
INSERT INTO stack VALUES ('article_id');
And then count the number of lines for each news and periodically clear the table.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question