sql - Many to many relationship and MySQL -


if wanted make database subscribers (think youtube), thought have 1 table containing user information such user id, email, etc. table (subscription table) containing 2 columns: 1 user id , 1 new subscriber's user id.

so if user id 101 , user 312 subscribes me, subscription table updated new row containing 101 in column 1 , 312 in column 2.

my issue every time 101 gets new subscriber, adds id subscription table meaning can't set primary key subscription table user id can present many times each of subscribers , primary key requires unique value.

also in event there's lot of subscriptions going on, won't slow search of 101's followers rows have searched , checked every time 101 in first column , check user id (the subscriber 101) in second column?

is there's more optimal solution problem?

thanks!

in case, pairs (user_id, subscriber_id) unique (a user can't have 2 subscriptions user, can they?). make compound primary key consisting of both fields if need one.

regarding speed of querying subscription table: think queries you'll run on table, , add appropriate indexes. common operation might "give me list of subscribers", translate like

select subscriber_id subscriptions user_id = 123; 

(possibly part of join). if have indexed user_id column, query can run quite efficiently.


Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -