 <p>The counter i optimized works like this:</p> <pre><code>UPDATE page_views SET counter = counter + 1 WHERE page_id = x if (affected_rows == 0 ) { INSERT INTO page_views (page_id, counter) VALUES (x, 1) } </code></pre> <p>This way you run 2 query for the first view, the other views require only 1 query.</p>