Currently im using UPDATE pages SET hits = hits+1 WHERE id=:id to increment page hits with PDO, but while profiling the script its taking on average between 30ms & 65ms to update.
Is there a faster way to increment, hits is also a INDEX within the table:
Here is an example dump:
[queries] => Array
(
[0] => Array
(
[sql] => SELECT * FROM `settings`
[time] => 0.22602081298828
)
[1] => Array
(
[sql] => SELECT * FROM `menu_links` WHERE `active`="1" ORDER BY position ASC
[time] => 0.2291202545166
)
[2] => Array
(
[sql] => SELECT * FROM `pages` WHERE `url`=:field AND active='1'
[time] => 0.27203559875488
)
[3] => Array
(
[sql] => SELECT * FROM `pages` WHERE `menu_link`=:field AND active='1'
[time] => 0.24008750915527
)
[4] => Array
(
[sql] => UPDATE pages SET hits = hits+1 WHERE id=:id
[time] => 31.989107131958
)
)
UPDATE
It seems that using a LIMIT 1 clause has sped it up by 10-15ms
[4] => Array
(
[sql] => UPDATE `pages` SET hits = hits+1 WHERE id=1 LIMIT 1
[time] => Between 15 & 25ms
)
with LOW_PRIORITY
[4] => Array
(
[sql] => UPDATE LOW_PRIORITY `pages` SET hits = hits+1 WHERE id=1 LIMIT 1
[time] => Between 17 & 30ms
)
It is the best way of doing that.
And obviously update takes more time than select because it implies storage modifications, while selects usually use various caches and buffers.
If the speed is very important to you – you can set up any queue software and add incrementing task to the queue and refresh the data in a bulk manner by some backgound worker. This solution would be much more efficient but only applicable if you don’t need your changes to be available immediately.