Suppose I have a MySQL table that keeps the page visit count and I want to keep track of total number of page visits for each user. Here’s the SQL for incrementing the field:
UPDATE visits
SET visits = visits + 1
WHERE user_id = 12
It’s pretty simple but I wonder whether there’s a faster way to achieve this. I mean if I have a lot of visitors (ideally millions of users per day), is this method enough or I should use an alternative method. Thanks.
With an index on
user_idthis will be pretty fast. I doubt you’ll be able to achieve a (noticeably) faster result using any other means. You will likely run into other performance / server issues with millions of users than this query (some call this “micro-optimization”)