mysql - Better way to retrieve the Count of records -
i dealing small-scale website has tags identify genre of topic. every tag has followers similar so. when tag being entered, suggestions populate shows auto-completed tags along followers.
what best way design table these tags ease retrieval follower-count , manage tags.
1. 'tags'( id int, tagname string) tag_followers( tag_id int, userid int)
followerscount query :
select count(*) tag_followers tag_id = (select id tags tagname = "games")
.......
2. 'tags'( id int, tagname string, follower_count int) tag_followers( tag_id int, userid int)
followerscount query :
select follower_count tags tagname = "games"
i had these 2 ideas. can't judge me in fast retrieval on long run. please me choose. also, if there better way above two, please suggest me. in advance.
i suggest first approach , query should like:
select count(1) tag_followers tf join tags t on tf.tag_id = t.id , tagname = "games"
if need count don't use count(*), use count(1). because count(*) consider columns while count(1) consider 1 column(first column) while calculating count.
steps needed in second approach:
- first need select follower_count tags table
- then need increment , update tags table , need to
- insert tag_followers table.
but in first approach step 3 needed.
Comments
Post a Comment