I have a voting script which stores the post_id and the user_id in a table, to determine whether a particular user has already voted on a post and disallow them in the future.
To do that, I am doing the following 3 queries.
SELECT user_id, post_id from votes_table where postid=? AND user_id=?
If that returns no rows, then:
UPDATE post_table set votecount = votecount-1 where post_id = ?
Then
SELECT votecount from post where post_id=?
To display the new votecount on the web page
Any better way to do this? 3 queries are seriously slowing down the user's voting experience
Edit
- In the votes table, vote_id is a primary key
- In the post table, post_id is a primary key.
- Any other suggestions to speed things up?