There are a few ways to implement counters. Even though it’s not
a complex feature, often I see people having problems around it.
This post describes how bad implementation can impact both
application and MySQL performance and how to improve it.
A customer asked me for help with performance problem they were
facing. I logged into their database and found many client
connections waiting for table locks. Almost all threads were
stuck on one, small table called hits. What was the
reason?
The problem was related to the way they developed a very simple
system for counting page views they later used in some reporting.
The table structure was:
mysql> SHOW CREATE TABLE hits\G
*************************** 1. row ***************************
Table: hits
Create Table: CREATE TABLE `hits` (
`cnt` int(11) NOT NULL
) ENGINE=MyISAM
mysql> SELECT * FROM hits;
+---------+
| cnt |
+---------+
| 3823273 |
+---------+
…
[Read more]