voting once- design and optimization
Posted: Tue Nov 21, 2006 5:57 am
I've written a small script which allows users to vote (only once) for the articles uploaded.
So, I've taken this road to accomplish it:
tbl_votes's structure
id_tbl_votes (primary key)
fk_id_user_votes
fk_id_article_votes
Then, if the user "1234" votes the article "9876", I add a row like this:
id_tbl_votes => 1234#9876
fk_id_user_votes => 1234
fk_id_article_votes => 9876
(The vote's value itself goes directly to the article's table )
Now comes the question:
Is it better, when a user votes, try to insert the record and watch for duplicate key error, or select first "1234#9876" and see whether it exists or not?
How do you handle this kind of situation?
So, I've taken this road to accomplish it:
tbl_votes's structure
id_tbl_votes (primary key)
fk_id_user_votes
fk_id_article_votes
Then, if the user "1234" votes the article "9876", I add a row like this:
id_tbl_votes => 1234#9876
fk_id_user_votes => 1234
fk_id_article_votes => 9876
(The vote's value itself goes directly to the article's table )
Now comes the question:
Is it better, when a user votes, try to insert the record and watch for duplicate key error, or select first "1234#9876" and see whether it exists or not?
How do you handle this kind of situation?