Voting Script, Possibility of Simplifying Database Queries

Posted by Sev on Stack Overflow See other posts from Stack Overflow or by Sev
Published on 2010-05-28T23:47:13Z Indexed on 2010/05/29 1:02 UTC
Read the original article Hit count: 206

Filed under:
|
|

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?

© Stack Overflow or respective owner

Related posts about php

Related posts about sql